1 Inheritance inheritance a mechanism that build a

1 Inheritance • inheritance – a mechanism that build a new class by deriving from an existing class – derived class (or subclass) inherit from based class (or superclass) – increase code reusability – e. g. • car(轎車) is a class derived from vehicle(交通 具) • graduate_student is a class derived from student • cat is a class derived from animal – syntax class Derived_class : public Base_class {. . . }

2 Inheritance(example) class Pen { public: enum ink {Off, On}; void set_status(ink); void set_location(int, int); private: int x; int y; ink status; }; class CPen : public Pen { public: void set_color(int); private: int color; }; int main( ) { Pen p 1; CPen p 2; . . . p 1. set_location(100, 200); p 2. set_location(200, 300); p 2. set_color(255); . . . }

3 Inheritance Pen: CPen: private: x, y, status, x, y, status public: derive from color set_status() public: set_location() set_status( ) set_location( ) set_color( ) black: inherit from Pen red: new declaration in CPen

4 Inheritance • 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

5 Inheritance hierarchy for Shapes: Shape Two. Dimensional. Shape Circle Square Triangle Three. Dimensional. Shape Sphere Cube Tetrahedron

6 Inheritance hierarchy for university Community. Members: Community. Member Employee Faculty Student Staff Alumnus Single inheritance Teacher Single inheritance Administrator. Teacher Multiple inheritance Administrator Single inheritance

7 Base Classes and Derived Classes • more inheritance examples

8 Inheritance • Three types of inheritance – public (default) • 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 • every member of derived class becomes private – protected • Rarely used

9 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

10 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 – but, inaccessible for others – Derived-class members • Refer to public and protected members of base class, Simply use member names

11 protected members class BC { public: void set_x(int a) { x=a; } protected: int get_x( ) const { return x; } private: int x; } class DC : public BC { public: void add 2( ) { int c=get_x( ); // O. K. here set_x(c+2); } } int main( ) { DC d; d. set_x(3); //O. K. cout << d. get_x( ) << ‘n’; // Error! // it is protected! d. x = 77; // Error! private! d. add 2( ); // O. K. return 0; }

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 12

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 ); // set x in coordinate pair int get. X() const; // return x from coordinate pair void set. Y( int ); // set y in coordinate pair int get. Y() const; // return y from coordinate pair void print() const; // output Point object Maintain 18 19 20 21 private: int x; // x part of coordinate pair int y; // y part of coordinate pair 22 23 }; // end class Point 24 25 #endif Outline point. h (1 of 1) x- and y-coordinates as private data members. 2003 Prentice Hall, Inc. All rights reserved. 13

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" // Point class definition 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. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 14

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 } // end function get. Y Outline point. cpp (2 of 3) 2003 Prentice Hall, Inc. All rights reserved. 15

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. 16

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" // Point class definition 9 10 11 12 int main() { Point point( 72, 115 ); // instantiate Point object Outline Create a 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. Invoke public function // display new point value print to display new cout << "nn. The new location of point is " coordinates. ; point. print(); cout << endl; 25 2003 Prentice Hall, Inc. All rights reserved. 17

26 27 28 return 0; // indicates successful termination } // end main Outline 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. 18

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 // default constructor Circle( int = 0, double = 0. 0 ); 12 13 14 15 16 17 18 19 20 int get. X() const; // return x from coordinate pair void set. Y( int ); // set y in coordinate pair int get. Y() const; // return y from coordinate pair void set. Radius( double ); // set radius double get. Radius() const; // return radius 21 22 23 24 Note code similar to Point void set. X( int ); // set x in coordinate pair code. Outline circle. h (1 of 2) double get. Diameter() const; // return diameter double get. Circumference() const; // return circumference double get. Area() const; // return area 25 2003 Prentice Hall, Inc. All rights reserved. 19

26 void print() const; // output Circle object 27 28 29 30 31 int y; // y-coordinate of Circle's center double radius; // Circle's radius Maintain x-y coordinates and private: radius as private data int x; // x-coordinate of Circle's center members. 32 33 }; // end class Circle 34 35 #endif Outline Note code similar to Point code. circle. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 20

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" // Circle class definition 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. cpp (1 of 4) 24 2003 Prentice Hall, Inc. All rights reserved. 21

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 } // end function get. Y Outline circle. cpp (2 of 4) 2003 Prentice Hall, Inc. All rights reserved. 22

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. 23

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. 24

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" // Circle class definition 14 15 16 17 int main() { Circle circle( 37, 43, 2. 5 ); // instantiate Circle object 18 19 20 21 22 // display point coordinates cout << "X coordinate is " << circle. get. X() << "n. Y coordinate is " << circle. get. Y() << "n. Radius is " << circle. get. Radius(); Outline Create Circle object. circletest. cpp (1 of 2) 23 2003 Prentice Hall, Inc. All rights reserved. 25

