Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 1.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 1."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 1

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter More About Classes 14

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Instance and Static Members 14.1

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 4 Instance and Static Members instance variable: a member variable in a class. Each object has its own copy. static variable: one variable shared among all objects of a class static member function: can be used to access static member variable; can be called before any objects are defined

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 5 static member variable Contents of Tree. h 1 // Tree class 2 class Tree 3 { 4 private: 5 static int objectCount; // Static member variable. 6 public: 7 // Constructor 8 Tree() 9 { objectCount++; } 10 11 // Accessor function for objectCount 12 int getObjectCount() const 13 { return objectCount; } 14 }; 15 16 // Definition of the static member variable, written 17 // outside the class. 18 int Tree::objectCount = 0; Static member declared here. Static member defined here.

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 6

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 7 Three Instances of the Tree Class, But Only One objectCount Variable

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 8 static member function Declared with static before return type: static int getObjectCount() { return objectCount; } Static member functions can only access static member data Can be called independent of objects: int num = Tree::getObjectCount();

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 9 Modified Version of Tree. h 1 // Tree class 2 class Tree 3 { 4 private: 5 static int objectCount; // Static member variable. 6 public: 7 // Constructor 8 Tree() 9 { objectCount++; } 10 11 // Accessor function for objectCount 12 static int getObjectCount() 13 { return objectCount; } 14 }; 15 16 // Definition of the static member variable, written 17 // outside the class. 18 int Tree::objectCount = 0; Now we can call the function like this: cout << "There are " << Tree::getObjectCount() << " objects.\n";

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Friends of Classes 14.2

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 11 Friends of Classes Friend: a function or class that is not a member of a class, but has access to private members of the class A friend function can be a stand-alone function or a member function of another class It is declared a friend of a class with friend keyword in the function prototype

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 12 friend Function Declarations Stand-alone function: friend void setAVal(intVal&, int); // declares setAVal function to be // a friend of this class Member function of another class: friend void SomeClass::setNum(int num) // setNum function from SomeClass // class is a friend of this class

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 13 friend Class Declarations Class as a friend of a class: class FriendClass {... }; class NewClass { public: friend class FriendClass; // declares // entire class FriendClass as a friend // of this class … };

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Memberwise Assignment 14.3

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 15 Memberwise Assignment Can use = to assign one object to another, or to initialize an object with an object’s data Copies member to member. e.g., instance2 = instance1; means: copy all member values from instance1 and assign to the corresponding member variables of instance2 Use at initialization: Rectangle r2 = r1;

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 16 // Specification file for the Rectangle class // This version has a constructor. #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double width; double length; public: Rectangle(double, double); // Constructor void setWidth(double); void setLength(double); double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 17 // Define two Rectangle objects. Rectangle box1(10.0, 10.0); // width = 10.0, length = 10.0 Rectangle box2 (20.0, 20.0); // width = 20.0, length = 20.0 // Display each object's width and length. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl << endl; // Assign the members of box1 to box2. box2 = box1; // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl;

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 18

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Copy Constructors 14.4

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 20 Copy Constructors Memberwise Assignment Problem: what if object contains a pointer?

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 21 // Specification file for the Rectangle class // This version has a constructor. #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double * width; double * length; public: Rectangle(double, double); // Constructor void setWidth(double); void setLength(double); double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif Rectangle(double w, double l) { width = new double; *width = w; length = new double; *length = l; }

22 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 22 // Define two Rectangle objects. Rectangle box1(10.0, 10.0); // width = 10.0, length = 10.0 Rectangle box2 (20.0, 20.0); // width = 20.0, length = 20.0 // Display each object's width and length. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl << endl; // Assign the members of box1 to box2. box2 = box1; // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl; //update box2 box2.setWidth(30.0); box2.setLength(30.0); // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl;

23 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 23 box1's width and length: 10 10 box2's width and length: 20 20 box1's width and length: 10 10 box2's width and length: 10 10 box1's width and length: 30 30 box2's width and length: 30 30 Program Output

24 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 24 Copy Constructors Special constructor used when a newly created object is initialized to the data of another object of same class Default copy constructor copies field-to-field Default copy constructor works fine in many cases

25 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 25 class SomeClass { public: SomeClass(int val = 0) {value=new int; *value = val;} int getVal(); void setVal(int); private: int *value; }

26 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 26 Copy Constructors What we get using memberwise copy with objects containing dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // also 13 object1 object2 value 13

27 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 27 Programmer-Defined Copy Constructor Allows us to solve problem with objects containing pointers: SomeClass::SomeClass(const SomeClass &obj) { value = new int; *value = *(obj.value); } Copy constructor takes a reference parameter to an object of the class

28 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 28 Programmer-Defined Copy Constructor Each object now points to separate dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // still 5 object1 object2 value 135

29 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 29 Programmer-Defined Copy Constructor Since copy constructor has a reference to the object it is copying from, SomeClass::SomeClass(SomeClass &obj) it can modify that object. To prevent this from happening, make the object parameter const: SomeClass::SomeClass (const SomeClass &obj)

30 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 30 // Specification file for the Rectangle class // This version has a constructor. #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double * width; double * length; public: Rectangle(double, double); // Constructor Rectangle(const Rectangle &r); //Copy Constructor void setWidth(double); void setLength(double); double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif Rectangle::Rectangle(const Rectangle &r) { width = new double; *width = *(r.width); length = new double; *length = *(r.length); }

31 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 31 // Define two Rectangle objects. Rectangle box1(10.0, 10.0); // width = 10.0, length = 10.0 Rectangle box2 (box1); // call the copy constructor // Display each object's width and length. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl << endl; box2.setWidth(30.0); box2.setLength(30.0); // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl;

32 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 32 box1's width and length: 10 10 box2's width and length: 10 10 box1's width and length: 10 10 box2's width and length: 30 30 Program Output

33 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 33

34 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 34 The this Pointer Sometimes you need to reference a class’s hidden data field in a function For example, a data field name is often used as the parameter name in a set function for the data field In this case, you need to reference the hidden data field name in the function in order to set a new value to it A hidden data field can be accessed by using the this keyword

35 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 35 The this Pointer this : predefined pointer available to a class’s member functions Always points to the instance (object) of the class whose function is being called Is passed as a hidden argument to all non-static member functions Can be used to access members that may be hidden by parameters with same name

36 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 36 this Pointer Example class SomeClass { private: int num; public: void setNum(int num) { this->num = num; }... };

37 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Overloading 14.5

38 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 38 Operator Overloading Operators such as =, +, and others can be redefined when used with objects of a class The name of the function for the overloaded operator is operator followed by the operator symbol, e.g., operator+ to overload the + operator, and operator= to overload the = operator Prototype for the overloaded operator goes in the declaration of the class that is overloading it Overloaded operator function definition goes with other member functions

39 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 39 Operator Overloading Prototype: void operator=(const SomeClass &rval) Operator is called via object on left side return type function name parameter for object on right side of operator

40 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 40 Invoking an Overloaded Operator Operator can be invoked as a member function: object1.operator=(object2); It can also be used in more conventional manner: object1 = object2;

41 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 41 // Specification file for the Rectangle class // This version has a constructor. #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double * width; double * length; public: Rectangle(double, double); // Constructor Rectangle(const Rectangle &r); //Copy Constructor Rectangle operator=(const Rectangle &r); void setWidth(double); void setLength(double); double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif Rectangle Rectangle::operator=(const Rectangle &r) { width = new double; *width = *(r.width); length = new double; *length = *(r.length); return *this; }

42 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 42 // Define two Rectangle objects. Rectangle box1(10.0, 10.0); // width = 10.0, length = 10.0 Rectangle box2 (20.0, 20.0); // width = 20.0, length = 20.0 // Display each object's width and length. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl << endl; // Assign the members of box1 to box2. box2 = box1; // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl; //update box2 box2.setWidth(30.0); box2.setLength(30.0); // Display each object's width and length again. cout << "box1's width and length: " << box1.getWidth() << " " << box1.getLength() << endl; cout << "box2's width and length: " << box2.getWidth() << " " << box2.getLength() << endl;

43 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 43 box1's width and length: 10 10 box2's width and length: 20 20 box1's width and length: 10 10 box2's width and length: 10 10 box1's width and length: 10 10 box2's width and length: 30 30 Program Output

44 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 44 Returning a Value Overloaded operator can return a value class Point2d { public: double operator-(const point2d &right) { return sqrt(pow((x-right.x),2) + pow((y-right.y),2)); }... private: int x, y; }; Point2d point1(2,2), point2(4,4); // Compute and display distance between 2 points. cout << point2 – point1 << endl; // displays 2.82843

45 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 45 Returning a Value Return type the same as the left operand supports notation like: object1 = object2 = object3; Function declared as follows: const SomeClass operator=(const someClass &rval) In function, include as last statement: return *this;

46 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 46 Notes on Overloaded Operators Can change meaning of an operator Cannot change the number of operands of the operator (e.g., cannot overload + operator with 2 or more operands) Only certain operators can be overloaded. Cannot overload the following operators: ?:..* :: sizeof

47 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 47 Example: Overload +, () operator class Point { int x, y; public: Point() {} Point(int px, int py) { x = px; y = py; } void show() { cout << x << " "; cout << y << "\n"; } Point operator+(Point op2); //overload + operator Point operator()(int i, int j); //overload () operator };

48 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 48 // Overload ( ) for Point. Point Point::operator()(int i, int j) { x = i; y = j; return *this; } // Overload + for Point. Point Point::operator+(Point op2) { Point temp; temp.x = op2.x + x; temp.y = op2.y + y; return temp; } int main() { Point ob1(10, 20), ob2(1, 1); ob1.show(); ob1(7, 8); // can be executed by itself ob1.show(); ob1 = ob2 + ob1(10, 10); // can be used in expressions ob1.show(); return 0; }

49 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 49 Another Example class Point { public: Point (int x, int y) {this->x = x; this->y = y;} Point operator+(Point &p) {return Point(x + p.x, y + p.y);} Point operator-(Point &p) {return Point(x - p.x, y - p.y);} private: int x, y; }; Point p1(10,20), p2(10,20); Point p3 = p1 + p2; Point p4 = p1 - p2;

50 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 50 Can also overload operators globally class Point { public: Point (int x, int y) {this->x = x; this->y = y;} friend Point operator + (Point &p, Point &q); friend Point operator - (Point &p, Point &q); private: int x, y; }; Point operator + (Point &p, Point &q) { return Point(p.x + q.x, p.y + q.y); } Point operator - (Point &p, Point &q) { return Point(p.x - q.x, p.y - q.y); }

51 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 51 Overloading Types of Operators ++, -- operators overloaded differently for prefix vs. postfix notation Overloaded relational operators should return a bool value Overloaded stream operators >>, << must return reference to istream, ostream objects and take istream, ostream objects as parameters

52 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 52 Example: Overload > #include class Time { int hour; int minute; int second; public: friend ostream& operator >( istream &,Time &); }; ostream& operator<<( ostream & out, Time t) { out<<"\nHere's the time:\n"; out<<t.hour<<":"<<t.minute<<":"<<t.second<<"\n"; return out; }

53 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 53 istream& operator>>( istream & in,Time & t) { cout >t.hour; if((t.hour 23)) cout 23)); do{ cout >t.minute; if((t.minute 59)) cout 59)); do{ cout >t.second; if((t.second 59)) cout 59)); return in; } int main() { Time now; cin>>now; cout<<now; return 0; }

