Object Oriented Programming Inheritance
Object Oriented Programming Inheritance
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.
What is the purpose of Inheritance?
Specialization: Extending the functionality of an existing class
Generalization: sharing commonality between two or more
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 classs ancestors are those from which its own
super classes inherit
Descendant.
A classs descendants are those which inherit from its
sub classes.
3
Building
Commercial
Office
block
Public
Domestic
Office
block
Factory
Masjid
Apartment
Block
Hospital
4
Generalization
Building
Commercial
Office
block
Public
Domestic
Office
block
Factory
Masjid
Hospital
Apartment
Block
Specialization
5
Building
Commercial
Office
block
Public
Domestic
Office
block
Factory
Masjid
Apartment
Block
Hospital
6
Building
Commercial
Office
block
Factory
Public
A kind of Building
Domestic
Office
block
Apartment
Block
(AKO)
Masjid
Hospital
7
A kind of
Commercial building
Building
(AKO)
Commercial
Office
block
Public
Domestic
Office
block
Factory
Masjid
Apartment
Block
Hospital
8
Commercial
Office
block
Public
inherits from
Domestic
Office
block
Factory
Masjid
Apartment
Block
Hospital
9
Car
Car
Wheel
10
Building
Short Building
Tall
Building
11
Coloured Line
Attributes:
colour
Methods:
set colour
Specialisation
Extending the functionality of an existing class
eg a coloured line is a specialised kind of line
A class is both
closed in that it has an encapsulated, private part
which cannot be affected by external manipulation
and open in that it allows itself to be used as part of a
larger software unit.
13
Generalization
Sharing commonality between two or more classes
If we were modelling animals in a zoo would we
create a separate class for each animal type?
Cow
Whale
Elephant
Eagle
14
Generalization
Helpful to place common elements (attributes and
methods) in a shared base class and organize problem
into an inheritance hierarchy.
Animal
Mammal
Cow
Whale
Elephant
Bird
Eagle
15
Generalization
Sometimes this leads to the creation of abstract classes
which cant be instantiated directly
Abstract classes
Animal
Mammal
Cow
Whale
Elephant
Bird
Eagle
16
Generalization
concrete classes can be instantiated directly
Animal
Mammal
Bird
Concrete classes
Cow
Whale
Elephant
Eagle
17
(1/2)
// base class
// NOTE: not private
// count
// no-arg constructor
// 1-arg constructor
// return count
// incr count (prefix)
18
(2/2)
// derived class
// decr count (prefix)
// c1 of class CountDn
// Error
// display c1
// increment c1,3times
// display it
//decrement c1, twice
// display it
19
Counter C1;
C1.count = 5;
// Error
C1.get_count();
// OK
22
CountDn C2;
--C2 ;
// OK
C2.count = 10; // Error
23
24
(1/2)
// constructor, no args
25
(2/2)
// constructor, 1 arg
// decr count (prefix)
};
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
}
(1/3)
(2/3)
(3/3)
29
(1/3)
(2/3)
(3/3)
// ft and in
}
};
int main()
{
DistSign alpha;
alpha.getdist();
DistSign beta(11, 6.25);
DistSign gamma(100, 5.5, neg);
// no-arg constructor
// get alpha from user
// 2-arg constructor
// 3-arg constructor
// display all distances
cout << "\nalpha = "; alpha.showdist();
cout << "\nbeta = "; beta.showdist();
cout << "\ngamma = "; gamma.showdist();
cout << endl;
return 0;
}
32
33
(1/4)
(2/4)
(3/4)
<<
<<
<<
<<
<<
endl;
"\nEnter
"\nEnter
"\nEnter
"\nEnter
scientist s1;
data
data
data
data
(4/4)
laborer l1;
several employees
manager 1";
manager 2";
scientist 1";
laborer 1";
m1.putdata();
m2.putdata();
s1.putdata();
l1.putdata();
37
rectangle
circle
triangle
width
height
radius
height
rectangle ( )
draw ( )
circle ( )
draw ( )
triangle ( )
draw ( )
39
(1/5)
(2/5)
// 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
(3/5)
(4/5)
(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
tria tri(60, 7, 11, cGREEN, MEDIUM_FILL); //create triangle
cir.draw();
// draw all shapes
rec.draw();
tri.draw();
set_cursor_pos(1, 25);
// lower-left corner
return 0;
}
44
Access Combinations
(1/2)
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)
return 0;
46
}
Access Combinations
47
48
Levels of Inheritance
(1/4)
49
Levels of Inheritance
(2/4)
Levels of Inheritance
(3/4)
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
class A
// base class
{ };
class B
// base class
{ };
class C : public A, public B
{ };
A
B
// C is derived
// from A and B
53
(1/5)
(2/5)
56
(3/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
(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
(5/5)
(1/5)
(2/5)
(3/5)
62
(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
(5/5)
// constructor (6 args)
Lumber studs( "2x4", "const", 8, 0.0, 200, 4.45F );
}
64
66
70
71
72
(1/6)
(2/6)
75
(3/6)
};76
(4/6)
77
(5/6)
// laborer
// object of class employee
{ emp.getdata(); }
{ emp.putdata(); }
(6/6)
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
80
Chapter Summary
Chapter Summary
Inheritance permits the reusability of software: Derived classes can
extend the capabilities of base classes with no need to modifyor
even access the source code ofthe base class.
This leads to new flexibility in the software development process,
and to a wider range of roles for software developers.
82