You are on page 1of 84

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.

9.1 Inheritance

Introduction

Software reusability Create new class from existing class


Absorb existing classs 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.

9.1 Class hierarchy


Direct base class

Introduction

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.

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.

9.1 Abstraction

Introduction

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.

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.

9.2 Base Classes and Derived Classes Inheritance examples


Base class Student Shape Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount

Loan

Employee Account

2003 Prentice Hall, Inc. All rights reserved.

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.

Fig. 9.2

Inheritance hierarchy for university CommunityMembers. CommunityMember

Employee

Student

Alumnus

Single inheritance

Faculty

Staff

Single inheritance

Administrator

Teacher

Single inheritance

AdministratorTeacher

Multiple inheritance

2003 Prentice Hall, Inc. All rights reserved.

10

Fig. 9.3

Inheritance hierarchy for Shapes.

Shape

TwoDimensionalShape

ThreeDimensionalShape

Circle

Square

Triangle

Sphere

Cube

Tetrahedron

2003 Prentice Hall, Inc. All rights reserved.

11

9.2 Base Classes and Derived Classes public inheritance


Specify with: Class TwoDimensionalShape : public Shape
Class TwoDimensionalShape 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.

13

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.4: point.h // Point class definition represents an x-y coordinate pair. #ifndef POINT_H #define POINT_H class Point { public: Point( int = 0, int = 0 ); // default constructor void setX( int ); int getX() const; void setY( int ); int getY() const; void print() const; private: int x; int y; // set x in coordinate pair // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair // output Point object

14

Outline
point.h (1 of 1)

// x part of coordinate pair // y part of coordinate pair

Maintain x- and ycoordinates as private data members.

}; // end class Point


#endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.5: point.cpp // Point class member-function definitions. #include <iostream> using std::cout; #include "point.h" // Point class definition

15

Outline
point.cpp (1 of 3)

// default constructor Point::Point( int xValue, int yValue ) { x = xValue; y = yValue; } // end Point constructor // set x in coordinate pair void Point::setX( int xValue ) { x = xValue; // no need for validation } // end function setX

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// return x from coordinate pair int Point::getX() const { return x; } // end function getX // set y in coordinate pair void Point::setY( int yValue ) { y = yValue; // no need for validation } // end function setY // return y from coordinate pair int Point::getY() const { return y; } // end function getY

16

Outline
point.cpp (2 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

45 46 47 48 49 50

// output Point object void Point::print() const { cout << '[' << x << ", " << y << ']'; } // end function print

17

Outline
point.cpp (3 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.6: pointtest.cpp // Testing class Point. #include <iostream> using std::cout; using std::endl; #include "point.h" // Point class definition

18

Outline
pointtest.cpp (1 of 2)

Create a Point object.


int main() { Point point( 72, 115 );
// instantiate Point object

// display point coordinates cout << "X coordinate is " << point.getX() Invoke << "\nY coordinate is " << point.getY(); point.setX( 10 ); // set x-coordinate point.setY( 10 ); // set y-coordinate // display new point value cout << "\n\nThe new location point.print(); cout << endl;

set functions to modify private data.

Invoke public function print to display new of point is "; coordinates.

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28

return 0; } // end main

// indicates successful termination

19

Outline
pointtest.cpp (2 of 2) pointtest.cpp output (1 of 1)

X coordinate is 72 Y coordinate is 115 The new location of point is [10, 10]

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.7: circle.h // Circle class contains x-y coordinate pair and radius. #ifndef CIRCLE_H #define CIRCLE_H class Circle { public:

20

Outline
circle.h (1 of 2)

// default constructor Circle( int = 0, int = 0, double = 0.0 );


void setX( int ); int getX() const; void setY( int ); int getY() const; void setRadius( double ); double getRadius() const;

// set // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair // set radius // return radius // return diameter // return circumference // return area

Note code similar to Point x code. in coordinate pair

double getDiameter() const; double getCircumference() const; double getArea() const;

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29 30 31 32 33 34 35

void print() const; private: int x; int y; double radius;

// output Circle object

21

// // y-coordinate of Circle's center // Circle's radius

Maintain x-y coordinates and radius as private data x-coordinate of Circle's center members. Note code similar to Point code.

Outline
circle.h (2 of 2)

}; // end class Circle

#endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// Fig. 9.8: circle.cpp // Circle class member-function definitions. #include <iostream> using std::cout; #include "circle.h" // Circle class definition

22

Outline
circle.cpp (1 of 4)

// default constructor Circle::Circle( int xValue, int yValue, double radiusValue ) { x = xValue; y = yValue; setRadius( radiusValue ); } // end Circle constructor // set x in coordinate pair void Circle::setX( int xValue ) { x = xValue; // no need for validation

} // end function setX

