C++ Inheritance PDF
C++ Inheritance PDF
Abstract: C++ strongly supports the concept of Reusability. The C++ classes can be reused in several ways. Once a class has
been written and tested, it can be adapted by another programmer to suit their requirements. This is basically done by
creating new classes, reusing the properties of the existing ones. The mechanism of deriving a new class from an old one is
called inheritance.
The old class is referred to as the base class and the new one is called the derived class or subclass. A derived class includes
all features of the generic base class and then adds qualities specific to the derived class.
In this paper we have studied the Inheritance concept and its types using C++ (oop).
Keywords: Reusability, Base class – Subclass, Private data member, Public data member and Types of Inheritance.
I. Introduction
Inheritance is the process by which objects of one class acquire the properties of objects of another class in the hierarchy.
Subclasses provide specialized behavior from the basis of common elements provided by the super class. Through the use of
inheritance, programmers can reuse the code in the super class many times.
Once a super class is written and debugged, it need not be touched again but at the same time can be adapted to work in
different situations. Reusing existing code saves time and money and increases a program’s reliability.
For example, the scooter is a type of the class two-wheelers, which is again a type of (or kind of) the class motor vehicles.
As shown in the below diagram the principle behind it is that the derived class shares common characteristics with the class
from which it is derived.
New classes can be built from the existing classes. It means that we can add additional features to an existing class without
modifying it. The new class is referred as derived class or subclass and the original class is known as base classes or super
class.
In this paper we have considered the following types of Inheritance:
I. Single Level Inheritance
II. Multiple Inheritance
III. Hierarchical inheritance
IV. Multilevel Inheritance
V. Hybrid Inheritance.
II. Single Level Inheritance
A derived class with only one base class is called single inheritance. Consider a simple example of single inheritance. In
this program show a base class B and derived class D. The class B contains one private data member, one public data member,
and three public member functions. The class D contains one private data members and two public member functions.
};
Void B :: get_ab()
{ a=5;b=10; }
Int B :: get_a()
{ return a;}
Void B :: show_a()
{ count<< “a=”<<a<< “\n” ;}
Void D :: mul()
{ c=b*get_a();}
Void D :: display()
{
Count<< “a=”<<get_a()
Count<< “b=”<<b
Count<< “c=”<<c
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}
© 2013, IJARCSMS All Rights Reserved ISSN: 2321-7782 12 | P a g e
Shivam International Journal of Advance Research in Computer Science and Management Studies
Volume 1, Issue 2, July 2013 pg. 10-21
III. Multiple Inheritance
A class can inherit properties from more than one class which is known as multiple inheritances.
This form of inheritance can have several super classes. A class can inherit the attributes of two or more classes as shown
below diagram.
Multiple inheritances allow us to combine the features of several existing classes as a starting point for defining new
classes. It is like a child inheriting the physical features of one parent and the intelligent if another.
#include <iostream.h>
Class M
{
Protected:
Int m;
Public :
Void get_m(int);
};
Class N
{
Protected:
Int n;
Public :
Void get_n(int);
};
Class P :public M,public N
{
Public :
Void display();
};
Void M :: get_m(int x)
{
M=x;
}
Void N::get_n(int y)
{
N=y;
}
Void P:: dis play()
{
Count<<”m=”<<m<<”\n”;
Count<<”n=”<<n<<”\n”;
Count<<”m*n=”<<m*n<<”\n”;
}
int main()
{
P p1;
P1.get_m(10);
P1.get_n(20);
P1.display();
Return 0;
When the properties of one class are inherited by more than one class, it is called hierarchical inheritance.
This form has one super class and many Subclasses. More than one class inherits the traits of one class. For example: bank
accounts.
class first
{
int x=10,y=20;
void display()
{
System.out.println("This is the method in class one");
System.out.println("Value of X= "+x);
System.out.println("Value of Y= "+y);
}
}
class Hier
{
public static void main(String args[])
{
two t1=new two();
three t2=new three();
t1.display();
t1.add();
t2.mul();
}
}
A class can be derived from another derived class which is known as multilevel inheritance.
Order of Constructor Calling in Multilevel Inheritance, when the object of a subclass is created the constructor of the
subclass is called which in turn calls constructor of its immediate super class.
For example, if we take a case of multilevel inheritance, where class B inherits from class A, and class C inherits from class
B, which show the order of constructor calling.
class A
{
A()
{
System.out.println("Constructor of Class A has been called");
}
}
class B extends A
{
B()
{
super();
System.out.println("Constructor of Class B has been called");
}
}
class C extends B
{
C()
{
super();
System.out.println("Constructor of Class C has been called");
}
}
class Constructor_Call
{
public static void main(String[] args)
{
System.out.println("------Welcome to Constructor call Demo------");
C objc = new C();
}
}
There could be situations where we need to apply two or more types of inheritance to design one inheritance called
hybrid inheritance.
For instance, consider the case of processing the student results, the weight age for sport is stored in separate classes.
VII. Conclusion
The mechanism of deriving a new class from an old class is called inheritance, it provides the concept of Reusability that is
the most important concept in C++. All types of inheritance with its own features and its use to provide users to reusability
concepts strongly, to give save time and reduce the complexity.
Here, in this paper we have to study the above five types of inheritance. We have to find that inheritance is central
concepts in C++ that allows deriving a class from multiple classes at a time.
References
Books:
1. E Balagurusamy, Object oriented Programming with C++, 6th Edition, New Delhi: Tata McGraw-Hill
Publishing Company Limited.
2. Yashavant Kanetkar, Test your C++ Skills, 1st Edition, BPB Publication.
3. Salivahanan S, Arivazhagan S – Digital Circuits and Design 4th Edition, Vikas Publishing House Pvt Ltd.
5. S. Geetha, D. Jeya Mala – Object Oriented Analysis and Design Using UML, 1st Edition, McGraw-Hill
Education.
6. Yashwant Kanetkar, Data Structures Through C++ 1st Edition, BPB Publication.
7. ISRD Group, Data Structures Through C++ 1st Edition, McGraw-Hill Education.
10. Nell B. Dale, Studyguide for C++ Plus Data Structure, Academic Internet Publishers