54 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 54 Overloaded [] Operator Can create classes that behave like arrays, provide bounds-checking on subscripts Must consider constructor, destructor Overloaded [] returns a reference to object, not an object itself

55 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 55 #include #include using namespace std; class Point { int a[3]; public: Point(int i, int j, int k) { a[0] = i; a[1] = j; a[2] = k; } int &operator[](int i); }; // Provide range checking for Point. int &Point::operator[](int i) { if(i 2) { cout << "Boundary Error\n"; exit(1); } return a[i]; } int main() { Point ob(1, 2, 3); cout << ob[1]; // displays 2 cout << " "; ob[1] = 25; // [] appears on left cout << ob[1]; // displays 25 ob[3] = 44; // generates runtime error, 3 out-of-range return 0; }

56 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object Conversion 14.6

57 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 57 Object Conversion Type of an object can be converted to another type Automatically done for built-in data types Must write an operator function to perform conversion To convert an FeetInches object to an int : FeetInches::operator int() {return feet;} Assuming distance is a FeetInches object, allows statements like: int d = distance;

58 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Aggregation 14.7

59 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 59 Aggregation Aggregation: a class is a member of a class Supports the modeling of ‘has a’ relationship between classes – enclosing class ‘has a’ enclosed class Same notation as for structures within structures

60 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 60 Aggregation class StudentInfo { private: string firstName, LastName; string address, city, state, zip;... }; class Student { private: StudentInfo personalData;... };

61 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 61 See the Instructor, TextBook, and Course classes in Chapter 14.

62 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 62 Case Study Create a class named DentalAppointment. Include fields for a patient’s data (i.e., last name, first name, and zip code) the date the time and the duration of the appointment in minutes also include a field that contains the ending time of the appointment; this field will be calculated based on the start time and the duration. The DentalAppointment constructor requires a first and last name, and a month, day, year, hour, and minute for the appointment. Allow a DentalAppointment to be constructed with or without an additional argument for appointment duration, and force the duration to 30 minutes when no argument is supplied. The constructor does not allow any appointment over 240 minutes. The constructor calculates the appointment ending time based on the start time and the duration. Also include a display function for the DentalAppointment class.


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 14- 1."

Similar presentations


Ads by Google