2003 Prentice Hall, Inc.


All rights reserved.

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

// return x from coordinate pair int Circle::getX() const { return x; } // end function getX // set y in coordinate pair void Circle::setY( int yValue ) { y = yValue; // no need for validation } // end function setY // return y from coordinate pair int Circle::getY() const { return y; } // end function getY

23

Outline
circle.cpp (2 of 4)

2003 Prentice Hall, Inc.


All rights reserved.

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// set radius void Circle::setRadius( double radiusValue ) { radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); } // end function setRadius // return radius double Circle::getRadius() const { return radius; } // end function getRadius // calculate and return diameter double Circle::getDiameter() const { return 2 * radius; } // end function getDiameter

24

Outline
circle.cpp (3 of 4)

Ensure non-negative value for radius.

2003 Prentice Hall, Inc.


All rights reserved.

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// calculate and return circumference double Circle::getCircumference() const { return 3.14159 * getDiameter(); } // end function getCircumference // calculate and return area double Circle::getArea() const { return 3.14159 * radius * radius; } // end function getArea // output Circle object void Circle::print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; } // end function print

25

Outline
circle.cpp (4 of 4)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.9: circletest.cpp // Testing class Circle. #include <iostream> using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include "circle.h" // Circle class definition

26

Outline
circletest.cpp (1 of 2)

Create Circle object.

int main() { Circle circle( 37, 43, 2.5 );

// instantiate Circle object

// display point coordinates cout << "X coordinate is " << circle.getX() << "\nY coordinate is " << circle.getY() << "\nRadius is " << circle.getRadius();

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

circle.setX( 2 ); circle.setY( 2 ); circle.setRadius( 4.25 );

// set new x-coordinate // set new y-coordinate // set new radius

27

Outline
circletest.cpp (2 of 2)

Use set functions to modify // display new point value cout << "\n\nThe new location and radius of circle private data. are\n"; circle.print();
// display floating-point values cout << fixed << setprecision( 2

Invoke public function print to display new with 2 digits of precision );coordinates.

// display Circle's diameter cout << "\nDiameter is " << circle.getDiameter(); // display Circle's circumference cout << "\nCircumference is " << circle.getCircumference(); // display Circle's area cout << "\nArea is " << circle.getArea(); cout << endl;

return 0;
} // end main

// indicates successful termination

2003 Prentice Hall, Inc.


All rights reserved.

X coordinate is 37 Y coordinate is 43 Radius is 2.5 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

28

Outline
circletest.cpp output (1 of 1)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.10: circle2.h // Circle2 class contains x-y coordinate pair and radius. #ifndef CIRCLE2_H #define CIRCLE2_H #include "point.h" // Point class definition

29

Outline
circle2.h (1 of 2)

Class Circle2 inherits from class Point.

class Circle2 : public Point {

public:

Keyword public indicates Colon indicates inheritance. // default constructor type=of inheritance. Circle2( int = 0, int = 0, double 0.0 );
void setRadius( double ); double getRadius() const; // set radius // return radius // return diameter // return circumference // return area

double getDiameter() const; double getCircumference() const; double getArea() const; void print() const; private: double radius;

Maintain private data // output Circle2 object

member radius.

// Circle2's radius

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29

30
}; // end class Circle2 #endif