24 25 26 circle. set. X( 2 ); // set new x-coordinate circle. set. Y( 2 ); // set new y-coordinate circle. set. Radius( 4. 25 ); // set new radius 27 28 29 30 Use set functions to modify // display new point value cout << "nn. The new location and radius of circle aren" ; private data. circle. print(); 31 32 33 Invoke public function print to display new // display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 ); coordinates. 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; // indicates successful termination } // end main Outline circletest. cpp (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 26

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. 27

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" // Point class definition 7 8 class Circle 2 : public Point { 9 10 public: 11 12 13 Keyword indicates Colon indicatespublic inheritance. // default constructor type of inheritance. Circle 2( int = 0, double = 0. 0 ); 14 15 16 void set. Radius( double ); // set radius double get. Radius() const; // return radius 17 18 19 20 double get. Diameter() const; // return diameter double get. Circumference() const; // return circumference double get. Area() const; // return area 21 22 Maintain private data void print() const; // output Circle 2 object 23 24 25 private: double radius; // Circle 2's radius Outline Class Circle 2 inherits from class Point. circle 2. h (1 of 2) member radius. 2003 Prentice Hall, Inc. All rights reserved. 28

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" // Circle 2 class definition 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. cpp (1 of 3) 17 2003 Prentice Hall, Inc. All rights reserved. 29

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. 30

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. 31

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. 32

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 18 19 20 21 void set. X( int ); // set x in coordinate pair int get. X() const; // return x from coordinate pair void set. Y( int ); // set y in coordinate pair int get. Y() const; // return y from coordinate pair Maintain x- and y-coordinates void print() const; // output Point 2 object as protected data, Outline point 2. h (1 of 1) accessible to derived classes. 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. 33

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" // Point 2 class definition 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. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 34

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 } // end function get. Y Outline point 2. cpp (2 of 3) 2003 Prentice Hall, Inc. All rights reserved. 35

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. 36

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" // Point 2 class definition 7 8 class Circle 3 : public Point 2 { 9 10 public: 11 12 13 // default constructor Circle 3( int = 0, double = 0. 0 ); 14 15 16 void set. Radius( double ); // set radius double get. Radius() const; // return radius 17 18 19 20 double get. Diameter() const; // return diameter double get. Circumference() const; // return circumference double get. Area() const; // return area 21 22 Maintain private data void print() const; // output Circle 3 object 23 24 25 private: double radius; // Circle 3's radius Outline Class Circle 3 inherits from class Point 2. circle 3. h (1 of 2) member radius. 2003 Prentice Hall, Inc. All rights reserved. 37

26 27 }; // end class Circle 3 28 29 #endif Outline circle 3. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 38

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" // Circle 3 class definition 8 9 10 11 12 13 14 Outline Constructor first implicitly Modify data // default constructor calls base inherited class’s default Circle 3: : Circle 3( int x. Value, int y. Value, double radius. Value ) members x and y, declared constructor. { protected in base class x = x. Value; Point 2. 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 circle 3. cpp (1 of 3) 24 2003 Prentice Hall, Inc. All rights reserved. 39

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. 40

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. 41

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 14 15 16 17 18 19 20 21 22 #include "circle 3. h" // Circle 3 class definition Create Outline Circle 3 object. int main() { Circle 3 circle( 37, 43, 2. 5 ); // instantiate Circle 3 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 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. 42

