Ch09 Inheritance
Ch09 Inheritance
Programming
1
What is Inheritance?
Inheritance is the process of creating new classes, called derived
classes, from existing or base classes.
The derived class inherits all the capabilities of the base class but
can add more features into itself.
Classes can inherit attributes (data members) and methods
(member functions) from other classes.
classes.
Improved efficiency and greater robustness
2
Terminology
Derived class or subclass or child class.
A class which inherits some of its attributes and methods
from another class
Base class or superclass or parent class.
A class from which another class inherits
Ancestor.
A class’s ancestors are those from which its own
super classes inherit
Descendant.
A class’s descendants are those which inherit from its
sub classes.
3
Terminology - a classification Hierarchy
Building
Masjid Hospital
4
Terminology - a classification Hierarchy
Generalization
Building
5
Terminology - a classification Hierarchy
Generalized
Building ‘base class’
Masjid Hospital
6
Terminology - a classification Hierarchy
Building
Masjid Hospital
7
Terminology - a classification Hierarchy
A ‘kind of’
Commercial building Building
(AKO)
Masjid Hospital
8
Terminology - a classification Hierarchy
Arrow in diagram
means
Building ’inherits from’
Masjid Hospital
9
Designing your classification hierarchy:
‘A kind of’ or ‘a part of’?
Vehicle
A car is ‘a kind of’ vehicle
Car
A wheel isn’t ‘a kind of’ car.
A wheel is ‘a part of’ a car
Wheel
10
Designing your classification hierarchy:
Different classes or different states?
Need to analyze whether differences between objects
are dependant on type (such as a house being different
to a factory) or state. (different values of the class’s
attributes)
might vary only in the value of
Building the height attribute - don’t need
separate classes
Tall
Short Building
Building
11
What do objects inherit?
Line
Attributes: A ‘coloured line’ is a kind of line
start position the coloured line class inherits all
the attributes and methods of
end position
the line class and adds attributes
Methods: and methods of its own
draw
A class is both
closed in that it has an encapsulated, private part
13
Generalization
Sharing commonality between two or more classes
If we were modelling animals in a zoo would we
14
Generalization
Helpful to place common elements (attributes and methods)
in a shared base class and organize problem into an
inheritance hierarchy.
Animal
Mammal Bird
15
Generalization
Sometimes this leads to the creation of abstract classes
which can’t be instantiated directly
Mammal Bird
16
Generalization
concrete classes can be instantiated directly
Animal
Mammal Bird
Concrete classes
17
Derived Class and Base Class (1/2)
// inheritance with Counter class
#include <iostream>
using namespace std;
class Counter{ // base class
protected: // NOTE: not private
unsigned int count; // count
public:
Counter() : count(0)
{ } // no-arg
constructor
Counter(int c) : count(c)
{ } // 1-arg constructor
unsigned int get_count() const // return count
{ return count; }
Counter operator ++ () // incr count
(prefix)
{ return Counter(++count); }
};
18
Derived Class and Base Class (2/2)
class CountDn: public Counter{ // derived class
public:
Counter operator -- () // decr count
(prefix)
{ return Counter(--count); }
};
int main(){
CountDn c1; // c1 of class CountDn
// CountDn c2(100); // Error
20
Generalization in UML Class Diagrams
In the UML, inheritance is called
generalization, because the parent
class is a more general form of the
child class.
The child is more specific version of
the parent.
In figure, the direction of the arrow
emphasizes that the derived class
refers to functions and data in the
base class, while the base class has
no access to the derived class.
21
Counter C1;
C1.count = 5;
// Error
C1.get_count();
// OK
22
CountDn C2;
--C2 ; // OK
C2.count = 10; // Error
23
24
Derived Class Constructors (1/2)
// constructors in derived class
#include <iostream>
using namespace std;
class Counter{
protected: // NOTE: not private
unsigned int count; // count
public:
Counter() : count() {} // constructor, no args
Counter(int c) : count(c) {} // constructor, one arg
unsigned int get_count() const // return count
{ return count; }
Counter operator ++ () // incr count (prefix)
{ return Counter(++count); }
};
class CountDn : public Counter{
public: CountDn() : Counter() {} // constructor, no
args
25
Derived Class Constructors (2/2)
CountDn(int c) : Counter(c) {} // constructor, 1 arg
CountDn operator -- () // decr count
(prefix)
{ return CountDn(--count); }
};
int main(){
CountDn c1; CountDn c2(100);
cout << "\nc1=" << c1.get_count(); // display
cout << "\nc2=" << c2.get_count(); // display
++c1; ++c1; ++c1; // increment c1
cout << "\nc1=" << c1.get_count(); // display it
--c2; --c2; // decrement c2
cout << "\nc2=" << c2.get_count(); // display it
CountDn c3 = --c2; // = calls 1 arg
constructor
cout << "\nc3=" << c3.get_count(); // display c3
cout << endl;
return 0;
26
}
Overriding Member Functions (1/3)
// overloading functions in base and derived classes
#include <iostream>
using namespace std;
#include <process.h> // for exit()
class Stack
{
protected: // NOTE: can’t be private
enum { MAX = 3 }; // size of stack array
int st[MAX]; // stack: array of integers
int top; // index to top of stack
public:
Stack() { top = -1; } // constructor
void push(int var) // put number on stack
{ st[++top] = var; }
int pop() // take number off stack
{ return st[top--]; }
};
27
Overriding Member Functions (2/3)
class Stack2 : public Stack{
public:
void push(int var) // put number on stack
{
if(top >= MAX-1) // error if stack full
{ cout << "\nError: stack is full";
exit(1);
}
Stack::push(var); // call push() in Stack
class
}
int pop() // take number off stack
{
if(top < 0) // error if stack empty
{ cout << "\nError: stack is empty\n"; exit(1); }
return Stack::pop(); // call pop() in Stack
class
}
28
};
Overriding Member Functions (3/3)
int main()
{
Stack2 s1;
s1.push(11); // push some values onto stack
s1.push(22);
s1.push(33);
cout << endl << s1.pop(); // pop some values from stack
cout << endl << s1.pop();
cout << endl << s1.pop();
cout << endl << s1.pop(); // oops, popped one too many...
cout << endl;
return 0;
}
29
Inheritance in the English Distance Class (1/3)
// inheritance using English Distances
#include <iostream>
using namespace std;
enum posneg { pos, neg }; // for sign in DistSign
class Distance{ // English Distance class
protected: // NOTE: can’t be private
int feet; float inches;
public:
Distance() : feet(0), inches(0.0) { } // no-arg constructor
Distance(int ft, float in) : feet(ft), inches(in) { }
void getdist(){ // get length from
user
cout << "\nEnter feet: " ; cin >> feet;
cout << "Enter inches: " ; cin >> inches;
}
void showdist() const // display distance
{ cout << feet << "\'-" << inches << '\"'; }
}; 30
Inheritance in the English Distance Class (2/3)
class DistSign : public Distance{ // adds sign to Distance
private: posneg sign; // sign is pos or neg
public: // no-arg constructor
DistSign() : Distance() // call base constructor
{ sign = pos; } // set the sign to +
// 2- or 3-arg
constructor
DistSign(int ft, float in, posneg sg=pos) :
Distance(ft, in) // call base constructor
{ sign = sg; } // set the sign
void getdist(){ // get length from user
Distance::getdist(); // call base getdist()
char ch; // get sign from user
cout << "Enter sign (+ or -): "; cin >> ch;
sign = (ch=='+') ? pos : neg;}
void showdist() const{ // display distance
cout << ( (sign==pos) ? "(+)" : "(-)" ); // show
sign 31
Inheritance in the English Distance Class (3/3)
Distance::showdist(); // ft and in
}
};
int main()
{
DistSign alpha; // no-arg constructor
alpha.getdist(); // get alpha from user
DistSign beta(11, 6.25); // 2-arg constructor
DistSign gamma(100, 5.5, neg); // 3-arg constructor
// display all
distances
cout << "\nalpha = "; alpha.showdist();
cout << "\nbeta = "; beta.showdist();
cout << "\ngamma = "; gamma.showdist();
cout << endl;
return 0;
}
32
Class Hierarchies (UML Diagram)
33
Class Hierarchies ( Employee Database ) (1/4)
// models employee database using inheritance
#include <iostream>
using namespace std;
const int LEN = 80; // maximum length of names
class employee { // employee class
private:
char name[LEN]; // employee name
unsigned long number; // employee number
public:
void getdata(){
cout << "\n Enter last name: "; cin >> name;
cout << " Enter number: "; cin >> number;
}
void putdata() const{
cout << "\n Name: " << name;
cout << "\n Number: " << number;
} };
34
Class Hierarchies ( Employee Database ) (2/4)
class manager : public employee{ // management class
private:
char title[LEN]; // "vice-president" etc.
double dues; // golf club dues
public:
void getdata(){
employee::getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
}
void putdata() const{
employee::putdata();
cout << "\n Title: " << title;
cout << "\n Golf club dues: " << dues;
}
};
35
Class Hierarchies ( Employee Database ) (3/4)
class scientist : public employee{ // scientist class
private:
int pubs; // number of
publications
public:
void getdata()
{
employee::getdata();
cout << " Enter number of pubs: "; cin >> pubs;
}
void putdata() const
{
employee::putdata();
cout << "\n Number of publications: " << pubs;
}
};
class laborer : public employee // laborer class
{ }; 36
Class Hierarchies ( Employee Database ) (4/4)
int main(){
manager m1, m2; scientist s1; laborer l1;
39
Inheritance and Graphics Shapes (1/5)
// multshap.cpp balls, rects, and polygons
#include "msoftcon.h" // for graphics functions
class shape{ // base class
protected:
int xCo, yCo; // coordinates of shape
color fillcolor; // color
fstyle fillstyle; // fill pattern
public: // no-arg constructor
shape() : xCo(0), yCo(0), fillcolor(cWHITE),
fillstyle(SOLID_FILL) { }
shape(int x, int y, color fc, fstyle fs) :
xCo(x), yCo(y), fillcolor(fc), fillstyle(fs) { }
void draw() const{ // set color and fill style
set_color(fillcolor);
set_fill_style(fillstyle);
}
};
40
Inheritance and Graphics Shapes (2/5)
class circle : public shape
{
private:
int radius; // (xCo, yCo) is center
public:
circle() : shape() // no-arg constr
{ }
// 5-arg constructor
circle(int x, int y, int r, color fc, fstyle fs)
: shape(x, y, fc, fs), radius(r)
{ }
void draw() const // draw the circle
{
shape::draw();
draw_circle(xCo, yCo, radius);
}
};
41
Inheritance and Graphics Shapes (3/5)
class rect : public shape
{
private:
int width, height; // (xCo, yCo) is upper-left corner
public:
rect() : shape(), height(0), width(0) // no-arg ctor
{ } // 6-arg ctor
rect(int x, int y, int h, int w, color fc, fstyle fs) :
shape(x, y, fc, fs), height(h), width(w)
{ }
void draw() const // draw the rectangle
{
shape::draw();
draw_rectangle(xCo, yCo, xCo+width, yCo+height);
set_color(cWHITE); // draw diagonal
draw_line(xCo, yCo, xCo+width, yCo+height);
} };
42
Inheritance and Graphics Shapes (4/5)
class tria : public shape
{
private:
int height; // (xCo, yCo) is tip of pyramid
public:
tria() : shape(), height(0) // no-arg
constructor
{ } // 5-arg constructor
tria(int x, int y, int h, color fc, fstyle fs) :
shape(x, y, fc, fs), height(h)
{ }
void draw() const // draw the triangle
{
shape::draw();
draw_pyramid(xCo, yCo, height);
}
};
43
Inheritance and Graphics Shapes (5/5)
int main()
{
init_graphics(); // initialize graphics
system
circle cir(40, 12, 5, cBLUE, X_FILL); // create circle
rect rec(12, 7, 10, 15, cRED, SOLID_FILL);
// create rectangle
44
Access Combinations (1/2)
// tests publicly- and privately-derived classes
#include <iostream>
using namespace std;
class A{ // base class
private: int privdataA; // (functions have the same
access
protected: int protdataA; // rules as the data shown here)
public: int pubdataA;
};
class B : public A{ // publicly-derived class
public:
void funct(){
int a;
a = privdataA; // error: not accessible
a = protdataA; // OK
a = pubdataA; // OK
}
}; 45
Access Combinations (2/2)
class C : private A{ // privately-derived class
public:
void funct(){
int a;
a = privdataA; // error: not accessible
a = protdataA; /* OK */ a = pubdataA; // OK
}
};
int main(){
int a; B objB; a = objB.privdataA; // error: not accessible
a = objB.protdataA; // error: not
accessible
a = objB.pubdataA; // OK (A public to B)
C objC; a = objC.privdataA; // error: not
accessible
a = objC.protdataA; // error: not
accessible
a = objC.pubdataA; // error: not accessible (A private to C)
46
return 0;
Access Combinations
47
Levels of Inheritance (UML Diagram)
48
Levels of Inheritance (1/4)
// multiple levels of inheritance
#include <iostream>
using namespace std;
const int LEN = 80;
class employee{
private:
char name[LEN]; // employee name
unsigned long number; // employee number
public:
void getdata(){
cout << "\n Enter last name: "; cin >> name;
cout << " Enter number: "; cin >> number; }
void putdata() const{
cout << "\n Name: " << name;
cout << "\n Number: " << number;
}
};
49
Levels of Inheritance (2/4)
class scientist : public employee // scientist class
{
private:
int pubs; // number of publications
public:
void getdata()
{
employee::getdata();
cout << " Enter number of pubs: "; cin >> pubs;
}
void putdata() const
{
employee::putdata();
cout << "\n Number of publications: " << pubs;
}
};
50
Levels of Inheritance (3/4)
class laborer : public employee // laborer class
{
};
class foreman : public laborer // foreman class
{
private:
float quotas; // percent of quotas met
successfully
public:
void getdata(){
laborer::getdata();
cout << " Enter quotas: "; cin >> quotas;
}
void putdata() const{
laborer::putdata();
cout << "\n Quotas: " << quotas;
}
}; 51
Levels of Inheritance (4/4)
int main(){
laborer l1;
foreman f1;
cout << endl;
cout << "\nEnter data for laborer 1";
l1.getdata();
cout << "\nEnter data for foreman 1";
f1.getdata();
cout << endl;
cout << "\nData on laborer 1";
l1.putdata();
cout << "\nData on foreman 1";
f1.putdata();
cout << endl;
return 0;
}
52
Multiple Inheritance (UML Diagram)
A class can be derived from more than
one base class. This is called multiple
inheritance. Figure shows how this
looks when a class C is derived from
base classes A and B.
In programming we write as
class employee
{ };
class manager :
private employee, private student
{ };
class scientist :
private employee, private student
{ };
54
Member Functions in Multiple Inheritance (1/5)
}
};
class manager : private employee, private student{
private:
char title[LEN]; // "vice-president" etc.
double dues; // golf club dues
public:
void getdata(){
employee::getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
student::getedu();
}
void putdata() const{
employee::putdata();
cout << "\n Title: " << title;
cout << "\n Golf club dues: " << dues;
57
Member Functions in Multiple Inheritance (4/5)
student::putedu();
}
};
class scientist : private employee, private student{
private: int pubs; // number of publications
public:
void getdata(){
employee::getdata();
cout << " Enter number of pubs: "; cin >> pubs;
student::getedu();
}
void putdata() const{
employee::putdata();
cout << "\n Number of publications: " << pubs;
student::putedu();
}
};
58
Member Functions in Multiple Inheritance (5/5)
int main(){
manager m1; scientist s1, s2; laborer l1;
cout << endl;
cout << "\nEnter data for manager 1"; // get data for
m1.getdata(); //several employees
cout << "\nEnter data for scientist 1"; s1.getdata();
cout << "\nEnter data for scientist 2"; s2.getdata();
cout << "\nEnter data for laborer 1"; l1.getdata();
cout << "\nData on manager 1"; //display data for
m1.putdata(); //several employees
cout << "\nData on scientist 1"; s1.putdata();
cout << "\nData on scientist 2"; s2.putdata();
cout << "\nData on laborer 1"; l1.putdata();
cout << endl; return 0;
}
59
Constructors in Multiple Inheritance (1/5)
// multiple inheritance with English Distances
#include <iostream>
#include <string>
using namespace std;
class Type{ // type of lumber
private:
string dimensions; string grade;
public:
Type() : dimensions("N/A"), grade("N/A")
{ } // no-arg constructor
Type(string di, string gr) : dimensions(di), grade(gr)
{ } // 2-arg constructor
void gettype(){ // get type from
user
cout << " Enter nominal dimensions (2x4 etc.): ";
cin >> dimensions;
cout << " Enter grade (rough, const, etc.): ";
cin >> grade; } 60
Constructors in Multiple Inheritance (2/5)
void showtype() const{ // display
type
cout << "\n Dimensions: " << dimensions;
cout << "\n Grade: " << grade;
}
};
class Distance{ // English Distance class
private: int feet; float inches;
public: // no-arg constructor
Distance() : feet(0), inches(0.0)
{ } // constructor (two
args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist(){ // get length from user
cout << " Enter feet: "; cin >> feet;
cout << " Enter inches: "; cin >> inches;
}
61
Constructors in Multiple Inheritance (3/5)
void showdist() const // display distance
{ cout << feet << "\'-" << inches << '\"'; }
};
class Lumber : public Type, public Distance {
private:
int quantity; // number of pieces
double price; // price of each piece
public: // constructor (no args)
Lumber() : Type(), Distance(), quantity(0), price(0.0)
{ } // constructor (6 args)
Lumber( string di, string gr, // args for Type
int ft, float in, // args for Distance
int qu, float prc ) : // args for our data
Type(di, gr), // call Type ctor
Distance(ft, in), // call Distance ctor
quantity(qu), price(prc) // initialize our data
{ }
62
Constructors in Multiple Inheritance (4/5)
void getlumber()
{
Type::gettype();
Distance::getdist();
cout << " Enter quantity: "; cin >>
quantity;
cout << " Enter price per piece: "; cin >> price;
}
void showlumber() const
{
Type::showtype();
cout << "\n Length: ";
Distance::showdist();
cout << "\n Price for " << quantity
<< " pieces: $" << price * quantity;
}
};
63
Constructors in Multiple Inheritance (5/5)
int main()
{
Lumber siding; // constructor (no args)
// constructor (6 args)
Lumber studs( "2x4", "const", 8, 0.0, 200, 4.45F );
64
Ambiguity in Multiple Inheritance
Two base classes have functions with the same name, while a
class derived from both base classes has no function with this
name.
How do objects of the derived class access the correct base
class function?
The name of the function alone is insufficient, since the
compiler can’t figure out which of the two functions is meant.
Next program demonstrates this situation.
Another kind of ambiguity arises if you derive a class from two
classes that are each derived from the same class.
This creates a diamond-shaped inheritance tree.
The DIAMOND program shows how this looks
65
Ambiguity in Multiple Inheritance
// demonstrates ambiguity in multiple inheritance
#include <iostream>
using namespace std;
class A{
public: void show() { cout << "Class A\n"; }
};
class B{
public: void show() { cout << "Class B\n"; }
};
class C : public A, public B { };
int main() {
C objC; // object of class C
// objC.show(); //ambiguous--will not compile
objC.A::show(); //OK
objC.B::show(); //OK
return 0;
}
66
67
Ambiguity in Multiple Inheritance
//diamond.cpp investigates diamond-shaped multiple inheritance
#include <iostream>
using namespace std;
class A { public: void func() {cout << "Hello World \n" ; } };
class B : public A { };
class C : public A { };
class D : public B, public C { };
int main(){
D objD;
objD.func(); // ambiguous: won’t compile
return 0;
}
68
69
Aggregation: Classes Within Classes
If a class B is derived by inheritance from a class A, we can say
that “B is a kind of A.”
This is because B has all the characteristics of A, and in addition
some of its own.
It’s like saying that a car is a kind of vehicle : A car has the
characteristics shared by all vehicles (wheels, seats, and so on) but
has some distinctive characteristics of its own (CNG, EFI,
automatic etc).
For this reason inheritance is often called a “kind of” relationship.
Aggregation is called a “has a” relationship. We say a library has a
book or an invoice has an item line.
Aggregation is also called a “part-whole” relationship: the book is
a part of the library.
70
Aggregation: Classes Within Classes
In object-oriented programming, aggregation may occur when one
object is an attribute of another. Here’s a case where an object of
class A is an attribute of class B:
class A { };
class B { A objA; /* define objA as an object of class A */ };
In the UML, aggregation is considered a special kind of
association.
Sometimes it’s hard to tell when an association is also an
aggregation.
It’s always safe to call a relationship an association, but if class A
contains objects of class B, and is organizationally superior to
class B, it’s a good candidate for aggregation.
A company might have an aggregation of employees, or a stamp
collection might have an aggregation of stamps. 71
Aggregation: Classes Within Classes
Aggregation is shown in the same way as association in UML
class diagrams, except that the “whole” end of the association line
has an open diamond-shaped arrowhead.
72
Aggregation in the employee program
Let’s rearrange the EMPMULT program to use aggregation
instead of inheritance.
In EMPMULT the manager and scientist classes are derived from
the employee and student classes using the inheritance
relationship.
In our new program, EMPCONT, the manager and scientist
classes contain instances of the employee and student classes as
attributes.
This aggregation relationship is shown in next figure.
73
Aggregation in the employee program
stu.putedu(); } }; 78
Using Aggregation in the Program (4/6)
class scientist{ // scientist
private:
int pubs; // number of publications
employee emp; // object of class employee
student stu; // object of class student
public:
void getdata(){
emp.getdata();
cout << " Enter number of pubs: "; cin >> pubs;
stu.getedu();
}
void putdata() const{
emp.putdata();
cout << "\n Number of publications: " << pubs;
stu.putedu();
}
};
79
Using Aggregation in the Program (5/6)
class laborer { // laborer
private: employee emp; // object of class employee
public:
void getdata() { emp.getdata(); }
void putdata() const { emp.putdata(); }
};
int main(){
manager m1; scientist s1, s2; laborer l1;
cout << endl;
cout << "\nEnter data for manager 1"; //get data for
m1.getdata(); //several employees
cout << "\nEnter data for scientist 1";
s1.getdata();
cout << "\nEnter data for scientist 2";
s2.getdata();
cout << "\nEnter data for laborer 1";
l1.getdata();
80
Using Aggregation in the Program (6/6)
cout << "\nData on manager 1"; // display data for
m1.putdata(); // several employees
81
Composition: A Stronger Aggregation
Composition is a stronger form of aggregation. It has all the
characteristics of aggregation, plus two more:
The part may belong to only one whole.
The lifetime of the part is the same as the lifetime of the whole.
A car is composed of doors (among other things). The doors can’t
belong to some other car, and they are born and die along with the
car.
A room is composed of a floor, ceiling, and walls.
Aggregation is a “has a” relationship, composition is a “consists
of” relationship.
In UML diagrams, composition is shown in the same way as
aggregation, except that the diamond-shaped arrowhead is solid
instead of open.
82
Composition: A Stronger Aggregation
83
Chapter Summary
A class, called the derived class, can inherit the features of another
class, called the base class.
The derived class can add other features of its own, so it becomes a
specialized version of the base class.
Inheritance provides a powerful way to extend the capabilities of
existing classes, and to design programs using hierarchical
relationships.
Accessibility of base class members from derived classes and from
objects of derived classes is an important issue.
Data or functions in the base class that are prefaced by the keyword
protected can be accessed from derived classes but not by any other
objects, including objects of derived classes.
Classes may be publicly or privately derived from base classes.
Objects of a publicly derived class can access public
84
Chapter Summary
members of the base class, while objects of a privately derived
class cannot.
A class can be derived from more than one base class. This is called
multiple inheritance. A class can also be contained within another
class.
In the UML, inheritance is called generalization. This relationship
is represented in class diagrams by an open triangle pointing to the
base (parent) class.
Aggregation is a “has a” or “part-whole” relationship: one class
contains objects of another class.
Aggregation is represented in UML class diagrams by an open
diamond pointing to the “whole” part of the part-whole pair.
Composition is a strong form of aggregation. Its arrowhead is
85
Chapter Summary
solid rather than open.
Inheritance permits the reusability of software: Derived classes can
extend the capabilities of base classes with no need to modify—or
even access the source code of—the base class.
This leads to new flexibility in the software development process,
and to a wider range of roles for software developers.
86