Outline
circle2.h (2 of 2) circle2.cpp (1 of 3)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 9.11: circle2.cpp // Circle2 class member-function definitions. #include <iostream> using std::cout; #include "circle2.h" // Circle2 class definition

// default constructor Attempting to access base Circle2::Circle2( int xValue, int yValue, double radiusValue ) class Points private { data members x and y results x = xValue; in syntax errors. y = yValue; setRadius( radiusValue ); } // end Circle2 constructor

2003 Prentice Hall, Inc.


All rights reserved.

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// set radius void Circle2::setRadius( double radiusValue ) { radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); } // end function setRadius // return radius double Circle2::getRadius() const { return radius; } // end function getRadius // calculate and return diameter double Circle2::getDiameter() const { return 2 * radius; } // end function getDiameter

31

Outline
circle2.cpp (2 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

// calculate and return circumference double Circle2::getCircumference() const { return 3.14159 * getDiameter(); } // end function getCircumference // calculate and return area double Circle2::getArea() const { return 3.14159 * radius * radius; } // end function getArea // output Circle2 object void Circle2::print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; } // end function print

32

Outline
circle2.cpp (3 of 3)

Attempting to access base class Points private data members x and y results in syntax errors.

2003 Prentice Hall, Inc.


All rights reserved.

C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(12) : error C2248: 'x' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(20) : see declaration of 'x' C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(13) : error C2248: 'y' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(21) : see declaration of 'y' C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(56) : error C2248: 'x' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(20) : see declaration of 'x' C:\cpphtp4\examples\ch09\CircleTest\circle2.cpp(56) : error C2248: 'y' : cannot access private member declared in class 'Point' C:\cpphtp4\examples\ch09\circletest\point.h(21) : Attempting to see declaration of 'y'

33

Outline
circle2.cpp output (1 of 1)

access base class Points private data members x and y results in syntax errors.

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.12: point2.h // Point2 class definition represents an x-y coordinate pair. #ifndef POINT2_H #define POINT2_H class Point2 { public: Point2( int = 0, int = 0 ); // default constructor void setX( int ); int getX() const; void setY( int ); int getY() const; void print() const; // set x in coordinate pair // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair

34

Outline
point2.h (1 of 1)

protected: int x; // x part of int y; // y part of coordinate pair

Maintain x- and y// output Point2 coordinates asobject protected data, accessible to derived classes. coordinate pair

}; // end class Point2


#endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.13: point2.cpp // Point2 class member-function definitions. #include <iostream> using std::cout; #include "point2.h" // Point2 class definition

35

Outline
point2.cpp (1 of 3)

// default constructor Point2::Point2( int xValue, int yValue ) { x = xValue; y = yValue; } // end Point2 constructor // set x in coordinate pair void Point2::setX( int xValue ) { x = xValue; // no need for validation } // end function setX

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// return x from coordinate pair int Point2::getX() const { return x; } // end function getX // set y in coordinate pair void Point2::setY( int yValue ) { y = yValue; // no need for validation } // end function setY // return y from coordinate pair int Point2::getY() const { return y; } // end function getY

36

