3. Introduction
Title: Inheritance
Class: Master Computer Science (MCS)
Subject: OOP using C++
Present by Zubair Farooq
Date 20 August, 2015
22/08/2015Hadziq Fabroyir - Informatics ITS 3
4. Objectives
• Inheritance
• Advantages of Inheritance
• Why we use Inheritance?
• FORMS OF INHERITANCE
• Defining a Base Class
• Defining a Derived Class
• Visibility Modes
• Inheritance Program example
22/08/2015Hadziq Fabroyir - Informatics ITS 4
6. Inheritance
The existing class is called
the parent, super, or base
class.
The derived class is called a
child or subclass.
22/08/2015Zubair Farooq (MCS) 6
7. Inheritance
• The child inherits
characteristics of the
parent.
• The child has special
rights to the parents
methods and data.
•The child has its own
unique behaviors and
data.
22/08/2015Zubair Farooq (MCS) 7
8. Inheritance
Inheritance relationships are
often shown graphically in a
class diagram with the arrow
pointing to the parent class.
Inheritance should create an
is-arelationship, meaning
the child is a more specific
version of the parent.
22/08/2015Zubair Farooq (MCS) 8
Animal
Bird
9. Advantages of Inheritance?
Sometimes we need to repeat the code or we need
repeat the whole class properties. So It helps in various
ways.
It saves memory space.
It saves time.
It increases reliability of the code.
It saves the developing and testing efforts.
22/08/2015Zubair Farooq (MCS) 9
10. Why we use Inheritance?
To increase the reusability of the code and to make
further usable for another classes. We use the
concept of inheritance.
22/08/2015Zubair Farooq (MCS) 10
11. FORMS OF INHERITANCE
Single Inheritance:
It is the inheritance hierarchy wherein one derived class inherits from one base
class.
Multiple Inheritance:
It is the inheritance hierarchy wherein one derived class inherits from multiple
base classes.
Hierarchical Inheritance:
It is the inheritance hierarchy wherein multiple subclasses inherits from one base
class.
Multilevel Inheritance:
It is the inheritance hierarchy wherein subclass acts as a base class for other
classes.
Hybrid Inheritance:
The inheritance hierarchy that reflects any legal combination of other four types
of inheritance.
22/08/2015Zubair Farooq (MCS) 11
12. FORMS OF INHERITANCE
22/08/2015Zubair Farooq (MCS) 12
C# and Java support only Single inheritance, meaning that a
derived class can have only one parent class.
13. Defining a Base Class
Base class has general features common to all the derived
classes and derived class (apart from having all the
features of the base class) adds specific features.
class Base-class
{
... ... ...
…….//Members of base class
};
22/08/2015Zubair Farooq (MCS) 13
14. Defining a Derived Class
The general form of deriving a subclass from a base class is as
follows
Class derived-class-name : visibility-mode base-class-name
{
…… //
…….// members of the derived class
};
The visibility-mode is optional.
This visibility mode specifies how the features of base class are
visible to the derived class.
22/08/2015Zubair Farooq (MCS) 14
16. Public Inheritance
class A : public B
{ // Class A now inherits the members of Class B
// with no change in the “access specifier” for
} // the inherited members
22/08/2015Zubair Farooq (MCS) 16
17. Protected Inheritance
class A : protected B
{ // Class A now inherits the members of Class B
// with public members “promoted” to protected
} // but no other changes to the inherited members
22/08/2015Zubair Farooq (MCS) 17
18. Private Inheritance
class A : private B
{ // Class A now inherits the members of Class B
// with public and protected members
} // “promoted” to private
22/08/2015Zubair Farooq (MCS) 18
20. Execution of base class
constructor
When both derived and base class contains
constructors, the base constructor is executed first
and then the constructor in the derived class is
executed.
22/08/2015Zubair Farooq (MCS) 20
21. Program Statement
Imagine a publishing company that markets both book and
audiocassette versions of its works.
Create a class publication that stores the title (a string) and price
(type float) of a publication. From this class derive two classes:
book, which adds a page count (type int), and tape, which adds a
playing time in minutes (type float). Each of these three classes
should have a getdata() function to get its data from the user at the
keyboard, and a putdata() function to display its data. Write a
main() program to test the book and tape classes by creating
instances of them, asking the user to fill in data with getdata(), and
then displaying the data with putdata().
Reference: Object-Oriented Programming in C++ (4th Edition) by Robert Lafore(CH-9 Ex Question #1.)
22/08/2015Zubair Farooq (MCS) 21
22. Base Class
classpublication{
char title[20];
float price;
public:
void getdata( ){
cout<<"Please enter thetitleand price of Book : ";
cin>>title>>price;
}
void putdata( ){
cout<<"Book Title : "<<title<<endl;
cout<<"Price: "<<price<<endl;
}
};
22/08/2015Zubair Farooq (MCS) 22
23. SubClass
class book : publication{
int pages;
public:
void getdata(){
publication::getdata();
cout<<"ENter the number of pages : ";
cin>>pages;
}
Void putdata () {
publication::putdata();
cout<<"Pages : "<<pages<<endl;
}
};
22/08/2015Zubair Farooq (MCS) 23
24. SubClass
class tape : publication{
float time;
public:
void getdata(){
publication::getdata();
cout<<"Enter time play in minute : ";
cin>>time;
}
void putdata(){
publication::putdata();
cout<<"Playing time : "<<time;
}
};
22/08/2015Zubair Farooq (MCS) 24
26. Feel Free to ask any
Questions.
22/08/2015Zubair Farooq (MCS) 26
Editor's Notes
The child inherits characteristics of the parent.
Methods and data defined for the parent class.
The child has special rights to the parents methods and data.
Public access like any one else
Protected access available only to child classes (and their descendants).
The child has its own unique behaviors and data.