24 25 26 27 28 29 30 31 32 33 circle. set. X( 2 ); // set new x-coordinate circle. set. Y( 2 ); // set new y-coordinate circle. set. Radius( 4. 25 ); // set new radius Use inherited Outline set functions to modify inherited // display new point value Use Circle 3 data set x function protected and y. to cout << "nn. The new location and radius of circle aren" modify private; data circle. print(); radius. // 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; // indicates successful termination } // end main circletest 3. cpp (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 43

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. 44

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 45

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 18 19 20 21 void set. X( int ); // set x in coordinate pair int get. X() const; // return x from coordinate pair void set. Y( int ); // set y in coordinate pair int get. Y() const; // return y from coordinate pair Better software-engineering void print() const; // output Point 3 object practice: private over Outline point 3. h (1 of 1) protected when possible. private: int x; // x part of coordinate pair int y; // y part of coordinate pair 22 23 }; // end class Point 3 24 25 #endif 2003 Prentice Hall, Inc. All rights reserved. 46

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" // Point 3 class definition 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 Member initializers specify values of x and y. point 3. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 47

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 } // end function get. Y Outline point 3. cpp (2 of 3) 2003 Prentice Hall, Inc. All rights reserved. 48

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. 49

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 Class Circle 4 inherits from class Point 3. #include "point 3. h" // Point 3 class definition 7 8 class Circle 4 : public Point 3 { 9 10 public: 11 12 13 // default constructor Circle 4( int = 0, double = 0. 0 ); 14 15 16 void set. Radius( double ); // set radius double get. Radius() const; // return radius 17 18 19 20 double get. Diameter() const; // return diameter double get. Circumference() const; // return circumference double get. Area() const; // return area 21 22 Maintain private data void print() const; // output Circle 4 object 23 24 25 private: double radius; // Circle 4's radius Outline circle 4. h (1 of 2) member radius. 2003 Prentice Hall, Inc. All rights reserved. 50

26 27 }; // end class Circle 4 28 29 #endif Outline circle 4. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 51

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" // Circle 4 class definition 8 9 10 11 12 13 Outline Base-class initializer syntax passes arguments to base class // default constructor Point 3. Circle 4: : Circle 4( int x. Value, int y. Value, double radius. Value ) : 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 circle 4. cpp (1 of 3) 23 2003 Prentice Hall, Inc. All rights reserved. 52

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. 53

45 46 47 48 // calculate and return area double Circle 4: : get. Area() const { return 3. 14159 * get. Radius(); 49 50 Redefine class Point 3’s } // end function get. Area member function print. Invoke function get. Radius // output Circle 4 object rather than directly accessing Invoke base-class Point 3’s void Circle 4: : print() const datafunction memberusing radius. { print binary cout << "Center = "; scope-resolution operator Point 3: : print(); // invoke Point 3's print function (: : ). 51 52 53 54 55 56 57 58 59 cout << "; Radius = " << get. Radius(); Outline circle 4. cpp (3 of 3) } // end function print 2003 Prentice Hall, Inc. All rights reserved. 54

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" // Circle 4 class definition 14 15 16 17 int main() { Circle 4 circle( 37, 43, 2. 5 ); // instantiate Circle 4 object 18 19 20 21 22 // display point coordinates cout << "X coordinate is " << circle. get. X() << "n. Y coordinate is " << circle. get. Y() << "n. Radius is " << circle. get. Radius(); Outline Create Circle 4 object. 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. 55

24 25 26 27 28 29 30 31 32 33 circle. set. X( 2 ); // set new x-coordinate circle. set. Y( 2 ); // set new y-coordinate circle. set. Radius( 4. 25 ); // set new radius Use inherited Outline set functions to modify inherited // display new circle value Use Circle 3 data set x function protected and y. to cout << "nn. The new location and radius of circle aren" modify private; data circle. print(); radius. // 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; // indicates successful termination } // end main circletest 4. cpp (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 56

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. 57

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 58

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" // Circle 4 class definition 7 8 class Cylinder : public Circle 4 { 9 10 public: 11 12 13 14 15 16 // default constructor Cylinder( int = 0, double = 0. 0 ); void set. Height( double ); // set Cylinder's height double get. Height() const; // return Cylinder's height 17 18 19 20 double get. Area() const; // return Cylinder's area double get. Volume() const; // return Cylinder's volume void print() const; // output Cylinder Maintain private data 21 22 23 private: double height; // Cylinder's height 24 25 }; // end class Cylinder Outline Class Cylinder inherits from class Circle 4. cylinder. h (1 of 2) member height. 2003 Prentice Hall, Inc. All rights reserved. 59

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" // Cylinder class definition 8 9 10 11 12 13 14 // default constructor passes arguments to base Cylinder: : Cylinder( int x. Value, int y. Value, double 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 Base-class initializer syntax class cylinder. h (2 of 2) cylinder. cpp (1 of 3) 17 2003 Prentice Hall, Inc. All rights reserved. 60

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 get. Circumference() * get. Height(); 37 38 Outline cylinder. cpp Redefine base class (2 of 3) Circle 4’s member function Invoke base-class Circle 4’s // redefine Circle 4 function get. Area to calculate Cylinder area get. Area to return get. Area function using double Cylinder: : get. Area() const Cylinder surface area. binary scope-resolution { operator (: : ). return 2 * Circle 4: : get. Area() + } // end function get. Area 39 2003 Prentice Hall, Inc. All rights reserved. 61

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. 62

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" // Cylinder class definition 14 15 16 17 18 int main() { // instantiate Cylinder object Cylinder cylinder( 12, 23, 2. 5, 5. 7 ); 19 20 21 22 23 24 // 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. 63