Outline
point2.cpp (2 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

45 46 47 48 49 50

// output Point2 object void Point2::print() const { cout << '[' << x << ", " << y << ']'; } // end function print

37

Outline
point2.cpp (3 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.14: circle3.h // Circle3 class contains x-y coordinate pair and radius. #ifndef CIRCLE3_H #define CIRCLE3_H #include "point2.h" // Point2 class definition

38

Outline
circle3.h (1 of 2)

Class Circle3 inherits from class Point2.

class Circle3 : public Point2 {

public:
// default constructor Circle3( int = 0, int = 0, double = 0.0 ); void setRadius( double ); double getRadius() const; // set radius // return radius // return diameter // return circumference // return area

double getDiameter() const; double getCircumference() const; double getArea() const; void print() const; private: double radius;

Maintain private data // output Circle3 object

member radius.

// Circle3's radius

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29

39
}; // end class Circle3 #endif

Outline
circle3.h (2 of 2)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// Fig. 9.15: circle3.cpp // Circle3 class member-function definitions. #include <iostream> using std::cout; #include "circle3.h" // Circle3 class definition

40

Outline
circle3.cpp (1 of 3)

// default constructor Circle3::Circle3( int xValue, { x = xValue; y = yValue; setRadius( radiusValue ); } // end Circle3 constructor

Constructor first implicitly Modify data calls base inherited classs default int yValue, radiusValue members x double and y, declared constructor. protected in base class Point2.

// set radius void Circle3::setRadius( double radiusValue ) { radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );

} // end function setRadius

2003 Prentice Hall, Inc.


All rights reserved.

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

// return radius double Circle3::getRadius() const { return radius; } // end function getRadius // calculate and return diameter double Circle3::getDiameter() const { return 2 * radius; } // end function getDiameter // calculate and return circumference double Circle3::getCircumference() const { return 3.14159 * getDiameter(); } // end function getCircumference

41

Outline
circle3.cpp (2 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

46 47 48 49 50 51 52 53 54 55 56 57 58 59

// calculate and return area double Circle3::getArea() const { return 3.14159 * radius * radius; } // end function getArea // output Circle3 object void Circle3::print() const { cout << "Center = [" << x << ", " << y << ']' << "; Radius = " << radius; } // end function print

42

Outline
circle3.cpp (3 of 3) Access inherited data members x and y, declared protected in base class Point2.

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.16: circletest3.cpp // Testing class Circle3. #include <iostream> using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include "circle3.h" // Circle3 class definition Create Circle3

43

Outline
circletest3.cpp (1 of 2)

object.

int main() { Use Circle3 circle( 37, 43, 2.5 ); // instantiate Circle3 object // display point coordinates cout << "X coordinate is " << circle.getX() << "\nY coordinate is " << circle.getY() << "\nRadius is " << circle.getRadius();

inherited get functions to access inherited protected data x and y. get function to Use Circle3 access private data radius.

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

circle.setX( 2 ); circle.setY( 2 ); circle.setRadius( 4.25 );

// set new x-coordinate // set new y-coordinate // set new radius Use inherited

44

Outline
circletest3.cpp (2 of 2)

// display new point value cout << "\n\nThe new location and radius circle.print();

set functions to modify inherited Use Circle3 set x function protected data and y. to of circle are\n"; modify private data radius.

// display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 ); // display Circle3's diameter cout << "\nDiameter is " << circle.getDiameter(); // display Circle3's circumference cout << "\nCircumference is " << circle.getCircumference(); // display Circle3's area cout << "\nArea is " << circle.getArea(); cout << endl;

return 0;
} // end main

// indicates successful termination

2003 Prentice Hall, Inc.


All rights reserved.

X coordinate is 37 Y coordinate is 43 Radius is 2.5 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

45

Outline
circletest3.cpp output (1 of 1)

2003 Prentice Hall, Inc.


All rights reserved.

46

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.17: point3.h // Point3 class definition represents an x-y coordinate pair. #ifndef POINT3_H #define POINT3_H class Point3 { public: Point3( int = 0, int = 0 ); // default constructor void setX( int ); int getX() const; void setY( int ); int getY() const; void print() const; private: int x; int y; // set x in coordinate pair // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair // output Point3 object practice: private over

47

Outline
point3.h (1 of 1)

Better software-engineering
protected when possible.

// x part of coordinate pair // y part of coordinate pair

}; // end class Point3


#endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.18: point3.cpp // Point3 class member-function definitions. #include <iostream> using std::cout; #include "point3.h" // Point3 class definition

48

Outline
point3.cpp (1 of 3)

// default constructor Point3::Point3( int xValue, int yValue ) : x( xValue ), y( yValue ) { // empty body } // end Point3 constructor // set x in coordinate pair void Point3::setX( int xValue ) { x = xValue; // no need for validation } // end function setX

Member initializers specify values of x and y.

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// return x from coordinate pair int Point3::getX() const { return x; } // end function getX // set y in coordinate pair void Point3::setY( int yValue ) { y = yValue; // no need for validation } // end function setY // return y from coordinate pair int Point3::getY() const { return y; } // end function getY

49

Outline
point3.cpp (2 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

45 46 47 48 49 50

// output Point3 object void Point3::print() const { cout << '[' << getX() << ", " << getY() << ']'; } // end function print

50

Outline
point3.cpp (3 of 3)

Invoke non-private member functions to access private data.

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.19: circle4.h // Circle4 class contains x-y coordinate pair and radius. #ifndef CIRCLE4_H #define CIRCLE4_H #include "point3.h" // Point3

51

Outline
circle4.h (1 of 2)

Class Circle4 inherits from class Point3 . class definition

class Circle4 : public Point3 {

public:
// default constructor Circle4( int = 0, int = 0, double = 0.0 ); void setRadius( double ); double getRadius() const; // set radius // return radius // return diameter // return circumference // return area

double getDiameter() const; double getCircumference() const; double getArea() const; void print() const; private: double radius;

Maintain private data // output Circle4 object

member radius.

// Circle4's radius

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29

52
}; // end class Circle4 #endif

Outline
circle4.h (2 of 2)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.20: circle4.cpp // Circle4 class member-function definitions. #include <iostream> using std::cout; #include "circle4.h" // Circle4 class definition

53

Outline
circle4.cpp (1 of 3)

// default constructor Circle4::Circle4( int xValue, int : Point3( xValue, yValue ) // call base-class constructor { setRadius( radiusValue ); } // end Circle4 constructor // set radius void Circle4::setRadius( double radiusValue ) { radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); } // end function setRadius

Base-class initializer syntax passes arguments to base class Point3 . yValue, double radiusValue )

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// return radius double Circle4::getRadius() const { return radius; } // end function getRadius // calculate and return diameter double Circle4::getDiameter() const { return 2 * getRadius(); } // end function getDiameter // calculate and return circumference double Circle4::getCircumference() const { return 3.14159 * getDiameter(); } // end function getCircumference

54

Outline
circle4.cpp (2 of 3) Invoke function getRadius rather than directly accessing data member radius.

2003 Prentice Hall, Inc.


All rights reserved.

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

// calculate and return area double Circle4::getArea() const { return 3.14159 * getRadius() * getRadius(); } // end function getArea

55

Outline
circle4.cpp (3 of 3)

// output Circle4 object void Circle4::print() const { cout << "Center = "; Point3::print(); // invoke cout << "; Radius = " << getRadius(); } // end function print

Redefine class Point3s member function print. Invoke function getRadius rather than directly accessing Invoke base-class Point3 s data memberusing radius . print function binary scope-resolution operator Point3's (::). print function

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 9.21: circletest4.cpp // Testing class Circle4. #include <iostream> using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include "circle4.h" // Circle4 class definition

56

Outline
circletest4.cpp (1 of 2)

Create Circle4 object. inherited get functions to access inherited protected data x and y. get function to Use Circle3 access private data radius.

int main() { Use Circle4 circle( 37, 43, 2.5 ); // instantiate Circle4 object // display point coordinates cout << "X coordinate is " << circle.getX() << "\nY coordinate is " << circle.getY() << "\nRadius is " << circle.getRadius();

2003 Prentice Hall, Inc.


All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

circle.setX( 2 ); circle.setY( 2 ); circle.setRadius( 4.25 );

// set new x-coordinate // set new y-coordinate // set new radius Use inherited

57

Outline
circletest4.cpp (2 of 2)

// display new circle value cout << "\n\nThe new location and radius circle.print();

set functions to modify inherited Use Circle3 set x function protected data and y. to of circle are\n"; modify private data radius.

// display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 ); // display Circle4's diameter cout << "\nDiameter is " << circle.getDiameter(); // display Circle4's circumference cout << "\nCircumference is " << circle.getCircumference(); // display Circle4's area cout << "\nArea is " << circle.getArea(); cout << endl;

return 0;
} // end main

// indicates successful termination

2003 Prentice Hall, Inc.


All rights reserved.

X coordinate is 37 Y coordinate is 43 Radius is 2.5 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

58

Outline
circletest4.cpp output (1 of 1)

2003 Prentice Hall, Inc.


All rights reserved.

59

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.22: cylinder.h // Cylinder class inherits from class Circle4. #ifndef CYLINDER_H #define CYLINDER_H #include "circle4.h"

60

Outline
cylinder.h (1 of 2)

// Circle4 class definition

Class Cylinder inherits from class Circle4.

class Cylinder : public Circle4 {

public:
// default constructor Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); void setHeight( double ); double getHeight() const; double getArea() const; double getVolume() const; void print() const; private: double height; // set Cylinder's height // return Cylinder's height // return Cylinder's area // return Cylinder's volume // output Cylinder Maintain private data

member height.
// Cylinder's height

}; // end class Cylinder

2003 Prentice Hall, Inc.


All rights reserved.

26 27

61
#endif

Outline
cylinder.h (2 of 2)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 9.23: cylinder.cpp // Cylinder class inherits from class Circle4. #include <iostream> using std::cout; #include "cylinder.h" // Cylinder class definition

cylinder.cpp (1 of 3)

// default constructor to base Cylinder::Cylinder( int xValue, int yValue, passes doublearguments radiusValue, class Circle4. double heightValue ) : Circle4( xValue, yValue, radiusValue ) { setHeight( heightValue ); } // end Cylinder constructor

Base-class initializer syntax

2003 Prentice Hall, Inc.


All rights reserved.

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

// set Cylinder's height void Cylinder::setHeight( double heightValue ) { height = ( heightValue < 0.0 ? 0.0 : heightValue ); } // end function setHeight // get Cylinder's height double Cylinder::getHeight() const { return height; } // end function getHeight // redefine Circle4 function getArea to double Cylinder::getArea() const { return 2 * Circle4::getArea() + getCircumference() * getHeight(); } // end function getArea

62

Outline
cylinder.cpp (2 of 3)

Redefine base class Circle4s member function Invoke base-class calculate Cylinder area Circle4sgetArea getAreato return Cylinder surface area. function using binary scoperesolution operator (::).

2003 Prentice Hall, Inc.


All rights reserved.

40 41 42 43 44 45 46 47 48 49 50 51 52 53

// calculate Cylinder volume double Cylinder::getVolume() const { return Circle4::getArea() * getHeight(); } // end function getVolume

63

Invoke base-class Circle4s getArea function using binary scoperesolution operator (::).

Outline
cylinder.cpp (3 of 3)

// output Cylinder object void Cylinder::print() const { Circle4::print(); cout << "; Height = " << getHeight(); } // end function print

Redefine class Circle4s member function print. Invoke base-class Circle4s print function using binary scope-resolution operator (::).

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.24: cylindertest.cpp // Testing class Cylinder. #include <iostream> using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include "cylinder.h" // Cylinder class definition

64

Outline
cylindertest.cpp (1 of 3)

int main() { // instantiate Cylinder object Cylinder cylinder( 12, 23, 2.5, 5.7 ); // display point coordinates cout << "X coordinate is " << cylinder.getX() << "\nY coordinate is " << cylinder.getY() << "\nRadius is " << cylinder.getRadius() << "\nHeight is " << cylinder.getHeight();

Invoke indirectly inherited Point3 member functions. Invoke directly inherited Invoke Cylinder member Circle4 member function. function.

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

cylinder.setX( 2 ); cylinder.setY( 2 ); cylinder.setRadius( 4.25 ); cylinder.setHeight( 10 );

// // // //

set set set set

new new new new

x-coordinate y-coordinate Invoke indirectly inherited radius Point3 member functions. height Invoke directly inherited

65

Outline
cylindertest.cpp (2 of 3)

// display new cylinder value cout << "\n\nThe new location and cylinder.print();

Circle4 member function. Invoke Cylinder member function. radius of circle are\n";

// display floating-point values with 2 digits of precision cout << fixed << setprecision( 2 function. );
// display cylinder's diameter cout << "\n\nDiameter is " << cylinder.getDiameter(); // display cylinder's circumference cout << "\nCircumference is " << cylinder.getCircumference(); // display cylinder's area cout << "\nArea is " << cylinder.getArea();

Invoke redefined print

// display cylinder's volume cout << "\nVolume is " << cylinder.getVolume();

Invoke redefined getArea function.

2003 Prentice Hall, Inc.


All rights reserved.

51 52 53 54 55

cout << endl; return 0; } // end main // indicates successful termination

66

Outline
cylindertest.cpp (3 of 3) cylindertest.cpp output (1 of 1)

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

2003 Prentice Hall, Inc.


All rights reserved.

67

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: Point3/Circle4/Cylinder hierarchy Point3 constructor called last Point3 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.

68

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.

69

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// Fig. 9.25: point4.h // Point4 class definition represents an x-y coordinate pair. #ifndef POINT4_H #define POINT4_H class Point4 { public: Point4( int = 0, int = 0 ); // default constructor ~Point4(); // destructor void setX( int ); int getX() const; void setY( int ); int getY() const; void print() const; private: int x; int y; // set x in coordinate pair // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair // output Point3 object

70

Outline

point4.h (1 of 1) Constructor and destructor output messages to demonstrate function call order.

// x part of coordinate pair // y part of coordinate pair

}; // end class Point4 #endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.26: point4.cpp // Point4 class member-function definitions. #include <iostream> using std::cout; using std::endl; #include "point4.h" // Point4 class definition

