lecture16

lecture16 - Wednesday,December1 st ClassChallenges...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, December 1 st   Class Challenges  Enumerated Types Classes before C++ Classes Under The Hood The “this” pointer Operator Overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class Challenge Part A : Which of the following lines in our  main  function is invalid given  the Rect class below? class Rect { public: Rect(float x, float y=0) { w = x; h = y; } float GetArea(void) { return( w * h ); } private: float w, h; }; int main(void) { Rect a(5,6); // OK? Rect b(5); // OK? Rect *c; // OK? Rect d[10]; // OK? Rect *e[10]; // OK? } Part B:  Show the two ways you  can fix the class so it will accept  all five lines below.
Background image of page 2
Class Challenge Question : What will the following  program print out?  Or does it not compile at all? class Goober { public: Goober(int x) { cout << “First c’tor\n”; ival = x; dval = 0.0; } Goober(double y) { cout << “Second c’tor\n”; dval = y; ival = 0; } private: int ival; double dval; }; int main(void) { Goober a(5); Goober b(5.0); } Question #2 : Convert the  constructors to use initializer  lists.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class Challenge Given the  Rect  class, write a  main   function that: 1. Defines an array of  10 Rect  pointers  2. Loops  ten  times: A. Prompts the user to enter the  dimensions of a rectangle B. Allocates a rectangle object  with   new C. Sets the next slot in the array to  point to the new rectangle. 3. Prints out all ten of the rectangle’s  areas. class Rect { public: void Rect(float x, float y) { w = x; h = y; } float GetArea(void) { return( w * h ); } private: float w, h; };
Background image of page 4
Class Challenge What will the following program print  out? class Carey { public: Carey(string lastName = “Nacho”) { ln = lastName; cout << “Carey “<<ln<<“ is born\n”; } ~Carey() { cout << “Carey “<<ln<<“ dies\n”; } private: string ln; }; Carey a(“Grant”); void blah(void) { Carey c; } int main(void) { Carey arr[3]; Carey *ptr; ptr = new Carey(“Mariah”); for (int i=0;i<2;i++) { static Carey b(“Airy”); blah(); } delete ptr; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class Challenge Convert the following program to use  new and delete for  EVERY  variable. class Cubes { public: Cubes(void) { for (int i=0;i<10;i++) arr[i] = i*i*i; } int GetNthCube(int num) const { return(arr[num]); } private: int arr[10]; }; int main(void) { Cubes a; cout << a.GetNthCube(5); }
Background image of page 6
Struct Challenges Write a function called printCars  that prints out the following  information for each car in array  arr :   name1 horsepower1 cylinders1   name2 horsepower2 cylinders2   Etc. . Your function may not use any  bracket notation. struct ENGINE { int horsePower; int cylinders; }; struct CAR { string name; ENGINE *ptr; }; int printCars(CAR arr[],int num) { // fill in the blank }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Enumerated Types enum Scaries { Ghosts, Goblins, Witches, Nerds }; int main(void) { Scaries a, b; a = Ghosts; b = Goblins; if (a == Nerds) cout << “So scary!\n”; if (a < b) cout “Ghosts are < goblins”; } You can use the  enum  command in C++ to define a new  data type  that has a  specific set of possible symbolic values.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2009 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 29

lecture16 - Wednesday,December1 st ClassChallenges...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online