Title: Inheritance
1Inheritance
- Software reusability.
- Create new (derived) class from existing (base)
class or classes. - New derived class is to inherit the data members
and member functions of a previous defined base
class. - New derived class can have its own members
(attributes and behaviors) .
2Inheritance
- In addition, new class can embellish the members
(attributes and behaviors) of base class. - Derived class can be large, but more specific.
- Derived class can also be a base class for future
derived class. - Single inheritance derived from one base class
- Multiple inheritance derived from multiple base
classes.
3Inheritance
- Derived class is subclass (larger), while base
class is supper-class (small), in aspect of
usability, but size is small. - Derived class can be larger than the base one.
- Derived class is more specific than its base
class, represents a small group of objects.
4Inheritance
- Three type of inheritance - public, private, and
protected. - Public inheritance every object of derived class
from a base class is also object of the base
class. The converse is not true. - Derived class can not access the private member
of its base class, but can access the public and
protected members of its base class.
5Inheritance
- "is a relationships" , Inheritance object of a
derived class type may be treated as an object of
the base-class type. - " has a relationships", Composition a class
object has one or more objects of other classes
as members.
6Inheritance
- Since a derived class can not access the private
members of its base class, we introduce protected
members of its base class. - A derived class can however access public and
protected members of its base class. - Why we need inheritance?
7Inheritance
- Inheritance examples
- Student (supper class)
- graduate (sub-class)
- undergraduate (sub-class)
- Shape (supper class)
- circle (sub-class)
- triangle (sub-class)
- rectangle (sub-class)
8Inheritance
- Inheritance examples
- loan
- Car loan
- Home Improvement Loan
- Mortgaeg Loan
- Employee
- faculty
- staff
9Inheritance
- Account
- Checking Account
- Savings Account
- Certificated Deposit (CD)
10Inheritance
- Inheritance forms tree-hierarchical structure
- Examples
University-Members
Employee Students Alumnus (single inheritance)
Faculty Staff (single inheritance)
Administrator Teachers (single inheritance),
(multiple inheritance)
Administrator Teacher
11Inheritance
- Inheritance forms tree-hierarchical structure
- Examples
Shape
TwoDimensionalShape ThreeDimensionalShape
circle square triangle sphere
cube tetrahedron
This is public inheritance.
How to define a derived class?
class TwoDimensionalShape public Shape
12Inheritance
- public, private and protected inheritances
- With public inheritance, public and protected
members of the base class are inherited as public
and protected members of the derived class. - Private member and friend functions of the base
class are not accessible from the derived
classes. - Friend functions are not inherited.
- What is "protected" member?
13Inheritance
- "protected" access as intermediate level between
public access and private access. - The reason to introduce protected members is to
allow the members of derived class to access.
14Inheritance
- Distinguish between inheritance and accessibility
relationships among base class and derived class
should be clear. - Syntax of bass class and derived class
class baseName private (or protected)
public
class derivedName public baseName private
(or protected) public
15Inheritance
- Example casting base-class pointers to
derived-class pointers - Show an object of derived class can be treated as
a object of base class - Assign derived-class pointers to base-class
pointers - Explicitly cast to convert a base-class pointer
to a derived-class pointer (downcasting)b
16// Fig. 9.4 point.h // Definition of class
Point ifndef POINT_H define POINT_H include
ltiostreamgt using stdostream class Point
friend ostream operatorltlt( ostream , const
Point ) public Point( int 0, int 0 )
// default constructor void setPoint( int,
int ) // set coordinates int getX()
const return x // get x coordinate int
getY() const return y // get y
coordinate protected // accessible by
derived classes int x, y // x and y
coordinates of the Point endif
17// Fig. 9.4 point.cpp // Member functions for
class Point include ltiostreamgt include
"point.h" // Constructor for class
Point PointPoint( int a, int b ) setPoint( a,
b ) // Set x and y coordinates of Point void
PointsetPoint( int a, int b ) x a y
b // Output Point (with overloaded stream
insertion operator) ostream operatorltlt( ostream
output, const Point p ) output ltlt '' ltlt
p.x ltlt ", " ltlt p.y ltlt '' return output
// enables cascaded calls
18// Fig. 9.4 circle.h // Definition of class
Circle ifndef CIRCLE_H define
CIRCLE_H include ltiostreamgt using
stdostream include ltiomanipgt using
stdios using stdsetiosflags using
stdsetprecision include "point.h"
19class Circle public Point // Circle inherits
from Point friend ostream operatorltlt( ostream
, const Circle ) public // default
constructor Circle( double r 0.0, int x 0,
int y 0 ) void setRadius( double ) //
set radius double getRadius() const //
return radius double area() const //
calculate area protected double
radius endif
20// Fig. 9.4 circle.cpp // Member function
definitions for class Circle include
"circle.h" // Constructor for Circle calls
constructor for Point // with a member
initializer then initializes radius. CircleCircl
e( double r, int a, int b ) Point( a, b )
// call base-class constructor setRadius( r
) // Set radius of Circle void
CirclesetRadius( double r ) radius ( r
gt 0 ? r 0 ) // Get radius of Circle double
CirclegetRadius() const return radius
21 // Calculate area of Circle double
Circlearea() const return 3.14159 radius
radius // Output a Circle in the form //
Center x, y Radius . ostream
operatorltlt( ostream output, const Circle c
) output ltlt "Center " ltlt static_castlt
Point gt( c ) ltlt " Radius "
ltlt setiosflags( iosfixed iosshowpoint )
ltlt setprecision( 2 ) ltlt c.radius
return output // enables cascaded calls
22// Fig. 9.4 fig09_04.cpp // Casting base-class
pointers to derived-class pointers include
ltiostreamgt using stdcout using
stdendl include ltiomanipgt include
"point.h" include "circle.h" int main()
Point pointPtr 0, p( 30, 50 ) Circle
circlePtr 0, c( 2.7, 120, 89 )
23 cout ltlt "Point p " ltlt p ltlt "\nCircle c " ltlt
c ltlt '\n' // Treat a Circle as a Point (see
only the base class part) pointPtr c //
assign address of Circle to pointPtr cout ltlt
"\nCircle c (via pointPtr) " ltlt
pointPtr ltlt '\n' // Treat a Circle as a
Circle (with some casting) // cast base-class
pointer to derived-class pointer circlePtr
static_castlt Circle gt( pointPtr ) cout ltlt
"\nCircle c (via circlePtr)\n" ltlt circlePtr
ltlt "\nArea of c (via circlePtr) "
ltlt circlePtr-gtarea() ltlt '\n'
24 // DANGEROUS Treat a Point as a Circle
pointPtr p // assign address of Point to
pointPtr // cast base-class pointer to
derived-class pointer circlePtr static_castlt
Circle gt( pointPtr ) cout ltlt "\nPoint p
(via circlePtr)\n" ltlt circlePtr ltlt
"\nArea of object circlePtr points to "
ltlt circlePtr-gtarea() ltlt endl return 0
25(No Transcript)
26Inheritance
- A derived class's member functions may need to
access certain of its base class's data member
and member functions. - Difference between function overriding and
function overloading - Another example overriding base-class members in
a derived class
27// Fig. 9.5 employ.h // Definition of class
Employee ifndef EMPLOY_H define EMPLOY_H class
Employee public Employee( const char ,
const char ) // constructor void print()
const // output first and last name
Employee() // destructor private
char firstName // dynamically allocated
string char lastName // dynamically
allocated string endif
28// Fig. 9.5 employ.cpp // Member function
definitions for class Employee include
ltiostreamgt using stdcout include
ltcstringgt include ltcassertgt include
"employ.h" // Constructor dynamically allocates
space for the // first and last name and uses
strcpy to copy // the first and last names into
the object. EmployeeEmployee( const char
first, const char last ) firstName new
char strlen( first ) 1 assert( firstName
! 0 ) // terminate if not allocated
29strcpy( firstName, first ) lastName new
char strlen( last ) 1 assert( lastName
! 0 ) // terminate if not allocated strcpy(
lastName, last ) // Output employee name void
Employeeprint() const cout ltlt firstName ltlt
' ' ltlt lastName // Destructor deallocates
dynamically allocated memory EmployeeEmployee()
delete firstName // reclaim dynamic
memory delete lastName // reclaim
dynamic memory
30// Fig. 9.5 hourly.h // Definition of class
HourlyWorker ifndef HOURLY_H define
HOURLY_H include "employ.h" class HourlyWorker
public Employee public HourlyWorker(
const char, const char, double, double )
double getPay() const // calculate and return
salary void print() const // overridden
base-class print private double wage
// wage per hour double hours //
hours worked for week endif
31// Fig. 9.5 hourly.cpp // Member function
definitions for class HourlyWorker include
ltiostreamgt using stdcout using
stdendl include ltiomanipgt using
stdios using stdsetiosflags using
stdsetprecision include "hourly.h"
32// Constructor for class HourlyWorker HourlyWorker
HourlyWorker( const char first,
const char last,
double initHours, double initWage )
Employee( first, last ) // call base-class
constructor hours initHours // should
validate wage initWage // should
validate // Get the HourlyWorker's pay double
HourlyWorkergetPay() const return wage
hours
33// Print the HourlyWorker's name and pay void
HourlyWorkerprint() const cout ltlt
"HourlyWorkerprint() is executing\n\n"
Employeeprint() // call base-class print
function cout ltlt " is an hourly worker with
pay of " ltlt setiosflags( iosfixed
iosshowpoint ) ltlt setprecision( 2 ) ltlt
getPay() ltlt endl
34// Fig. 9.5 fig.09_05.cpp // Overriding a
base-class member function in a // derived
class. include "hourly.h" int main()
HourlyWorker h( "Bob", "Smith", 40.0, 10.00 )
h.print() return 0
35Inheritance
Result
HourlyWorkerprint() is executing Bob Smith is
an hourly worker with pay of 400.00
Conclusion the member functions of derived
class often override The ones of base class, if
they have same mane of member functions.
36Inheritance
- A base class can be inherited to derived class as
public, protected, and private. - Protected and private are rare to use.
- The Fig 9.6 summarizes the accessibility of base
class-members in a derived class for each type of
inheritance. It tells you two things. One is how
to access members of the base class. Two is what
are the type of hidden (implicitly declared in
base class) members in derived class.
37Inheritance
- Type of inheritance specifier in derived class
definition - Accessibility specifier in base class definition
class baseName private (or protected)
public
class derivedName public baseName private
(or protected) public
Accessbility specifier
Inheritance specifier
38Inheritance
Type of inheritance
Base class member access specifier
public protected
private
protected in derived class
private in derived class
public
public in derived class
protected
protected in derived class
private in derived class
protected in derived class
private
Hidden in derived class
Hidden in derived class
Hidden in derived class
39Inheritance
- If a derived class inherits from a base-class, as
public inheritance, the public members of the
base class become public members of the derived
class, and protected members of the base class
become protected members of the derived class.
The members can be accessed by member and friend
functions of the derived class. Referring to
40Inheritance
- In any inheritance type (public, protected, and
private), private members of the base can be
accessed indirectly through public or protected
members of the base class. Referring to - The privilege order is private, protected, and
public. - The member functions of derived class used to
directly access the members of the base class are
non-static member functions.
41Inheritance
- The type of inheritance and specification of base
members effect the next inheritance of derived
class, if the derived class become a base class
of other classes. - In class hierarchy, we have direct base classes
and indirect base class. Indirect base class is
inherited from two or more levels up the class
hierarchy.
42Inheritance
- Example if class A is the base of derived class
B (explicitly specified in definition of class B
after colon), and class B itself is also a base
class of another derived class C, class C is also
inherited implicitly from class A. The class A is
the indirect base of class C.
43Inheritance
- Because a derived class inherits its base class's
members, when an object of a derived class is
instantiated. The base class's constructor must
be called to initialize the base-class members of
the derived-class object. - A base-class initializer can be provided in the
derived-class constructor to call the base-class
constructor explicitly otherwise, the derived
class's constructor will call the defaulted
constructor of the base-class implicitly.
44Inheritance
- A derived-class constructor always calls the
constructor for its base class first to
initialize the derived-class's base-class data
members. - A derived-class destructor is called before its
base-class destructor. - Example order in which base-class and
derived-class constructors and destructors are
called.
45// Fig. 9.7 point2.h // Definition of class
Point ifndef POINT2_H define POINT2_H class
Point public Point( int 0, int 0 ) //
default constructor Point() //
destructor protected // accessible by
derived classes int x, y // x and y
coordinates of Point endif
46// Fig. 9.7 point2.cpp // Member function
definitions for class Point include
ltiostreamgt using stdcout using
stdendl include "point2.h" // Constructor
for class Point PointPoint( int a, int b )
x a y b cout ltlt "Point constructor
" ltlt '' ltlt x ltlt ", " ltlt y ltlt '' ltlt
endl
47 // Destructor for class Point PointPoint()
cout ltlt "Point destructor " ltlt ''
ltlt x ltlt ", " ltlt y ltlt '' ltlt endl
48// Fig. 9.7 circle2.h // Definition of class
Circle ifndef CIRCLE2_H define
CIRCLE2_H include "point2.h" class Circle
public Point public // default constructor
Circle( double r 0.0, int x 0, int y 0
) Circle() private double radius
endif
49// Fig. 9.7 circle2.cpp // Member function
definitions for class Circle include
ltiostreamgt using stdcout using
stdendl include "circle2.h" // Constructor
for Circle calls constructor for
Point CircleCircle( double r, int a, int b )
Point( a, b ) // call base-class
constructor radius r // should validate
cout ltlt "Circle constructor radius is "
ltlt radius ltlt " " ltlt x ltlt ", " ltlt y ltlt '' ltlt
endl
50 // Destructor for class Circle CircleCircle()
cout ltlt "Circle destructor radius is "
ltlt radius ltlt " " ltlt x ltlt ", " ltlt y ltlt '' ltlt
endl
51// Fig. 9.7 fig09_07.cpp // Demonstrate when
base-class and derived-class // constructors and
destructors are called. include
ltiostreamgt using stdcout using
stdendl include "point2.h" include
"circle2.h" int main() // Show constructor
and destructor calls for Point Point
p( 11, 22 )
52 cout ltlt endl Circle circle1( 4.5, 72, 29
) cout ltlt endl Circle circle2( 10, 5, 5
) cout ltlt endl return 0
53(No Transcript)
54Inheritance
- Under public inheritance, although a
derived-class object is also a base-class object,
- assign a base-class object to a derived-class
object would leave the additional derived-class
members undefined. - assign a derived-class object to a base-class
object is a syntax error.
55Inheritance
- Under public inheritance, one can a base-class
pointer to refer a derived-class pointer, but can
not use a derived-class pointer to refer a
base-class object.
56Inheritance
- Composition vs. inheritance ("has a
relationships" vs. "is a relationships") - Example point class-gtcircle class-gtcylinder
57// Fig. 9.8 point2.h // Definition of class
Point ifndef POINT2_H define POINT2_H include
ltiostreamgt using stdostream class Point
friend ostream operatorltlt( ostream , const
Point ) public Point( int 0, int 0 )
// default constructor void setPoint( int,
int ) // set coordinates int getX()
const return x // get x coordinate int
getY() const return y // get y
coordinate protected // accessible to
derived classes int x, y // coordinates
of the point endif
58// Fig. 9.8 point2.cpp // Member functions for
class Point include "point2.h" // Constructor
for class Point PointPoint( int a, int b )
setPoint( a, b ) // Set the x and y
coordinates void PointsetPoint( int a, int b
) x a y b // Output the
Point ostream operatorltlt( ostream output, const
Point p ) output ltlt '' ltlt p.x ltlt ", " ltlt
p.y ltlt '' return output //
enables cascading
59// Fig. 9.8 fig09_08.cpp // Driver for class
Point include ltiostreamgt using stdcout using
stdendl include "point2.h" int main()
Point p( 72, 115 ) // instantiate Point object
p // protected data of Point inaccessible to
main cout ltlt "X coordinate is " ltlt p.getX()
ltlt "\nY coordinate is " ltlt p.getY()
p.setPoint( 10, 10 ) cout ltlt "\n\nThe new
location of p is " ltlt p ltlt endl return 0
60(No Transcript)
61Example Fig 9.9
62// Fig. 9.8 point2.h // Definition of class
Point ifndef POINT2_H define POINT2_H include
ltiostreamgt using stdostream class Point
friend ostream operatorltlt( ostream , const
Point ) public Point( int 0, int 0 )
// default constructor void setPoint( int,
int ) // set coordinates int getX()
const return x // get x coordinate int
getY() const return y // get y
coordinate protected // accessible to
derived classes int x, y // coordinates
of the point endif
63// Fig. 9.9 circle2.h // Definition of class
Circle ifndef CIRCLE2_H define
CIRCLE2_H include ltiostreamgt using
stdostream include "point2.h" class Circle
public Point friend ostream operatorltlt(
ostream , const Circle ) public Circle(
double r 0.0, int x 0, int y 0 ) void
setRadius( double ) // set radius double
getRadius() const // return radius double
area() const // calculate
area protected // accessible to derived
classes double radius // radius of the
Circle endif
64// Fig. 9.8 point2.cpp // Member functions for
class Point include "point2.h" // Constructor
for class Point PointPoint( int a, int b )
setPoint( a, b ) // Set the x and y
coordinates void PointsetPoint( int a, int b
) x a y b // Output the
Point ostream operatorltlt( ostream output, const
Point p ) output ltlt '' ltlt p.x ltlt ", " ltlt
p.y ltlt '' return output // enables
concatenation
65// Fig. 9.9 circle2.cpp // Member function
definitions for class Circle include
ltiomanipgt using stdios using
stdsetiosflags using stdsetprecision inclu
de "circle2.h" // Constructor for Circle calls
constructor for Point // with a member
initializer and initializes radius CircleCircle(
double r, int a, int b ) Point( a, b )
// call base-class constructor setRadius( r )
66// Set radius void CirclesetRadius( double r )
radius ( r gt 0 ? r 0 ) // Get
radius double CirclegetRadius() const return
radius // Calculate area of Circle double
Circlearea() const return 3.14159 radius
radius // Output a circle in the form //
Center x, y Radius . ostream
operatorltlt( ostream output, const Circle c
) output ltlt "Center " ltlt static_castlt
Point gt ( c ) ltlt " Radius "
ltlt setiosflags( iosfixed iosshowpoint )
ltlt setprecision( 2 ) ltlt c.radius
return output // enables cascaded calls
67// Fig. 9.9 fig09_09.cpp // Driver for class
Circle include ltiostreamgt using
stdcout using stdendl include
"point2.h" include "circle2.h" int main()
Circle c( 2.5, 37, 43 ) cout ltlt "X
coordinate is " ltlt c.getX() ltlt "\nY
coordinate is " ltlt c.getY() ltlt "\nRadius
is " ltlt c.getRadius()
68c.setRadius( 4.25 ) c.setPoint( 2, 2 )
cout ltlt "\n\nThe new location and radius of c
are\n" ltlt c ltlt "\nArea " ltlt c.area() ltlt
'\n' Point pRef c cout ltlt "\nCircle
printed as a Point is " ltlt pRef ltlt endl
return 0
69(No Transcript)
70Example Fig 9_10
71// Fig. 9.8 point2.h // Definition of class
Point ifndef POINT2_H define POINT2_H include
ltiostreamgt using stdostream class Point
friend ostream operatorltlt( ostream , const
Point ) public Point( int 0, int 0 )
// default constructor void setPoint( int,
int ) // set coordinates int getX()
const return x // get x coordinate int
getY() const return y // get y
coordinate protected // accessible to
derived classes int x, y // coordinates
of the point
72// Fig. 9.9 circle2.h Definition of class
Circle ifndef CIRCLE2_H define
CIRCLE2_H include ltiostreamgt using
stdostream include "point2.h" class Circle
public Point friend ostream operatorltlt(
ostream , const Circle ) public //
default constructor Circle( double r 0.0,
int x 0, int y 0 ) void setRadius( double
) // set radius double getRadius() const
// return radius double area() const
// calculate area protected //
accessible to derived classes double radius
// radius of the Circle
73// Fig. 9.10 cylindr2.h Definition of class
Cylinder ifndef CYLINDR2_H define
CYLINDR2_H include ltiostreamgt using
stdostream include "circle2.h" class Cylinder
public Circle friend ostream operatorltlt(
ostream , const Cylinder ) public
Cylinder( double h 0.0, double r 0.0,
int x 0, int y 0 ) void setHeight(
double ) // set height double getHeight()
const // return height double area() const
// calculate and return area double
volume() const // calculate and return
volume protected double height
// height of the Cylinder
74// Fig. 9.8 point2.cpp // Member functions for
class Point include "point2.h" // Constructor
for class Point PointPoint( int a, int b )
setPoint( a, b ) // Set the x and y
coordinates void PointsetPoint( int a, int b
) x a y b // Output the
Point ostream operatorltlt( ostream output, const
Point p ) output ltlt '' ltlt p.x ltlt ", " ltlt
p.y ltlt '' return output // enables
cascading
75// Fig. 9.9 circle2.h // Member function
definitions for class Circle include
ltiomanipgt using stdios using
stdsetiosflags using stdsetprecision inclu
de "circle2.h" // Constructor for Circle calls
constructor for Point // with a member
initializer and initializes radius CircleCircle(
double r, int a, int b ) Point( a, b )
// call base-class constructor setRadius( r )
76// Set radius void CirclesetRadius( double r )
radius ( r gt 0 ? r 0 ) // Get
radius double CirclegetRadius() const return
radius // Calculate area of Circle double
Circlearea() const return 3.14159 radius
radius // Output a circle in the form //
Center x, y Radius . ostream
operatorltlt( ostream output, const Circle c
) output ltlt "Center " ltlt static_castlt
Point gt( c ) ltlt " Radius "
ltlt setiosflags( iosfixed iosshowpoint )
ltlt setprecision( 2 ) ltlt c.radius
return output // enables cascaded calls
77// Fig. 9.10 cylindr2.cpp // Member and friend
function definitions // for class
Cylinder. include "cylindr2.h" // Cylinder
constructor calls Circle constructor CylinderCyl
inder( double h, double r, int x, int y )
Circle( r, x, y ) // call base-class
constructor setHeight( h ) // Set height of
Cylinder void CylindersetHeight( double h )
height ( h gt 0 ? h 0 ) // Get height
of Cylinder double CylindergetHeight() const
return height
78// Calculate area of Cylinder (i.e., surface
area) double Cylinderarea() const return 2
Circlearea() 2 3.14159 radius
height // Calculate volume of
Cylinder double Cylindervolume() const
return Circlearea() height // Output
Cylinder dimensions ostream operatorltlt( ostream
output, const Cylinder c ) output ltlt
static_castlt Circle gt( c ) ltlt " Height
" ltlt c.height return output // enables
cascaded calls
79// Fig. 9.10 fig09_10.cpp // Driver for class
Cylinder include ltiostreamgt using
stdcout using stdendl include
"point2.h" include "circle2.h" include
"cylindr2.h" int main() // create Cylinder
object Cylinder cyl( 5.7, 2.5, 12, 23 )
80// use get functions to display the Cylinder
cout ltlt "X coordinate is " ltlt cyl.getX()
ltlt "\nY coordinate is " ltlt cyl.getY() ltlt
"\nRadius is " ltlt cyl.getRadius() ltlt
"\nHeight is " ltlt cyl.getHeight() ltlt "\n\n"
// use set functions to change the Cylinder's
attributes cyl.setHeight( 10 )
cyl.setRadius( 4.25 ) cyl.setPoint( 2, 2 )
cout ltlt "The new location, radius, and height of
cyl are\n" ltlt cyl ltlt '\n' cout ltlt
"The area of cyl is\n" ltlt cyl.area() ltlt
'\n'
81 // display the Cylinder as a Point Point
pRef cyl // pRef "thinks" it is a Point
cout ltlt "\nCylinder printed as a Point is "
ltlt pRef ltlt "\n\n" // display the
Cylinder as a Circle Circle circleRef cyl
// circleRef thinks it is a Circle cout ltlt
"Cylinder printed as a Circle is\n" ltlt
circleRef ltlt "\nArea " ltlt
circleRef.area() ltlt endl return 0
82(No Transcript)
83Inheritance
- Multiple Inheritance derived class is derived
from two or more base classes. Very useful
capacity of C inheritance. - Example
84// Fig. 9.11 base1.h // Definition of class
Base1 ifndef BASE1_H define BASE1_H class
Base1 public Base1( int x ) value x
int getData() const return value
protected // accessible to derived
classes int value // inherited by derived
class endif
85// Fig. 9.11 base2.h // Definition of class
Base2 ifndef BASE2_H define BASE2_H class
Base2 public Base2( char c ) letter c
char getData() const return letter
protected // accessible to derived
classes char letter // inherited by derived
class endif
86// Fig. 9.11 derived.h // Definition of class
Derived which inherits // multiple base classes
(Base1 and Base2). ifndef DERIVED_H define
DERIVED_H include ltiostreamgt using
stdostream include "base1.h" include
"base2.h" // multiple inheritance class Derived
public Base1, public Base2 friend ostream
operatorltlt( ostream , const Derived )
87 public Derived( int, char, double )
double getReal() const private double real
// derived class's private data endif
88// Fig. 9.11 derived.cpp // Member function
definitions for class Derived include
"derived.h" // Constructor for Derived calls
constructors for // class Base1 and class
Base2. // Use member initializers to call
base-class constructors DerivedDerived( int i,
char c, double f ) Base1( i ), Base2( c ),
real ( f ) // Return the value of
real double DerivedgetReal() const return
real
89// Display all the data members of
Derived ostream operatorltlt( ostream output,
const Derived d ) output ltlt " Integer "
ltlt d.value ltlt "\n Character " ltlt
d.letter ltlt "\nReal number " ltlt
d.real return output // enables cascaded
calls
90// Fig. 9.11 fig09_11.cpp // Driver for multiple
inheritance example include ltiostreamgt using
stdcout using stdendl include
"base1.h" include "base2.h" include
"derived.h" int main() Base1 b1( 10 ),
base1Ptr 0 // create Base1 object Base2
b2( 'Z' ), base2Ptr 0 // create Base2 object
Derived d( 7, 'A', 3.5 ) // create
Derived object
91 // print data members of base class objects
cout ltlt "Object b1 contains integer " ltlt
b1.getData() ltlt "\nObject b2 contains
character " ltlt b2.getData() ltlt "\nObject
d contains\n" ltlt d ltlt "\n\n" // print data
members of derived class object // scope
resolution operator resolves getData ambiguity
cout ltlt "Data members of Derived can be"
ltlt " accessed individually" ltlt "\n
Integer " ltlt d.Base1getData() ltlt "\n
Character " ltlt d.Base2getData() ltlt
"\nReal number " ltlt d.getReal() ltlt "\n\n"
cout ltlt "Derived can be treated as an "
ltlt "object of either base class\n"
92 // treat Derived as a Base1 object
base1Ptr d cout ltlt "base1Ptr-gtgetData()
yields " ltlt base1Ptr-gtgetData() ltlt
'\n' // treat Derived as a Base2 object
base2Ptr d cout ltlt "base2Ptr-gtgetData()
yields " ltlt base2Ptr-gtgetData() ltlt
endl return 0
93(No Transcript)