71

Outline
point4.cpp (1 of 3)

// default constructor Point4::Point4( int xValue, int yValue ) : x( xValue ), y( yValue ) { cout << "Point4 constructor: "; print(); cout << endl;
} // end Point4 constructor // destructor Point4::~Point4() { cout << "Point4 destructor: "; print(); cout << endl;

Output message to demonstrate constructor function call order.

Output message to demonstrate destructor function call order.

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

72
} // end Point4 destructor // set x in coordinate pair void Point4::setX( int xValue ) { x = xValue; // no need for validation } // end function setX // return x from coordinate pair int Point4::getX() const { return x; } // end function getX // set y in coordinate pair void Point4::setY( int yValue ) { y = yValue; // no need for validation

Outline
point4.cpp (2 of 3)

} // end function setY

2003 Prentice Hall, Inc.


All rights reserved.

50 51 52 53 54 55 56 57 58 59 60 61 62

// return y from coordinate pair int Point4::getY() const { return y; } // end function getY // output Point4 object void Point4::print() const { cout << '[' << getX() << ", " << getY() << ']'; } // end function print

73

Outline
point4.cpp (3 of 3)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// Fig. 9.27: circle5.h // Circle5 class contains x-y coordinate pair and radius. #ifndef CIRCLE5_H #define CIRCLE5_H #include "point4.h" // Point4 class definition