26 27 28 29 cylinder. set. X( 2 ); // set new x-coordinate cylinder. set. Y( 2 ); // set new y-coordinate Invoke indirectly inherited cylinder. set. Radius( 4. 25 ); // set new radius Point 3 member functions. cylinder. set. Height( 10 ); // set new height Invoke directly inherited 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 function. cout << fixed << setprecision( 2 ); 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(); 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. 64

51 cout << endl; 52 53 54 55 return 0; // indicates successful termination } // end main Outline 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. 65

Constructors and Destructors in Derived Classes • Instantiating derived-class object – Chain of constructor calls • Derived-class constructor invokes base class constructor – Implicitly or explicitly • sequence of constructor calls of inheritance hierarchy – constructor of based class is called first – next base class down hierarchy next – Continue down hierarchy until final derived class reached – Example: Point 3/Circle 4/Cylinder hierarchy • Point 3 constructor is called first • Cylinder constructor called last • Initializing data members – Each base-class constructor initializes data members and Inherited by derived class 66

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 67

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 ); // set x in coordinate pair int get. X() const; // return x from coordinate pair void set. Y( int ); // set y in coordinate pair int get. Y() const; // return y from coordinate pair void print() const; // output Point 3 object 19 20 21 22 private: int x; // x part of coordinate pair int y; // y part of coordinate pair 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) 2003 Prentice Hall, Inc. All rights reserved. 68

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" // Point 4 class definition 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; Output message to demonstrate constructor function call order. Outline point 4. cpp (1 of 3) Output message to demonstrate destructor function call order. 2003 Prentice Hall, Inc. All rights reserved. 69

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. 70

50 51 52 53 // return y from coordinate pair int Point 4: : get. Y() const { return y; 54 55 56 57 58 59 60 } // end function get. Y // output Point 4 object void Point 4: : print() const { cout << '[' << get. X() << ", " << get. Y() << ']'; 61 62 } // end function print Outline point 4. cpp (3 of 3) 2003 Prentice Hall, Inc. All rights reserved. 71

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" // Point 4 class definition 7 8 class Circle 5 : public Point 4 { 9 10 public: 11 12 13 // default constructor Circle 5( int = 0, double = 0. 0 ); 14 15 16 17 ~Circle 5(); // destructor void set. Radius( double ); // set radius double get. Radius() const; // return radius 18 19 20 21 double get. Diameter() const; // return diameter double get. Circumference() const; // return circumference double get. Area() const; // return area 22 23 void print() const; // output Circle 5 object Outline Constructor and destructor output messages to demonstrate function call order. circle 5. h (1 of 2) 24 2003 Prentice Hall, Inc. All rights reserved. 72

25 26 private: double radius; // Circle 5's radius 27 28 }; // end class Circle 5 29 30 #endif Outline circle 5. h (2 of 2) 2003 Prentice Hall, Inc. All rights reserved. 73

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" // Circle 5 class definition 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 constructor Output message to { demonstrate constructor set. Radius( radius. Value ); 15 16 17 18 cout << "Circle 5 constructor: "; print(); cout << endl; 19 20 } // end Circle 5 constructor function call order. Outline circle 5. cpp (1 of 4) 21 2003 Prentice Hall, Inc. All rights reserved. 74

22 23 24 25 26 27 28 29 // destructor Circle 5: : ~Circle 5() { cout << "Circle 5 destructor: "; print(); cout << endl; } // end Circle 5 destructor 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 Outline Output message to demonstrate destructor function call order. circle 5. cpp (2 of 4) 44 2003 Prentice Hall, Inc. All rights reserved. 75

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. 76

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. 77

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" // Circle 5 class definition 10 11 12 13 14 15 16 17 int main() { { // begin new scope 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; Point 4 object goes in and out of scope immediately. Outline fig 09_29. cpp (1 of 2) Instantiate two Circle 5 objects to demonstrate order of derived-class and baseclass constructor/destructor function calls. 2003 Prentice Hall, Inc. All rights reserved. 78

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 execution leaves scope. Derived-class Circle 5 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 constructor executes first Circle 5 constructor: Center = [72, 29]; Radius = 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 Point 4 constructor: [11, 22] Point 4 destructor: [11, 22] 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. 79

public, protected and private Inheritance 80

81 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
- Slides: 81