Chapter 9 ObjectOriented Programming Inheritance Outline 9 1
Chapter 9 - Object-Oriented Programming: Inheritance Outline 9. 1 9. 2 9. 3 9. 4 9. 5 9. 6 9. 7 9. 8 9. 9 Introduction Base Classes and Derived Classes protected Members Relationship between Base Classes and Derived Classes Case Study: Three-Level Inheritance Hierarchy Constructors and Destructors in Derived Classes “Uses A” and “Knows A” Relationships public, protected and private Inheritance Software Engineering with Inheritance 2003 Prentice Hall, Inc. All rights reserved. 1
2 9. 1 Introduction • Inheritance – Software reusability – Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities – Derived class inherits from base class • Derived class – More specialized group of objects – Behaviors inherited from base class • Can customize – Additional behaviors 2003 Prentice Hall, Inc. All rights reserved.
3 9. 1 Introduction • Class hierarchy – Direct base class • Inherited explicitly (one level up hierarchy) – Indirect base class • Inherited two or more levels up hierarchy – Single inheritance • Inherits from one base class – Multiple inheritance • Inherits from multiple base classes – Base classes possibly unrelated • Chapter 22 2003 Prentice Hall, Inc. All rights reserved.
4 9. 1 Introduction • Three types of inheritance – public • Every object of derived class also object of base class – Base-class objects not objects of derived classes – Example: All cars vehicles, but not all vehicles cars • Can access non-private members of base class – Derived class can effect change to private base-class members • Through inherited non-private member functions – private • Alternative to composition • Chapter 17 – protected • Rarely used 2003 Prentice Hall, Inc. All rights reserved.
5 9. 1 Introduction • Abstraction – Focus on commonalities among objects in system • “is-a” vs. “has-a” – “is-a” • Inheritance • Derived class object treated as base class object • Example: Car is a vehicle – Vehicle properties/behaviors also car properties/behaviors – “has-a” • Composition • Object contains one or more objects of other classes as members • Example: Car has a steering wheel 2003 Prentice Hall, Inc. All rights reserved.
6 9. 2 Base Classes and Derived Classes • Base classes and derived classes – Object of one class “is an” object of another class • Example: Rectangle is quadrilateral. – Class Rectangle inherits from class Quadrilateral – Quadrilateral: base class – Rectangle: derived class – Base class typically represents larger set of objects than derived classes • Example: – Base class: Vehicle • Cars, trucks, boats, bicycles, … – Derived class: Car • Smaller, more-specific subset of vehicles 2003 Prentice Hall, Inc. All rights reserved.
7 9. 2 Base Classes and Derived Classes • Inheritance examples 2003 Prentice Hall, Inc. All rights reserved.
8 9. 2 Base Classes and Derived Classes • Inheritance hierarchy – Inheritance relationships: tree-like hierarchy structure – Each class becomes • Base class – Supply data/behaviors to other classes OR • Derived class – Inherit data/behaviors from other classes 2003 Prentice Hall, Inc. All rights reserved.
9 Fig. 9. 2 Inheritance hierarchy for university Community. Members. Community. Member Employee Faculty Student Staff Alumnus Single inheritance Teacher Single inheritance Administrator. Teacher Multiple inheritance Administrator 2003 Prentice Hall, Inc. All rights reserved. Single inheritance
10 Fig. 9. 3 Inheritance hierarchy for Shapes. Shape Two. Dimensional. Shape Circle Square Triangle 2003 Prentice Hall, Inc. All rights reserved. Three. Dimensional. Shape Sphere Cube Tetrahedron
11 9. 2 Base Classes and Derived Classes • public inheritance – Specify with: Class Two. Dimensional. Shape : public Shape • Class Two. Dimensional. Shape inherits from class Shape – Base class private members • Not accessible directly • Still inherited – Manipulate through inherited member functions – Base class public and protected members • Inherited with original member access – friend functions • Not inherited 2003 Prentice Hall, Inc. All rights reserved.
12 9. 3 protected Members • protected access – Intermediate level of protection between public and private – protected members accessible to • • Base class members Base class friends Derived class members Derived class friends – Derived-class members • Refer to public and protected members of base class – Simply use member names 2003 Prentice Hall, Inc. All rights reserved.
9. 4 Relationship between Base Classes and Derived Classes • Base class and derived class relationship – Example: Point/circle inheritance hierarchy • Point – x-y coordinate pair • Circle – x-y coordinate pair – Radius 2003 Prentice Hall, Inc. All rights reserved. 13
1 2 3 4 // Fig. 9. 4: point. h // Point class definition represents an x-y coordinate pair. #ifndef POINT_H #define POINT_H 5 6 class Point { 7 8 9 public: Point( int = 0, int = 0 ); // default constructor 10 11 12 13 14 15 16 17 void set. X( int ); int get. X() const; // set x in coordinate pair // return x from coordinate pair void set. Y( int ); int get. Y() const; // set y in coordinate pair // return y from coordinate pair void print() const; // output Point object 18 19 20 21 private: int x; int y; 22 23 }; // end class Point 24 25 #endif Outline point. h (1 of 1) Maintain x- and y-coordinates as private data members. // x part of coordinate pair // y part of coordinate pair 2003 Prentice Hall, Inc. All rights reserved. 14
1 2 3 // Fig. 9. 5: point. cpp // Point class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "point. h" 8 9 10 11 12 13 // default constructor Point: : Point( int x. Value, int y. Value ) { x = x. Value; y = y. Value; 14 15 } // end Point constructor 16 17 18 19 20 // set x in coordinate pair void Point: : set. X( int x. Value ) { x = x. Value; // no need for validation 21 22 } // end function set. X Outline // Point class definition point. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 15
24 25 26 27 // return x from coordinate pair int Point: : get. X() const { return x; 28 29 } // end function get. X 30 31 32 33 34 // set y in coordinate pair void Point: : set. Y( int y. Value ) { y = y. Value; // no need for validation 35 36 } // end function set. Y 37 38 39 40 41 // return y from coordinate pair int Point: : get. Y() const { return y; 42 43 44 Outline point. cpp (2 of 3) } // end function get. Y 2003 Prentice Hall, Inc. All rights reserved. 16
45 46 47 48 // output Point object void Point: : print() const { cout << '[' << x << ", " << y << ']'; 49 50 } // end function print Outline point. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 17
1 2 3 // Fig. 9. 6: pointtest. cpp // Testing class Point. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "point. h" 9 10 11 12 int main() { Point point( 72, 115 ); Outline // Point class definition Create a Point object. // instantiate Point object 13 14 15 16 // display point coordinates cout << "X coordinate is " << point. get. X() Invoke << "n. Y coordinate is " << point. get. Y(); 17 18 19 point. set. X( 10 ); // set x-coordinate point. set. Y( 10 ); // set y-coordinate 20 21 22 23 24 pointtest. cpp (1 of 2) set functions to modify private data. // display new point value cout << "nn. The new location point. print(); cout << endl; Invoke public function print to display new ofcoordinates. point is " ; 25 2003 Prentice Hall, Inc. All rights reserved. 18
26 27 28 return 0; // indicates successful termination Outline } // end main X coordinate is 72 Y coordinate is 115 The new location of point is [10, 10] pointtest. cpp (2 of 2) pointtest. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 19
1 2 3 4 // Fig. 9. 7: circle. h // Circle class contains x-y coordinate pair and radius. #ifndef CIRCLE_H #define CIRCLE_H 5 6 class Circle { 7 8 public: 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Outline // default constructor Circle( int = 0, double = 0. 0 ); Note code similar to Point x code. in coordinate pair void set. X( int ); int get. X() const; // set // return x from coordinate pair void set. Y( int ); int get. Y() const; // set y in coordinate pair // return y from coordinate pair void set. Radius( double ); double get. Radius() const; // set radius // return radius double get. Diameter() const; double get. Circumference() const; double get. Area() const; circle. h (1 of 2) // return diameter // return circumference // return area 25 2003 Prentice Hall, Inc. All rights reserved. 20
26 27 28 29 30 31 void print() const; private: int x; int y; double radius; // output Circle object Maintain x-y coordinates and radius as private data x-coordinate of Circle's center members. Outline // // y-coordinate of Circle's center // Circle's radius 32 33 }; // end class Circle 34 35 #endif Note code similar to Point code. circle. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 21
1 2 3 // Fig. 9. 8: circle. cpp // Circle class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "circle. h" 8 9 10 11 12 13 14 // default constructor Circle: : Circle( int x. Value, int y. Value, double radius. Value ) { x = x. Value; y = y. Value; set. Radius( radius. Value ); 15 16 } // end Circle constructor 17 18 19 20 21 // set x in coordinate pair void Circle: : set. X( int x. Value ) { x = x. Value; // no need for validation 22 23 } // end function set. X Outline // Circle class definition circle. cpp (1 of 4) 24 2003 Prentice Hall, Inc. All rights reserved. 22
25 26 27 28 // return x from coordinate pair int Circle: : get. X() const { return x; 29 30 } // end function get. X 31 32 33 34 35 // set y in coordinate pair void Circle: : set. Y( int y. Value ) { y = y. Value; // no need for validation 36 37 } // end function set. Y 38 39 40 41 42 // return y from coordinate pair int Circle: : get. Y() const { return y; 43 44 45 Outline circle. cpp (2 of 4) } // end function get. Y 2003 Prentice Hall, Inc. All rights reserved. 23
46 47 48 49 // set radius void Circle: : set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); 50 51 } // end function set. Radius 52 53 54 55 56 // return radius double Circle: : get. Radius() const { return radius; 57 58 } // end function get. Radius 59 60 61 62 63 // calculate and return diameter double Circle: : get. Diameter() const { return 2 * radius; 64 65 } // end function get. Diameter Outline Ensure non-negative value for radius. circle. cpp (3 of 4) 66 2003 Prentice Hall, Inc. All rights reserved. 24
67 68 69 70 // calculate and return circumference double Circle: : get. Circumference() const { return 3. 14159 * get. Diameter(); 71 72 } // end function get. Circumference 73 74 75 76 77 // calculate and return area double Circle: : get. Area() const { return 3. 14159 * radius; 78 79 } // end function get. Area 80 81 82 83 84 85 // output Circle object void Circle: : print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; 86 87 } // end function print Outline circle. cpp (4 of 4) 2003 Prentice Hall, Inc. All rights reserved. 25
1 2 3 // Fig. 9. 9: circletest. cpp // Testing class Circle. #include <iostream> 4 5 6 7 using std: : cout; using std: : endl; using std: : fixed; 8 9 #include <iomanip> 10 11 using std: : setprecision; 12 13 #include "circle. h" 14 15 16 17 int main() { Circle circle( 37, 43, 2. 5 ); 18 19 20 21 22 Outline // Circle class definition Create Circle object. circletest. cpp (1 of 2) // instantiate Circle object // display point coordinates cout << "X coordinate is " << circle. get. X() << "n. Y coordinate is " << circle. get. Y() << "n. Radius is " << circle. get. Radius(); 23 2003 Prentice Hall, Inc. All rights reserved. 26
24 25 26 circle. set. X( 2 ); circle. set. Y( 2 ); circle. set. Radius( 4. 25 ); 27 28 29 30 Use set functions to modify // display new point value cout << "nn. The new location and radius of circle private data. aren" ; circle. print(); 31 32 33 // display floating-point values cout << fixed << setprecision( 2 34 35 36 // display Circle's diameter cout << "n. Diameter is " << circle. get. Diameter(); 37 38 39 // display Circle's circumference cout << "n. Circumference is " << circle. get. Circumference(); 40 41 42 // display Circle's area cout << "n. Area is " << circle. get. Area(); 43 44 cout << endl; 45 46 47 48 return 0; // set new x-coordinate // set new y-coordinate // set new radius Outline Invoke public function print to display new with 2 digits of precision ); coordinates. circletest. cpp (2 of 2) // indicates successful termination } // end main 2003 Prentice Hall, Inc. All rights reserved. 27
X coordinate is 37 Y coordinate is 43 Radius is 2. 5 Outline The new location and radius of circle are Center = [2, 2]; Radius = 4. 25 Diameter is 8. 50 Circumference is 26. 70 Area is 56. 74 circletest. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 28
1 2 3 4 // Fig. 9. 10: circle 2. h // Circle 2 class contains x-y coordinate pair and radius. #ifndef CIRCLE 2_H #define CIRCLE 2_H 5 6 #include "point. h" 7 8 class Circle 2 : public Point { 9 10 public: Class Circle 2 inherits from class Point. // Point class definition 11 12 13 Keyword indicates Colon indicatespublic inheritance. // default constructor type=of 0. 0 inheritance. Circle 2( int = 0, double ); 14 15 16 void set. Radius( double ); double get. Radius() const; 17 18 19 20 double get. Diameter() const; double get. Circumference() const; double get. Area() const; 21 22 void print() const; 23 24 25 private: double radius; Outline circle 2. h (1 of 2) // set radius // return diameter // return circumference // return area Maintain private data // output Circle 2 object member radius. // Circle 2's radius 2003 Prentice Hall, Inc. All rights reserved. 29
26 27 }; // end class Circle 2 28 29 #endif 1 2 3 // Fig. 9. 11: circle 2. cpp // Circle 2 class member-function definitions. #include <iostream> 4 5 using std: : cout; Outline circle 2. h (2 of 2) 6 7 #include "circle 2. h" 8 9 10 11 12 13 14 // default constructor Attempting to access base Circle 2: : Circle 2( int x. Value, int y. Value, double radius. Value ) class Point’s private data { members x and y results in x = x. Value; syntax errors. y = y. Value; set. Radius( radius. Value ); 15 16 } // end Circle 2 constructor // Circle 2 class definition circle 2. cpp (1 of 3) 17 2003 Prentice Hall, Inc. All rights reserved. 30
18 19 20 21 // set radius void Circle 2: : set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); 22 23 } // end function set. Radius 24 25 26 27 28 // return radius double Circle 2: : get. Radius() const { return radius; 29 30 } // end function get. Radius 31 32 33 34 35 // calculate and return diameter double Circle 2: : get. Diameter() const { return 2 * radius; 36 37 } // end function get. Diameter Outline circle 2. cpp (2 of 3) 38 2003 Prentice Hall, Inc. All rights reserved. 31
39 40 41 42 // calculate and return circumference double Circle 2: : get. Circumference() const { return 3. 14159 * get. Diameter(); 43 44 } // end function get. Circumference 45 46 47 48 49 // calculate and return area double Circle 2: : get. Area() const { return 3. 14159 * radius; 50 51 } // end function get. Area 52 53 54 55 56 57 // output Circle 2 object void Circle 2: : print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; 58 59 } // end function print Outline Attempting to access base circle 2. cpp (3 of 3) class Point’s private data members x and y results in syntax errors. 2003 Prentice Hall, Inc. All rights reserved. 32
C: cpphtp 4examplesch 09 Circle. Testcircle 2. cpp(12) : error C 2248: 'x' : cannot access private member declared in class 'Point' C: cpphtp 4examplesch 09 circletestpoint. h(20) : see declaration of 'x' Outline C: cpphtp 4examplesch 09 Circle. Testcircle 2. cpp(13) : error C 2248: 'y' : cannot access private member declared in class 'Point' C: cpphtp 4examplesch 09 circletestpoint. h(21) : see declaration of 'y' C: cpphtp 4examplesch 09 Circle. Testcircle 2. cpp(56) : error C 2248: 'x' : cannot access private member declared in class 'Point' C: cpphtp 4examplesch 09 circletestpoint. h(20) : see declaration of 'x' circle 2. cpp output (1 of 1) C: cpphtp 4examplesch 09 Circle. Testcircle 2. cpp(56) : error C 2248: 'y' : cannot access private member declared in class 'Point' C: cpphtp 4examplesch 09 circletestpoint. h(21) : Attempting to access base see declaration of 'y' class Point’s private data members x and y results in syntax errors. 2003 Prentice Hall, Inc. All rights reserved. 33
1 2 3 4 // Fig. 9. 12: point 2. h // Point 2 class definition represents an x-y coordinate pair. #ifndef POINT 2_H #define POINT 2_H 5 6 class Point 2 { 7 8 9 public: Point 2( int = 0, int = 0 ); // default constructor 10 11 12 13 14 15 16 17 void set. X( int ); int get. X() const; // set x in coordinate pair // return x from coordinate pair void set. Y( int ); int get. Y() const; // set y in coordinate pair // return y from coordinate pair void print() const; // Outline point 2. h (1 of 1) Maintain x- and y-coordinates output Point 2 object as protected data, accessible to derived classes. 18 19 20 21 protected: int x; // x part of coordinate pair int y; // y part of coordinate pair 22 23 }; // end class Point 2 24 25 #endif 2003 Prentice Hall, Inc. All rights reserved. 34
1 2 3 // Fig. 9. 13: point 2. cpp // Point 2 class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "point 2. h" 8 9 10 11 12 13 // default constructor Point 2: : Point 2( int x. Value, int y. Value ) { x = x. Value; y = y. Value; 14 15 } // end Point 2 constructor 16 17 18 19 20 // set x in coordinate pair void Point 2: : set. X( int x. Value ) { x = x. Value; // no need for validation 21 22 } // end function set. X Outline // Point 2 class definition point 2. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 35
24 25 26 27 // return x from coordinate pair int Point 2: : get. X() const { return x; 28 29 } // end function get. X 30 31 32 33 34 // set y in coordinate pair void Point 2: : set. Y( int y. Value ) { y = y. Value; // no need for validation 35 36 } // end function set. Y 37 38 39 40 41 // return y from coordinate pair int Point 2: : get. Y() const { return y; 42 43 44 Outline point 2. cpp (2 of 3) } // end function get. Y 2003 Prentice Hall, Inc. All rights reserved. 36
45 46 47 48 // output Point 2 object void Point 2: : print() const { cout << '[' << x << ", " << y << ']'; 49 50 } // end function print Outline point 2. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 37
1 2 3 4 // Fig. 9. 14: circle 3. h // Circle 3 class contains x-y coordinate pair and radius. #ifndef CIRCLE 3_H #define CIRCLE 3_H 5 6 #include "point 2. h" 7 8 class Circle 3 : public Point 2 { 9 10 public: Class Circle 3 inherits from class Point 2. // Point 2 class definition 11 12 13 // default constructor Circle 3( int = 0, double = 0. 0 ); 14 15 16 void set. Radius( double ); double get. Radius() const; 17 18 19 20 double get. Diameter() const; double get. Circumference() const; double get. Area() const; 21 22 void print() const; 23 24 25 private: double radius; Outline circle 3. h (1 of 2) // set radius // return diameter // return circumference // return area Maintain private data // output Circle 3 object member radius. // Circle 3's radius 2003 Prentice Hall, Inc. All rights reserved. 38
26 27 }; // end class Circle 3 28 29 #endif Outline circle 3. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 39
1 2 3 // Fig. 9. 15: circle 3. cpp // Circle 3 class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "circle 3. h" 8 9 10 11 12 13 14 // default constructor Circle 3: : Circle 3( int x. Value, { x = x. Value; y = y. Value; set. Radius( radius. Value ); 15 16 } // end Circle 3 constructor 17 18 19 20 21 // set radius void Circle 3: : set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); 22 23 } // end function set. Radius Outline // Circle 3 class definition Constructor first implicitly Modify data calls base inherited class’s default int y. Value, x double radius. Value members and y, declared constructor. protected in base class Point 2. ) circle 3. cpp (1 of 3) 24 2003 Prentice Hall, Inc. All rights reserved. 40
25 26 27 28 // return radius double Circle 3: : get. Radius() const { return radius; 29 30 } // end function get. Radius 31 32 33 34 35 // calculate and return diameter double Circle 3: : get. Diameter() const { return 2 * radius; 36 37 } // end function get. Diameter 38 39 40 41 42 // calculate and return circumference double Circle 3: : get. Circumference() const { return 3. 14159 * get. Diameter(); 43 44 } // end function get. Circumference Outline circle 3. cpp (2 of 3) 45 2003 Prentice Hall, Inc. All rights reserved. 41
46 47 48 49 // calculate and return area double Circle 3: : get. Area() const { return 3. 14159 * radius; 50 51 } // end function get. Area 52 53 54 55 56 57 // output Circle 3 object void Circle 3: : print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; 58 59 } // end function print Outline Access inherited data members x and y, declared protected in base class Point 2. circle 3. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 42
1 2 3 // Fig. 9. 16: circletest 3. cpp // Testing class Circle 3. #include <iostream> 4 5 6 7 using std: : cout; using std: : endl; using std: : fixed; 8 9 #include <iomanip> 10 11 using std: : setprecision; 12 13 #include "circle 3. h" 14 15 16 17 int main() { Circle 3 circle( 37, 43, 2. 5 ); // instantiate Circle 3 object 18 19 20 21 22 // Circle 3 class definition Create // display point coordinates cout << "X coordinate is " << circle. get. X() << "n. Y coordinate is " << circle. get. Y() << "n. Radius is " << circle. get. Radius(); Outline Circle 3 object. circletest 3. cpp (1 of 2) Use inherited get functions to access inherited protected data x and y. get function to Use Circle 3 access private data radius. 23 2003 Prentice Hall, Inc. All rights reserved. 43
24 25 26 circle. set. X( 2 ); circle. set. Y( 2 ); circle. set. Radius( 4. 25 ); 27 28 29 30 // display new point value cout << "nn. The new location and radius circle. print(); 31 32 33 // display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 ); 34 35 36 // display Circle 3's diameter cout << "n. Diameter is " << circle. get. Diameter(); 37 38 39 // display Circle 3's circumference cout << "n. Circumference is " << circle. get. Circumference(); 40 41 42 // display Circle 3's area cout << "n. Area is " << circle. get. Area(); 43 44 cout << endl; 45 46 47 48 return 0; // set new x-coordinate // set new y-coordinate // set new radius Use inherited Outline set functions to modify inherited Use Circle 3 data set x function protected and y. to of circle aren" ; data modify private radius. circletest 3. cpp (2 of 2) // indicates successful termination } // end main 2003 Prentice Hall, Inc. All rights reserved. 44
X coordinate is 37 Y coordinate is 43 Radius is 2. 5 Outline The new location and radius of circle are Center = [2, 2]; Radius = 4. 25 Diameter is 8. 50 Circumference is 26. 70 Area is 56. 74 circletest 3. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 45
9. 4 Relationship between Base Classes and Derived Classes • Using protected data members – Advantages • Derived classes can modify values directly • Slight increase in performance – Avoid set/get function call overhead – Disadvantages • No validity checking – Derived class can assign illegal value • Implementation dependent – Derived class member functions more likely dependent on base class implementation – Base class implementation changes may result in derived class modifications • Fragile (brittle) software 2003 Prentice Hall, Inc. All rights reserved. 46
1 2 3 4 // Fig. 9. 17: point 3. h // Point 3 class definition represents an x-y coordinate pair. #ifndef POINT 3_H #define POINT 3_H 5 6 class Point 3 { 7 8 9 public: Point 3( int = 0, int = 0 ); // default constructor 10 11 12 13 14 15 16 17 void set. X( int ); int get. X() const; // set x in coordinate pair // return x from coordinate pair void set. Y( int ); int get. Y() const; // set y in coordinate pair // return y from coordinate pair void print() const; // output Point 3 object practice: private over 18 19 20 21 private: int x; int y; 22 23 }; // end class Point 3 24 25 #endif Outline point 3. h (1 of 1) Better software-engineering protected when possible. // x part of coordinate pair // y part of coordinate pair 2003 Prentice Hall, Inc. All rights reserved. 47
1 2 3 // Fig. 9. 18: point 3. cpp // Point 3 class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "point 3. h" 8 9 10 11 12 13 // default constructor Point 3: : Point 3( int x. Value, int y. Value ) : x( x. Value ), y( y. Value ) { // empty body 14 15 } // end Point 3 constructor 16 17 18 19 20 // set x in coordinate pair void Point 3: : set. X( int x. Value ) { x = x. Value; // no need for validation 21 22 } // end function set. X Outline // Point 3 class definition Member initializers specify values of x and y. point 3. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 48
24 25 26 27 // return x from coordinate pair int Point 3: : get. X() const { return x; 28 29 } // end function get. X 30 31 32 33 34 // set y in coordinate pair void Point 3: : set. Y( int y. Value ) { y = y. Value; // no need for validation 35 36 } // end function set. Y 37 38 39 40 41 // return y from coordinate pair int Point 3: : get. Y() const { return y; 42 43 44 Outline point 3. cpp (2 of 3) } // end function get. Y 2003 Prentice Hall, Inc. All rights reserved. 49
45 46 47 48 // output Point 3 object void Point 3: : print() const { cout << '[' << get. X() << ", " << get. Y() << ']'; 49 50 } // end function print Outline Invoke non-private member functions to access private data. point 3. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 50
1 2 3 4 // Fig. 9. 19: circle 4. h // Circle 4 class contains x-y coordinate pair and radius. #ifndef CIRCLE 4_H #define CIRCLE 4_H 5 6 #include "point 3. h" 7 8 class Circle 4 : public Point 3 { 9 10 public: // Point 3 Class Circle 4 inherits from class Point 3. class definition 11 12 13 // default constructor Circle 4( int = 0, double = 0. 0 ); 14 15 16 void set. Radius( double ); double get. Radius() const; 17 18 19 20 double get. Diameter() const; double get. Circumference() const; double get. Area() const; 21 22 void print() const; 23 24 25 private: double radius; Outline circle 4. h (1 of 2) // set radius // return diameter // return circumference // return area Maintain private data // output Circle 4 object member radius. // Circle 4's radius 2003 Prentice Hall, Inc. All rights reserved. 51
26 27 }; // end class Circle 4 28 29 #endif Outline circle 4. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 52
1 2 3 // Fig. 9. 20: circle 4. cpp // Circle 4 class member-function definitions. #include <iostream> 4 5 using std: : cout; 6 7 #include "circle 4. h" 8 9 10 11 12 13 // default constructor Circle 4: : Circle 4( int x. Value, int : Point 3( x. Value, y. Value ) // call base-class constructor { set. Radius( radius. Value ); 14 15 } // end Circle 4 constructor 16 17 18 19 20 // set radius void Circle 4: : set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); 21 22 } // end function set. Radius Outline // Circle 4 class definition Base-class initializer syntax passes arguments to base class Point 3. y. Value, double radius. Value ) circle 4. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 53
24 25 26 27 // return radius double Circle 4: : get. Radius() const { return radius; 28 29 } // end function get. Radius 30 31 32 33 34 // calculate and return diameter double Circle 4: : get. Diameter() const { return 2 * get. Radius(); 35 36 } // end function get. Diameter 37 38 39 40 41 // calculate and return circumference double Circle 4: : get. Circumference() const { return 3. 14159 * get. Diameter(); 42 43 } // end function get. Circumference Outline Invoke function get. Radius rather than directly accessing data member radius. circle 4. cpp (2 of 3) 44 2003 Prentice Hall, Inc. All rights reserved. 54
45 46 47 48 // calculate and return area double Circle 4: : get. Area() const { return 3. 14159 * get. Radius(); Outline Redefine class Point 3’s member function print. Invoke function get. Radius rather than directly accessing Invoke base-class Point 3’s datafunction memberusing radius. print binary scope-resolution operator Point 3's (: : ). print function 49 50 } // end function get. Area 51 52 53 54 55 56 57 // output Circle 4 object void Circle 4: : print() const { cout << "Center = "; Point 3: : print(); // invoke cout << "; Radius = " << get. Radius(); 58 59 } // end function print circle 4. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 55
1 2 3 // Fig. 9. 21: circletest 4. cpp // Testing class Circle 4. #include <iostream> 4 5 6 7 using std: : cout; using std: : endl; using std: : fixed; 8 9 #include <iomanip> 10 11 using std: : setprecision; 12 13 #include "circle 4. h" 14 15 16 17 int main() { Circle 4 circle( 37, 43, 2. 5 ); // instantiate Circle 4 object 18 19 20 21 22 Outline // Circle 4 class definition Create Circle 4 object. // display point coordinates cout << "X coordinate is " << circle. get. X() << "n. Y coordinate is " << circle. get. Y() << "n. Radius is " << circle. get. Radius(); circletest 4. cpp (1 of 2) Use inherited get functions to access inherited protected data x and y. get function to Use Circle 3 access private data radius. 23 2003 Prentice Hall, Inc. All rights reserved. 56
24 25 26 circle. set. X( 2 ); circle. set. Y( 2 ); circle. set. Radius( 4. 25 ); 27 28 29 30 // display new circle value cout << "nn. The new location and radius circle. print(); 31 32 33 // display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 ); 34 35 36 // display Circle 4's diameter cout << "n. Diameter is " << circle. get. Diameter(); 37 38 39 // display Circle 4's circumference cout << "n. Circumference is " << circle. get. Circumference(); 40 41 42 // display Circle 4's area cout << "n. Area is " << circle. get. Area(); 43 44 cout << endl; 45 46 47 48 return 0; // set new x-coordinate // set new y-coordinate // set new radius Use inherited Outline set functions to modify inherited Use Circle 3 data set x function protected and y. to of circle aren" ; data modify private radius. circletest 4. cpp (2 of 2) // indicates successful termination } // end main 2003 Prentice Hall, Inc. All rights reserved. 57
X coordinate is 37 Y coordinate is 43 Radius is 2. 5 Outline The new location and radius of circle are Center = [2, 2]; Radius = 4. 25 Diameter is 8. 50 Circumference is 26. 70 Area is 56. 74 circletest 4. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 58
9. 5 Case Study: Three-Level Inheritance Hierarchy • Three level point/circle/cylinder hierarchy – Point • x-y coordinate pair – Circle • x-y coordinate pair • Radius – Cylinder • x-y coordinate pair • Radius • Height 2003 Prentice Hall, Inc. All rights reserved. 59
1 2 3 4 // Fig. 9. 22: cylinder. h // Cylinder class inherits from class Circle 4. #ifndef CYLINDER_H #define CYLINDER_H 5 6 #include "circle 4. h" 7 8 class Cylinder : public Circle 4 { 9 10 public: 11 12 13 14 15 16 17 18 19 20 Outline Class Cylinder inherits from class Circle 4. // Circle 4 class definition // default constructor Cylinder( int = 0, double = 0. 0 ); void set. Height( double ); double get. Height() const; // set Cylinder's height // return Cylinder's height double get. Area() const; double get. Volume() const; void print() const; // return Cylinder's area // return Cylinder's volume // output Cylinder Maintain private data cylinder. h (1 of 2) member height. 21 22 23 private: double height; 24 25 }; // end class Cylinder // Cylinder's height 2003 Prentice Hall, Inc. All rights reserved. 60
26 27 #endif 1 2 3 // Fig. 9. 23: cylinder. cpp // Cylinder class inherits from class Circle 4. #include <iostream> 4 5 using std: : cout; 6 7 #include "cylinder. h" 8 9 10 11 12 13 14 // default constructor to base Cylinder: : Cylinder( int x. Value, int y. Value, passes doublearguments radius. Value, Circle 4. double height. Value ) : Circle 4( x. Value, y. Value, radius. Value ) { set. Height( height. Value ); 15 16 } // end Cylinder constructor Outline // Cylinder class definition Base-class initializer syntax class cylinder. h (2 of 2) cylinder. cpp (1 of 3) 17 2003 Prentice Hall, Inc. All rights reserved. 61
18 19 20 21 // set Cylinder's height void Cylinder: : set. Height( double height. Value ) { height = ( height. Value < 0. 0 ? 0. 0 : height. Value ); 22 23 } // end function set. Height 24 25 26 27 28 // get Cylinder's height double Cylinder: : get. Height() const { return height; 29 30 } // end function get. Height 31 32 33 34 35 36 // redefine Circle 4 function get. Area to double Cylinder: : get. Area() const { return 2 * Circle 4: : get. Area() + get. Circumference() * get. Height(); 37 38 } // end function get. Area Outline cylinder. cpp Redefine base class (2 of 3) Circle 4’s member function Invoke base-class Circle 4’s calculate Cylinder area get. Area to return get. Area function using Cylinder surface area. binary scope-resolution operator (: : ). 39 2003 Prentice Hall, Inc. All rights reserved. 62
40 41 42 43 // calculate Cylinder volume double Cylinder: : get. Volume() const { return Circle 4: : get. Area() * get. Height(); 44 45 } // end function get. Volume 46 47 48 49 50 51 // output Cylinder object void Cylinder: : print() const { Circle 4: : print(); cout << "; Height = " << get. Height(); 52 53 } // end function print Invoke base-class Circle 4’s get. Area function using binary scope-resolution operator (: : ). Outline Redefine class Circle 4’s member. Circle 4’s function print. Invoke base-class print function using binary scope-resolution operator (: : ). cylinder. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 63
1 2 3 // Fig. 9. 24: cylindertest. cpp // Testing class Cylinder. #include <iostream> 4 5 6 7 using std: : cout; using std: : endl; using std: : fixed; 8 9 #include <iomanip> 10 11 using std: : setprecision; 12 13 #include "cylinder. h" 14 15 16 17 18 int main() { // instantiate Cylinder object Cylinder cylinder( 12, 23, 2. 5, 5. 7 ); 19 20 21 22 23 24 // Cylinder class definition // display point coordinates cout << "X coordinate is " << cylinder. get. X() << "n. Y coordinate is " << cylinder. get. Y() << "n. Radius is " << cylinder. get. Radius() << "n. Height is " << cylinder. get. Height(); Outline cylindertest. cpp (1 of 3) Invoke indirectly inherited Point 3 member functions. Invoke directly inherited Invoke Cylinder member Circle 4 member function. 25 2003 Prentice Hall, Inc. All rights reserved. 64
26 27 28 29 cylinder. set. X( 2 ); cylinder. set. Y( 2 ); cylinder. set. Radius( 4. 25 ); cylinder. set. Height( 10 ); 30 31 32 33 // display new cylinder value function. cout << "nn. The new location and radius of circle aren" ; cylinder. print(); 34 35 36 // display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 function. ); 37 38 39 // display cylinder's diameter cout << "nn. Diameter is " << cylinder. get. Diameter(); 40 41 42 43 // display cylinder's circumference cout << "n. Circumference is " << cylinder. get. Circumference(); 44 45 46 // display cylinder's area cout << "n. Area is " << cylinder. get. Area(); 47 48 49 // display cylinder's volume cout << "n. Volume is " << cylinder. get. Volume(); // // set set new new x-coordinate y-coordinate Invoke indirectly inherited radius Point 3 member functions. height Invoke directly inherited Outline Circle 4 member function. Invoke Cylinder member Invoke redefined print cylindertest. cpp (2 of 3) Invoke redefined get. Area function. 50 2003 Prentice Hall, Inc. All rights reserved. 65
51 52 53 54 55 cout << endl; return 0; Outline // indicates successful termination } // end main X coordinate is 12 Y coordinate is 23 Radius is 2. 5 Height is 5. 7 The new location and radius of circle are Center = [2, 2]; Radius = 4. 25; Height = 10 Diameter is 8. 50 Circumference is 26. 70 Area is 380. 53 Volume is 567. 45 cylindertest. cpp (3 of 3) cylindertest. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 66
9. 6 Constructors and Destructors in Derived Classes • Instantiating derived-class object – Chain of constructor calls • Derived-class constructor invokes base class constructor – Implicitly or explicitly • Base of inheritance hierarchy – Last constructor called in chain – First constructor body to finish executing – Example: Point 3/Circle 4/Cylinder hierarchy • Point 3 constructor called last • Point 3 constructor body finishes execution first • Initializing data members – Each base-class constructor initializes data members • Inherited by derived class 2003 Prentice Hall, Inc. All rights reserved. 67
9. 6 Constructors and Destructors in Derived Classes • Destroying derived-class object – Chain of destructor calls • Reverse order of constructor chain • Destructor of derived-class called first • Destructor of next base class up hierarchy next – Continue up hierarchy until final base reached • After final base-class destructor, object removed from memory 2003 Prentice Hall, Inc. All rights reserved. 68
9. 6 Constructors and Destructors in Derived Classes • Base-class constructors, destructors, assignment operators – Not inherited by derived classes – Derived class constructors, assignment operators can call • Constructors • Assignment operators 2003 Prentice Hall, Inc. All rights reserved. 69
1 2 3 4 // Fig. 9. 25: point 4. h // Point 4 class definition represents an x-y coordinate pair. #ifndef POINT 4_H #define POINT 4_H 5 6 class Point 4 { 7 8 9 10 public: Point 4( int = 0, int = 0 ); // default constructor ~Point 4(); // destructor 11 12 13 14 15 16 17 18 void set. X( int ); int get. X() const; // set x in coordinate pair // return x from coordinate pair void set. Y( int ); int get. Y() const; // set y in coordinate pair // return y from coordinate pair void print() const; // output Point 3 object 19 20 21 22 private: int x; int y; 23 24 }; // end class Point 4 25 26 #endif Outline Constructor and destructor output messages to demonstrate function call order. point 4. h (1 of 1) // x part of coordinate pair // y part of coordinate pair 2003 Prentice Hall, Inc. All rights reserved. 70
1 2 3 // Fig. 9. 26: point 4. cpp // Point 4 class member-function definitions. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "point 4. h" 9 10 11 12 13 14 15 16 // default constructor Point 4: : Point 4( int x. Value, int y. Value ) : x( x. Value ), y( y. Value ) { cout << "Point 4 constructor: "; print(); cout << endl; 17 18 } // end Point 4 constructor 19 20 21 22 23 24 25 // destructor Point 4: : ~Point 4() { cout << "Point 4 destructor: "; print(); cout << endl; Outline // Point 4 class definition Output message to demonstrate constructor function call order. point 4. cpp (1 of 3) Output message to demonstrate destructor function call order. 2003 Prentice Hall, Inc. All rights reserved. 71
26 27 } // end Point 4 destructor 28 29 30 31 32 // set x in coordinate pair void Point 4: : set. X( int x. Value ) { x = x. Value; // no need for validation 33 34 } // end function set. X 35 36 37 38 39 // return x from coordinate pair int Point 4: : get. X() const { return x; 40 41 } // end function get. X 42 43 44 45 46 // set y in coordinate pair void Point 4: : set. Y( int y. Value ) { y = y. Value; // no need for validation 47 48 } // end function set. Y Outline point 4. cpp (2 of 3) 49 2003 Prentice Hall, Inc. All rights reserved. 72
50 51 52 53 54 55 56 57 58 59 60 61 62 // return y from coordinate pair int Point 4: : get. Y() const { return y; Outline } // end function get. Y // output Point 4 object void Point 4: : print() const { cout << '[' << get. X() << ", " << get. Y() << ']'; } // end function print point 4. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 73
1 2 3 4 // Fig. 9. 27: circle 5. h // Circle 5 class contains x-y coordinate pair and radius. #ifndef CIRCLE 5_H #define CIRCLE 5_H 5 6 #include "point 4. h" 7 8 class Circle 5 : public Point 4 { 9 10 public: Outline // Point 4 class definition 11 12 13 // default constructor Circle 5( int = 0, double = 0. 0 ); 14 15 16 17 ~Circle 5(); void set. Radius( double ); double get. Radius() const; 18 19 20 21 double get. Diameter() const; double get. Circumference() const; double get. Area() const; 22 23 void print() const; Constructor and destructor output messages to demonstrate function call order. circle 5. h (1 of 2) // destructor // set radius // return diameter // return circumference // return area // output Circle 5 object 24 2003 Prentice Hall, Inc. All rights reserved. 74
25 26 private: double radius; 27 28 }; // end class Circle 5 29 30 #endif // Circle 5's radius Outline circle 5. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 75
1 2 3 // Fig. 9. 28: circle 5. cpp // Circle 5 class member-function definitions. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include "circle 5. h" 9 10 11 12 13 14 // default constructor Circle 5: : Circle 5( int x. Value, int y. Value, double radius. Value ) : Point 4( x. Value, y. Value ) // call base-class Output constructor message to { demonstrate constructor set. Radius( radius. Value ); 15 16 17 18 19 20 Outline // Circle 5 class definition function call order. circle 5. cpp (1 of 4) cout << "Circle 5 constructor: "; print(); cout << endl; } // end Circle 5 constructor 21 2003 Prentice Hall, Inc. All rights reserved. 76
22 23 24 25 26 27 28 29 // destructor Circle 5: : ~Circle 5() { cout << "Circle 5 destructor: "; print(); cout << endl; } // end Circle 5 destructor Outline Output message to demonstrate destructor function call order. 30 31 32 33 34 // set radius void Circle 5: : set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); 35 36 } // end function set. Radius 37 38 39 40 41 // return radius double Circle 5: : get. Radius() const { return radius; 42 43 } // end function get. Radius circle 5. cpp (2 of 4) 44 2003 Prentice Hall, Inc. All rights reserved. 77
45 46 47 48 // calculate and return diameter double Circle 5: : get. Diameter() const { return 2 * get. Radius(); 49 50 } // end function get. Diameter 51 52 53 54 55 // calculate and return circumference double Circle 5: : get. Circumference() const { return 3. 14159 * get. Diameter(); 56 57 } // end function get. Circumference 58 59 60 61 62 // calculate and return area double Circle 5: : get. Area() const { return 3. 14159 * get. Radius(); 63 64 } // end function get. Area Outline circle 5. cpp (3 of 4) 65 2003 Prentice Hall, Inc. All rights reserved. 78
66 67 68 69 70 71 // output Circle 5 object void Circle 5: : print() const { cout << "Center = "; Point 4: : print(); // invoke Point 4's print function cout << "; Radius = " << get. Radius(); 72 73 } // end function print Outline circle 5. cpp (4 of 4) 2003 Prentice Hall, Inc. All rights reserved. 79
1 2 3 4 // Fig. 9. 29: fig 09_29. cpp // Display order in which base-class and derived-class // constructors are called. #include <iostream> 5 6 7 using std: : cout; using std: : endl; 8 9 #include "circle 5. h" 10 11 12 13 14 15 16 17 Outline // Circle 5 class definition int main() { { // begin new scope Point 4 object goes in and out of scope immediately. fig 09_29. cpp (1 of 2) Point 4 point( 11, 22 ); } // end scope 18 19 20 cout << endl; Circle 5 circle 1( 72, 29, 4. 5 ); 21 22 23 cout << endl; Circle 5 circle 2( 5, 5, 10 ); 24 25 cout << endl; Instantiate two Circle 5 objects to demonstrate order of derived-class and baseclass constructor/destructor function calls. 2003 Prentice Hall, Inc. All rights reserved. 80
26 27 28 29 return 0; // indicates successful termination Point 4 constructor called } // end main for object in block; destructor called immediately as Base-class Point 4 constructor: [11, 22] execution leaves scope. Derived-class Circle 5 Point 4 destructor: [11, 22] constructor executes first constructor body executes when instantiating derived. Base-class Point 4 after base-class Point 4’s Point 4 constructor: [72, 29] class Circle 5 object. Derived-class Circle 5 executes first Circle 5 constructor: Center = [72, 29]; Radius =constructor 4. 5 finishes execution. constructor body executes when instantiating derived. Destructors for Circle 5 after Point 4 constructor: [5, 5] class base-class Circle 5 Point 4’s object called in reverse order constructor finishes execution. Circle 5 constructor: Center = [5, 5]; Radius = 10 Destructors for Circle 5 of constructors. Circle 5 destructor: Center = [5, 5]; Radius = 10 object called in reverse order of constructors. Point 4 destructor: [5, 5] Outline fig 09_29. cpp (2 of 2) fig 09_29. cpp output (1 of 1) Circle 5 destructor: Center = [72, 29]; Radius = 4. 5 Point 4 destructor: [72, 29] 2003 Prentice Hall, Inc. All rights reserved. 81
82 9. 7 “Uses A” and “Knows A” Relationships • “Uses a” – Object uses another object • Call non-private member function – Using pointer, reference or object name • “Knows a” (association) – Object aware of another object • Contain pointer handle or reference handle – Knowledge networks 2003 Prentice Hall, Inc. All rights reserved.
9. 8 public, protected and private Inheritance 2003 Prentice Hall, Inc. All rights reserved. 83
84 9. 9 Software Engineering with Inheritance • Customizing existing software – Inherit from existing classes • Include additional members • Redefine base-class members • No direct access to base class’s source code – Link to object code – Independent software vendors (ISVs) • Develop proprietary code for sale/license – Available in object-code format • Users derive new classes – Without accessing ISV proprietary source code 2003 Prentice Hall, Inc. All rights reserved.
- Slides: 84