74

Outline
circle5.h (1 of 2)

class Circle5 : public Point4 {

public:
// default constructor Circle5( int = 0, int = 0, double = 0.0 ); ~Circle5(); void setRadius( double ); double getRadius() const; // destructor // set radius // return radius // return diameter // return circumference // return area

Constructor and destructor output messages to demonstrate function call order.

double getDiameter() const; double getCircumference() const; double getArea() const;

void print() const;

// output Circle5 object

2003 Prentice Hall, Inc.


All rights reserved.

25 26 27 28 29 30

private: double radius;

75
// Circle5's radius

Outline
circle5.h (2 of 2)

}; // end class Circle5 #endif

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Fig. 9.28: circle5.cpp // Circle5 class member-function definitions. #include <iostream> using std::cout; using std::endl; #include "circle5.h" // Circle5 class definition

76

Outline
circle5.cpp (1 of 4)

// default constructor Circle5::Circle5( int xValue, int yValue, double radiusValue ) : Point4( xValue, yValue ) // call base-class Output constructor message to { demonstrate constructor setRadius( radiusValue );

function call order.

cout << "Circle5 constructor: "; print(); cout << endl; } // end Circle5 constructor

2003 Prentice Hall, Inc.


All rights reserved.

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

// destructor Circle5::~Circle5() { cout << "Circle5 destructor: "; print(); cout << endl; } // end Circle5 destructor

