Professional Documents
Culture Documents
Lecture 5 Inheritance
(C++)
CS162 Object Oriented Programming
Lecture adapted from Imran Ihsan
C++ How to Program (9th Edition) www.imranihsan.com
By Paul Deitel and Harvey Deitel
Prentice Hall, Inc. All rights reserved. OOP 05 - Inheritance imranihsan.com
+ 5.1 Introduction
Inheritance
Software reusability
1
5/14/2014
+ 5.1 Introduction
Class hierarchy
Direct base class
Inherited explicitly (one level up hierarchy)
Single inheritance
Inherits from one base class
Multiple inheritance
Inherits from multiple base classes
Base classes possibly unrelated
+ 5.1 Introduction
private
Alternative to composition
protected
Rarely used
2
5/14/2014
+ 5.1 Introduction
Abstraction
Focus on commonalities among objects in system
3
5/14/2014
Inheritance examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
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
4
5/14/2014
CommunityMember
Single
Employee Student Alumnus inheritance
Single
Faculty Staff inheritance
Multiple
AdministratorTeacher inheritance
Shape
TwoDimensionalShape ThreeDimensionalShape
5
5/14/2014
public inheritance
Specify with:
Class TwoDimensionalShape : public Shape
Class TwoDimensionalShape inherits from class Shape
friend functions
Not inherited
protected access
Intermediate level of protection between public and private
Derived-class members
Refer to public and protected members of base class
Simply use member names
6
5/14/2014
Point
x-y coordinate pair
Circle
x-y coordinate pair
Radius
7
5/14/2014
8
5/14/2014
X coordinate is 72
Y coordinate is 115
9
5/14/2014
10
5/14/2014
+ 46 // set radius
47 void Circle::setRadius( double radiusValue )
48 {
49 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
50
51 } // end function setRadius
52
53 // return radius
54 double Circle::getRadius() const
55 {
56 return radius;
57
58 } // end function getRadius
59
60 // calculate and return diameter
61 double Circle::getDiameter() const
62 {
63 return 2 * radius;
64
65 } // end function getDiameter
66
11
5/14/2014
12
5/14/2014
+ X coordinate is 37
Y coordinate is 43
Radius is 2.5
13
5/14/2014
+ 18 // set radius
19 void Circle2::setRadius( double radiusValue )
20 {
21 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
22
23 } // end function setRadius
24
25 // return radius
26 double Circle2::getRadius() const
27 {
28 return radius;
29
30 } // end function getRadius
31
32 // calculate and return diameter
33 double Circle2::getDiameter() const
34 {
35 return 2 * radius;
36
37 } // end function getDiameter
38
14
5/14/2014
Attempting to access
base class Points
private data members x
and y results in syntax
errors.
15
5/14/2014
16
5/14/2014
17
5/14/2014
+ 25 // return radius
26 double Circle3::getRadius() const
27 {
28 return radius;
29
30 } // end function getRadius
31
32 // calculate and return diameter
33 double Circle3::getDiameter() const
34 {
35 return 2 * radius;
36
37 } // end function getDiameter
38
39 // calculate and return circumference
40 double Circle3::getCircumference() const
41 {
42 return 3.14159 * getDiameter();
43
44 } // end function getCircumference
45
18
5/14/2014
19
5/14/2014
+ X coordinate is 37
Y coordinate is 43
Radius is 2.5
20
5/14/2014
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
21
5/14/2014
22
5/14/2014
23
5/14/2014
+ 24 // return radius
25 double Circle4::getRadius() const
26 {
27 return radius;
28
29 } // end function getRadius
30
31 // calculate and return diameter
32 double Circle4::getDiameter() const
33 {
34 return 2 * getRadius();
35
36 } // end function getDiameter
37
38 // calculate and return circumference
39 double Circle4::getCircumference() const
40 {
41 return 3.14159 * getDiameter();
42
43 } // end function getCircumference
44
24
5/14/2014
25
5/14/2014
+ X coordinate is 37
Y coordinate is 43
Radius is 2.5
26
5/14/2014
27
5/14/2014
28
5/14/2014
+ 30
31 // display new cylinder value
32 cout << "\n\nThe new location and radius of circle are\n";
33 cylinder.print();
34
35 // display floating-point values with 2 digits of precision
36 cout << fixed << setprecision( 2 );
37
38 // display cylinder's diameter
39 cout << "\n\nDiameter is " << cylinder.getDiameter();
40
41 // display cylinder's circumference
42 cout << "\nCircumference is "
43 << cylinder.getCircumference();
44
45 // display cylinder's area
46 cout << "\nArea is " << cylinder.getArea();
47
48 // display cylinder's volume
49 cout << "\nVolume is " << cylinder.getVolume();
50
51 cout << endl;
52
53 return 0; // indicates successful termination
54
55 } // end main
29
5/14/2014
+ X coordinate is 12
Y coordinate is 23
Radius is 2.5
Height is 5.7
Diameter is 8.50
Circumference is 26.70
Area is 380.53
Volume is 567.45
30
5/14/2014
31
5/14/2014
32
5/14/2014
+ 26
27 } // end Point4 destructor
28
29 // set x in coordinate pair
30 void Point4::setX( int xValue )
31 {
32 x = xValue; // no need for validation
33
34 } // end function setX
35
36 // return x from coordinate pair
37 int Point4::getX() const
38 {
39 return x;
40
41 } // end function getX
42
43 // set y in coordinate pair
44 void Point4::setY( int yValue )
45 {
46 y = yValue; // no need for validation
47
48 } // end function setY
49
33
5/14/2014
34
5/14/2014
+ 22 // destructor
23 Circle5::~Circle5()
24 {
25 cout << "Circle5 destructor: ";
26 print();
27 cout << endl;
28
29 } // end Circle5 destructor
30
31 // set radius
32 void Circle5::setRadius( double radiusValue )
33 {
34 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
35
36 } // end function setRadius
37
38 // return radius
39 double Circle5::getRadius() const
40 {
41 return radius;
42
43 } // end function getRadius
44
35
5/14/2014
36
5/14/2014
Uses a
Object uses another object
Call non-private member function
Using pointer, reference or object name
Knows a (association)
Object aware of another object
Contain pointer handle or reference handle
Knowledge networks
37
5/14/2014
has-a
Composition
Object contains one or more objects of other classes as
members
Example: Car has a steering wheel
38
5/14/2014
+ 1 // P9_09.H
2 #ifndef P9_09_H
3 #define P9_09_H
4
5 #include <iostream>
6 using std::ostream;
7
8 class Point {
9 friend ostream &operator<<( ostream &, const Point & );
10 public:
11 Point( double a = 0, double b = 0 ) { setPoint( a, b ); }
12 void setPoint( double, double );
13 void print( void ) const;
14 double getX( void ) const { return x; }
15 double getY( void ) const { return y; }
16 private:
17 double x, y;
18 };
19
20 #endif
39
5/14/2014
+ 21 // P9_09PM.cpp
22 // Member functions for class Point
23 #include <iostream>
24
25 using std::cout;
26 using std::ostream;
27
28 #include "p9_09.h"
29
30 void Point::setPoint( double a, double b )
31 {
32 x = a;
33 y = b;
34 }
35
36 ostream &operator<<( ostream &output, const Point &p )
37 {
38 p.print();
39 return output;
40 }
41
42 void Point::print( void ) const
43 { cout << '[' << getX() << ", " << getY() << ']'; }
+ 44 // P9_09C.H
45 #ifndef P9_09C_H
46 #define P9_09C_H
47 #include "p9_09.h"
48
49 class Circle {
50 friend ostream &operator<<( ostream &, const Circle & );
51 public:
52 Circle( double = 0.0, double = 0.0, double = 0.0 );
53 void setRadius( double r ) { radius = r; }
54 double getRadius( void ) const { return radius; }
55 double area( void ) const;
56 void print( void ) const;
57 private:
58 double radius;
59 Point pointObject;
60 };
61
62 #endif
40
5/14/2014
+ 63 // P9_09CM.cpp
64 // Member function definitions for class Circle
65 #include <iostream>
66
67 using std::cout;
68 using std::ios;
69
70 #include <iomanip>
71
72 using std::setprecision;
73 using std::setiosflags;
74 using std::resetiosflags;
75
76 #include "p9_09c.h"
77
78 Circle::Circle( double r, double a, double b ) : pointObject( a, b )
79 { setRadius( r ); }
80
81 double Circle::area( void ) const
82 { return 3.14159 * getRadius() * getRadius(); }
83
41
5/14/2014
+ 98 // P9_09CY.H
99 #ifndef P9_09CY_H
100 #define P9_09CY_H
101 #include "p9_09.h"
102 #include "p9_09c.h"
103
104 class Cylinder {
105 friend ostream& operator<<(ostream&, const Cylinder&);
106 public:
107 Cylinder(double = 0.0, double = 0.0, double = 0.0, double = 0.0);
108 void setHeight(double h) { height = h; }
109 double getHeight(void) const { return height; }
110 void print(void) const;
111 double area(void) const;
112 double volume(void) const;
113 private:
114 double height;
115 Circle circleObject;
116 };
117
118 #endif
+ 119 // P9_09CYM.cpp
120 // Member function definitions for class Cylinder.
121 #include <iostream>
122
123 using std::cout;
124 using std::ostream;
125
126 #include "p9_09cy.h"
127
128 Cylinder::Cylinder( double h, double r, double x, double y )
129 : circleObject( r, x, y ) { height = h; }
130
131 double Cylinder::area( void ) const
132 { return 2 * circleObject.area() + 2 * 3.14159 *
133 circleObject.getRadius() * getHeight(); }
134
135 ostream& operator<<( ostream &output, const Cylinder& c )
136 {
137 c.print();
138 return output;
139 }
140
141 double Cylinder::volume( void ) const
142 { return circleObject.area() * getHeight(); }
143
144 void Cylinder::print( void ) const
145 {
146 circleObject.print();
147 cout << "; Height = " << getHeight() << '\n';
148 }
42
5/14/2014
+ 149 // P9_09.cpp
150 #include <iostream>
151
152 using std::cout;
153 using std::endl;
154
155 #include "p9_09.h"
156 #include "p9_09c.h"
157 #include "p9_09cy.h"
158
159 int main()
160 {
161 Point p( 1.1, 8.5 );
162 Circle c( 2.0, 6.4, 9.8 );
163 Cylinder cyl( 5.7, 2.5, 1.2, 2.3 );
164
165 cout << "Point: " << p << "\nCircle: " << c
166 << "\nCylinder: " << cyl << endl;
167
168 return 0;
169 }
Example:
In a nutshell:
43