Derived To Base Class PDF
Derived To Base Class PDF
Chapter 9 D&D
Derived Classes
It is sometimes the case that we have a class
is nearly what we need.
Derived classes acquire the properties of an
existing class.
The original class is called the base class.
Inheritance
Inheritance
New classes created from existing classes
Derived class
Class that inherits data members and member functions from a
previously defined base class
Single inheritance
Class inherits from one base class
Multiple inheritance
Class inherits from multiple base classes
Types of inheritance
public: Derived objects are accessible by the base class objects
private: Derived objects are inaccessible by the base class
protected: Derived classes and friends can access protected
members of the base class
Derived classes
Student
GraduateStudent
UndergraduateStudent
Shape
Circle
Triangle
Rectangle
Loan
CarLoan
HomeImprovementLoan
MortgageLoan
Employee
FacultyMember
StaffMember
Account
CheckingAccount
SavingsAccount
protected Members
protected access
Intermediate level of protection between
public and private inheritance
Derived-class members can refer to public
and protected members of the base class
simply by using the member names
Note that protected data breaks
encapsulation
Derived Classes
A derived class inherits member functions
of base class.
A derived class can be used anywhere the
base class is expected.
Derived Classes
A derived class inherits member functions
of base class.
A derived class can be used anywhere the
base class is expected.
However, a base class CANNOT be used
anywhere the derived class is expected.
An Example
The following example:
Demonstrates the casting of base class pointers
to derived class pointers
Class Circle is derived from class Point
A pointer of type Point is used to reference a
Circle object, and a pointer to type Circle
is used to reference a Point object
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
y = b;
}
// Output Point (with overloaded stream insertion operator)
ostream &operator<<( ostream &output, const Point &p )
{
output << [ << p.x << ", " << p.y << ];
return output;
// enables cascaded calls
}
// Fig. 9.4: circle.h
// Definition of class Circle
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
using std::ostream;
#include <iomanip>
using std::ios;
using std::setiosflags;
using std::setprecision;
#include "point.h"
class Circle : public Point { // Circle inherits from Point
friend ostream &operator<<( ostream &, const Circle & );
public:
// default constructor
65
66
67
// set radius
68
// return radius
69
// calculate area
70 protected:
71
double radius;
72 };
73
74 #endif
75 // Fig. 9.4: circle.cpp
76 // Member function definitions for class Circle
77 #include "circle.h"
78
79 // Constructor for Circle calls constructor for Point
80 // with a member initializer then initializes radius.
81 Circle::Circle( double r, int a, int b )
82
: Point( a, b )
83 { setRadius( r ); }
84
85 // Set radius of Circle
86 void Circle::setRadius( double r )
87
{ radius = ( r >= 0 ? r : 0 ); }
88
122
123
124
cout << "Point p: " << p << "\nCircle c: " << c << \n;
125
126
127
pointPtr = &c;
128
129
130
131
132
133
134
135
136
137
138
139
pointPtr = &p;
140
141
142
143
144
145
146
147 }
Program
Output
Overriding Base-Class
Members in a Derived Class
To override a base-class member function
In the derived class, supply a new version of that
function with the same signature
same function name, different definition
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#endif
// Fig. 9.5: employ.cpp
// Member function definitions for class Employee
#include <iostream>
using std::cout;
#include <cstring>
#include <cassert>
#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.
Employee::Employee( const char *first, const char *last )
{
firstName = new char[ strlen( first ) + 1 ];
HourlyWorker inherits
from Employee.
54 #define HOURLY_H
55
56 #include "employ.h"
57
58 class HourlyWorker : public Employee {
59 public:
60
61
62
63 private:
10
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
double wage;
double hours;
};
#endif
// Fig. 9.5: hourly.cpp
// Member function definitions for class HourlyWorker
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::ios;
using std::setiosflags;
using std::setprecision;
#include "hourly.h"
// 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 HourlyWorkers pay
double HourlyWorker::getPay() const { return wage * hours; }
96
97 // Print the HourlyWorkers name and pay
98 void HourlyWorker::print() const
99 {
100
101
Employee::print();
102
103
104
105
106 }
107 // Fig. 9.5: fig09_05.cpp
108 // Overriding a base-class member function in a
109 // derived class.
110 #include "hourly.h"
111
112 int main()
113 {
114
115
h.print();
116
return 0;
117 }
HourlyWorker::print() is executing
Bob Smith is an hourly worker with pay of $400.00
11
Public
Type of inheritance
public
inheritance
public in derived class.
Can be accessed directly in
derived class by member or nonmember functions
protected
inheritance
private
inheritance
12
13
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
27
28
29
30
31
32
33
34 // Destructor for class Point
35 Point::~Point()
36 {
37
38
destructor:
"
39 }
40 // Fig. 9.7: circle2.h
41 // Definition of class Circle
42 #ifndef CIRCLE2_H
43 #define CIRCLE2_H
44
45 #include "point2.h"
46
47 class Circle : public Point {
48 public:
49
// default constructor
50
51
52
~Circle();
53 private:
54
double radius;
55 };
56
57 #endif
14
: Point( a, b )
70 {
71
radius = r;
72
73
// should validate
<< radius << " [" << x << ", " << y << ] << endl;
74 }
75
76 // Destructor for class Circle
77 Circle::~Circle()
78 {
79
80
radius is "
<< radius << " [" << x << ", " << y << ] << endl;
81 }
96
97
98
Point p( 11, 22 );
}
99
100
101
102
103
104
105
return 0;
106 }
15
Point
Point
destructor:
destructor:
destructor:
destructor:
radius is 10 [5, 5]
[5, 5]
radius is 4.5 [72, 29]
[72, 29]
Program
Output
16
Has a relationships
Composition
Relationship in which a class contains other classes
as members
17
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
27
28
29
30
31
32
18
33
y = b;
34 }
35
36 // Output the Point
37 ostream &operator<<( ostream &output, const Point &p )
38 {
39
output << [ << p.x << ", " << p.y << ];
40
41
return output;
// enables cascading
42 }
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
19
34
35 // Constructor for Circle calls constructor for Point
36 // with a member initializer and initializes radius
37 Circle::Circle( double r, int a, int b )
38
: Point( a, b )
39 { setRadius( r ); }
40
41 // Set radius
42 void Circle::setRadius( double r )
43
{ radius = ( r >= 0 ? r : 0 ); }
44
45 // Get radius
46 double Circle::getRadius() const { return radius; }
47
48 // Calculate area of Circle
49 double Circle::area() const
50
51
52 // Output a circle in the form:
53 // Center = [x, y]; Radius = #.##
54 ostream &operator<<( ostream &output, const Circle &c )
55 {
56
57
58
59
60
61
return output;
//
62 }
#ifndef CYLINDR2_H
#define CYLINDR2_H
5
6
#include <iostream>
7
8
using std::ostream;
9
10 #include "circle2.h"
11
12 class Cylinder : public Circle {
13
14
15 public:
16
// default constructor
17
18
int x = 0, int y = 0 );
19
20
// set height
21
// return height
22
23
24
25 protected:
26
double height;
27 };
28
29 #endif
20
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Circle::area() is
overidden.
61
62
63
64
return output;
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
X coordinate is 12
Y coordinate is 23
Radius is 2.5
Height is 5.7
21
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 }
cout << "The new location, radius, and height of cyl are:\n"
<< cyl << \n;
cout << "The area of cyl is:\n"
<< cyl.area() << \n;
// display the Cylinder as a Point
Point &pRef = cyl;
// pRef "thinks" it is a Point
cout << "\nCylinder printed as a Point is: "
<< pRef << "\n\n";
// display the Cylinder as a Circle
Circle &circleRef = cyl; // circleRef thinks it is a Circle
cout << "Cylinder printed as a Circle is:\n" << circleRef
<< "\nArea: " << circleRef.area() << endl;
return 0;
X coordinate is 12
Y coordinate is 23
Radius is 2.5
Height is 5.7
The new location, radius, and height of cyl are:
Center = [2, 2]; Radius = 4.25; Height = 10.00
The area of cyl is:
380.53
Cylinder printed as a Point is: [2, 2]
Cylinder printed as a Circle is:
Center = [2, 2]; Radius = 4.25
Area: 56.74
Multiple Inheritance
Multiple Inheritance
Derived-class inherits from multiple baseclasses
Encourages software reuse, but can create
ambiguities
22
#ifndef BASE1_H
#define BASE1_H
5
6
class Base1 {
public:
10 protected:
11
int value;
12 };
13
14 #endif
15
16
17
18
19
20
21
22
23
24
25
26
27
28
40 #include "base2.h"
41
42 // multiple inheritance
43 class Derived : public Base1, public Base2 {
44
45
46 public:
47
48
49
50 private:
51
double real;
52 };
53
54 #endif
23
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 #include "derived.h"
87
88 int main()
89 {
90
Base1 b1( 10 ), *base1Ptr = 0; // create Base1 object
91
Base2 b2( Z ), *base2Ptr = 0; // create Base2 object
92
Derived d( 7, A, 3.5 );
// create Derived object
93
94
// print data members of base class objects
95
cout << "Object b1 contains integer " << b1.getData()
96
<< "\nObject b2 contains character " << b2.getData()
97
<< "\nObject d contains:\n" << d << "\n\n";
98
99
// print data members of derived class object
100
// scope resolution operator resolves getData ambiguity
101
cout << "Data members of Derived can be"
102
<< " accessed individually:"
103
<< "\n
Integer: " << d.Base1::getData()
104
<< "\n Character: " << d.Base2::getData()
105
<< "\nReal number: " << d.getReal() << "\n\n";
106
107
cout << "Derived can be treated as an "
108
<< "object of either base class:\n";
109
110
// treat Derived as a Base1 object
111
base1Ptr = &d;
112
cout << "base1Ptr->getData() yields "
113
<< base1Ptr->getData() << \n;
114
115
// treat Derived as a Base2 object
116
base2Ptr = &d;
24
117
118
119
120
return 0;
121 }
25