77

Outline
circle5.cpp (2 of 4) Output message to demonstrate destructor function call order.

// set radius void Circle5::setRadius( double radiusValue ) { radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
} // end function setRadius // return radius double Circle5::getRadius() const { return radius; } // end function getRadius

2003 Prentice Hall, Inc.


All rights reserved.

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

// calculate and return diameter double Circle5::getDiameter() const { return 2 * getRadius(); } // end function getDiameter // calculate and return circumference double Circle5::getCircumference() const { return 3.14159 * getDiameter(); } // end function getCircumference // calculate and return area double Circle5::getArea() const { return 3.14159 * getRadius() * getRadius(); } // end function getArea

78

Outline
circle5.cpp (3 of 4)

2003 Prentice Hall, Inc.


All rights reserved.

66 67 68 69 70 71 72 73

// output Circle5 object void Circle5::print() const { cout << "Center = "; Point4::print(); // invoke Point4's print function cout << "; Radius = " << getRadius(); } // end function print

79

Outline
circle5.cpp (4 of 4)

2003 Prentice Hall, Inc.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 9.29: fig09_29.cpp // Display order in which base-class and derived-class // constructors are called. #include <iostream> using std::cout; using std::endl; #include "circle5.h" // Circle5 class definition

80

Outline
fig09_29.cpp (1 of 2)

int main() { { // begin new scope Point4 point( 11, 22 ); } // end scope cout << endl; Circle5 circle1( 72, 29, 4.5 ); cout << endl; Circle5 circle2( 5, 5, 10 ); cout << endl;

Point4 object goes in and out of scope immediately.

Instantiate two Circle5 objects to demonstrate order of derived-class and baseclass constructor/destructor function calls.

2003 Prentice Hall, Inc.


All rights reserved.

26 27 28 29

81
return 0; // indicates successful termination

Outline
fig09_29.cpp (2 of 2) fig09_29.cpp output (1 of 1)

Point4 constructor called for object in block; destructor } // end main called immediately as Base-class Point4 Point4 constructor: [11, 22] execution leaves scope. Derived-class Circle5 Point4 destructor: [11, 22] constructor executes first constructor body executes when instantiating derivedBase-class Point4 after base-class Point4 Point4 constructor: [72, 29] class Circle5 object. s Derived-class Circle5 constructor executes Circle5 constructor: Center = [72, 29]; Radius = 4.5 finishes first constructor body executes when instantiating derivedexecution. Destructors for Circle5 after base-class Point4 Point4 constructor: [5, 5] class Circle5 object. s object called in reverse order constructor finishes Circle5 constructor: Center = [5, 5]; Radius = 10 Destructors for Circle5 of constructors. execution. object called in reverse order Circle5 destructor: Center = [5, 5]; Radius = 10 of constructors. Point4 destructor: [5, 5]
Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: [72, 29]

2003 Prentice Hall, Inc.


All rights reserved.

9.7 Uses a

Uses A and Knows A Relationships

82

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


public inheritance Type of inheritance protected inheritance protected in derived class. Can be accessed directly by all non-static member functions and friend functions. protected in derived class. Can be accessed directly by all non-static member functions and friend functions. Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class. private inheritance private in derived class. Can be accessed directly by all non-static member functions and friend functions. private in derived class. Can be accessed directly by all non-static member functions and friend functions. Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.

83

Base class member access specifier

Public

public in derived class. Can be accessed directly by any non-static member functions, friend functions and nonmember functions.

protected in derived class. Can be accessed directly by all Protected non-static member functions and friend functions. Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.

Private

2003 Prentice Hall, Inc. All rights reserved.

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

You might also like