Introduction to Inheritance
Inheritance is one of the most important features of Object Oriented Programming is
Inheritance. In object-oriented programming, inheritance enables new class and its objects to
take on the properties of the existing classes. A class that is used as the basis for inheritance
is called a superclass or base class. A class that inherits from a superclass is called a subclass
or derived class.
It is a process of creating new classes called derived classes, from the existing or base classes.
Inheritance allows us to inherit all the code (except declared as private) of one class to
another class. The class to be inherited is called base class or parent class and the class which
inherits the other class is called derived class or child class.
Derived Class and Base class
While defining a derived class, the derived class should identify the class from which it is
derived. The following points should be observed for defining the derived class.
i. The keyword class has to be used
ii. The name of the derived class is to be given after the keyword class
iii. A single colon
iv. The type of derivation (the visibility mode ), namely private, public or protected. If no
visibility mode is specified ,then by default the visibility mode is considered as private.
v. The names of all base classes(parent classes) separated by comma.
visibility mode
Visibility mode is used in the inheritance of C++ to show or relate how base classes are
viewed with respect to derived class. When one class gets inherited from another, visibility
mode is used to inherit all the public and protected members of the base class. Private
members never get inherited and hence do not take part in visibility.
Private visibility mode
When a base class is inherited with private visibility mode the public and protected members
of the base class become ‘private’ members of the derived class
protected visibility mode
When a base class is inherited with protected visibility mode the protected and public
members of the base class become ‘protected members ‘ of the derived class
public visibility mode
When a base class is inherited with public visibility mode , the protected members of the base
class will be inherited as protected members of the derived class and the public members of
the base class will be inherited as public members of the derived class.
Types of Inheritance
There are different types of inheritance viz., Single Inheritance, Multiple inheritance,
Multilevel inheritance, hybrid inheritance and hierarchical inheritance.
1. Single Inheritance
When a derived class inherits only from one base class, it is known as single inheritance
2. Multiple Inheritance
When a derived class inherits from multiple base classes it is known as multiple inheritance
3. Hierarchical inheritance
When more than one derived classes are created from a single base class , it is known as
Hierarchical inheritance.
4. Multilevel Inheritance
The transitive nature of inheritance is itself reflected by this form of inheritance. When a
class is derived from a class which is a derived class – then it is referred to as multilevel
inheritance.
5. Hybrid inheritance
When there is a combination of more than one type of inheritance, it is known as hybrid
inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or
Hierarchical and Multilevel inheritance or Hierarchical, Multilevel and Multiple inheritance.
Single Inheritance
Though the derived class inherits all the members of base class ,it has access
privilege only to non-private members of the base class .
# include <iostream>
using namespace std;
class student //base class
{
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
};
class exam : public student //derived class with single base class
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6,total;
void acceptmark()
cout<<"\n Enter lang,eng,phy,che,csc,mat marks.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
};
int main()
exam e1;
e1.acceptname();
e1.acceptmark();
e1.displayname();
e1.displaymark();
return 0;
Output
Enter roll no and name .. 1201 KANNAN
Enter lang,eng,phy,che,csc,mat marks.. 100 100 100 100 100 100
Roll no :-1201
Name :-KANNAN
Marks Obtained
Language.. 100
English .. 100
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
Multiple Inheritance
#include <iostream>
using namespace std;
class student //base class
{
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
}
void displayname()
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
};
class detail //Base class
{
int dd,mm,yy;
char cl[4];
public:
void acceptdob()
cout<<"\n Enter date,month,year in digits and class .. ";
cin>>dd>>mm>>yy>>cl;
void displaydob()
cout<<"\n class:-"<<cl;
cout<<"\t\t DOB : "<<dd<<” - “<<mm<<” –“ <<yy<<endl;
};
class exam : public student,public detail
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6,total;
void acceptmark()
{
cout<<"\n Enter lang,eng,phy,che,csc,mat marks.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
};
int main()
{
exam e1;
e1.acceptname();
e1.acceptdob();
e1.acceptmark();
e1.displayname();
e1.displaydob();
e1.displaymark();
return 0;
}
Output:
Enter roll no and name .. 1201 MEENA
Enter date,month,year in digits and class .. 7 12 2001 XII
Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100
Roll no :-1201
Name :- MEENA
class :-XII DOB : 7 - 12 -2001
Marks Obtained
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
Multilevel Inheritance
#include <iostream>
using namespace std;
class student //base class
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
void displayname()
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
}};
class exam : public student
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6;
void acceptmark()
{
cout<<"\n Enter lang,eng,phy,che,csc,mat marks.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6; }
void displaymark(){
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language... "<<mark1;
cout<<"\n English... "<<mark2;
cout<<"\n Physics... "<<mark3;
cout<<"\n Chemistry... "<<mark4;
cout<<"\n Comp.sci... "<<mark5;
cout<<"\n Maths... "<<mark6;
}
};
class result : public exam
int total;
public:
void showresult()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL MARK SCORED : "<<total;
}
};
int main()
result r1;
r1.acceptname();
r1.acceptmark();
r1.displayname();
r1.displaymark();
r1.showresult();
return 0;
}
Output:
Enter roll no and name .. 1201 SARATHI
Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100
Roll no :-1201
Name :-SARATHI
Marks Obtained
Language... 96
English... 98
Physics... 100
Chemistry... 100
Comp.sci... 100
Maths... 100
TOTAL MARK SCORED : 594
Hierarchical inheritance
#include <iostream>
using namespace std;
class student //base class
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
void displayname()
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
};
class qexam : public student
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6;
void acceptmark()
cout<<"\n Enter lang,eng,phy,che,csc,mat marks for quarterly exam.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
cout<<"\n\t\t Marks Obtained in quarterly";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
};
class hexam : public student
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6;
void acceptmark()
cout<<"\n Enter lang,eng,phy,che,csc,mat marks for halfyearly exam.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
cout<<"\n\t\t Marks Obtained in Halfyearly";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2; cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4; cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
}
};
int main()
qexam q1;
hexam h1;
q1.acceptname();
q1.acceptmark();
h1.acceptname();
h1.acceptmark();
q1.displayname();
h1.displaymark();
q1.displaymark();
return 0;
Output
Enter roll no and name .. 1201 KANNAN
Enter lang,eng,phy,che,csc,mat marks for quarterly exam..
95 96 100 98 100 99
Roll no :-1201
Name :-KANNAN
Marks Obtained in quarterly
Language.. 95
English .. 96
Physics .. 100
Chemistry.. 98
Comp.sci.. 100
Maths .. 99
Enter roll no and name .. 1201 KANNAN
Enter lang,eng,phy,che,csc,mat marks for halfyearly exam..
96 98 100 100 100 100
Roll no :-1201
Name :-KANNAN
Marks Obtained in Halfyearly
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
Hybrid inheritance
# include <iostream>
using namespace std;
class student //base class
private :
char name[20];
int rno;
public:
void acceptname()
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
void displayname()
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
};
class exam : public student
public:
int mark1, mark2 ,mark3,mark4,mark5,mark6;
void acceptmark()
cout<<"\n Enter lang,eng,phy,che,csc,mat marks.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark() {
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
};
class detail
int dd,mm,yy;
char cl[4];
public:
void acceptdob()
cout<<"\n Enter date,month,year in digits and class .. ";
cin>>dd>>mm>>yy>>cl;
void displaydob()
cout<<"\n class :-"<<cl;
cout<<"\t\t DOB : "<<dd<<" - "<<mm<<" -" <<yy<<endl;
};
class result : public exam,public detail
{
int total;
public:
void showresult()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL MARK SCORED : "<<total;
}
};
int main()
result r1;
r1.acceptname(); //calling base class function using derived class object
r1.acceptmark();
r1.acceptdob();
cout<<"\n\n\t\t MARKS STATEMENT";
r1.displayname(); //calling base class function using derived class object
r1.displaydob();
r1.displaymark();
r1.showresult();
return 0;
}
Output:
Enter roll no and name .. 1201 RAGU
Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100
Enter date,month,year in digits and class .. 7 12 2001 XII
MARKS STATEMENT
Roll no :-1201
Name :-RAGU
class :-XII DOB : 7 - 12 -2001
Marks Obtained
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
TOTAL MARK SCORED : 594
Virtual Base Class in C++
The data members/functions of class A are inherited twice to class D, as seen in the
diagram. The first will take you through class B, and the second will take you through class
C. When an object of class D accesses any data or function member of class A, it's unclear
which data or function member would be named. One was inherited via B, and it inherited the
other via C. This throws the compiler into a loop and causes an error to appear.
The duplication of inherited members can be avoided by making the common base class as
virtual base class.
virtual can be written before or after the public. Now only one copy of
data/function member will be copied to class C and class B and class A becomes the
virtual base class.
Virtual base classes offer a way to save space and avoid ambiguities in class
hierarchies that use multiple inheritances. When a base class is specified as a virtual
base, it can act as an indirect base more than once without duplication of its data
members. A single copy of its data members is shared by all the base classes that use
virtual base.
Pure Virtual Functions and Abstract
Classes in C++
Pure Virtual Functions in C++
A pure virtual function (or abstract function) in C++ is a virtual function
for which we don’t have an implementation, we only declare it. A pure
virtual function is declared by assigning 0 in the declaration. We can
write pure virtual functions in a class as below. Pure virtual function
should not have implementation in the class itself and all pure virtual
functions must be defined or have to be implemented in all derived
classes.
E.g. virtual void func() = 0;
Abstract Class
Sometimes implementation of all function cannot be provided in a base class
because we don’t know the implementation. Such a class is called abstract
class.
A class is abstract if it has at least one pure virtual function.
If we do not override the pure virtual function in derived class, then
derived class also becomes abstract class.
An abstract class can have constructors.
The object of abstract class cannot be created
Constructor in Derived Class
Whenever you create derived class object, first the base class default constructor is executed and
then the derived class’s constructor finishes execution.
In inheritance constructor of base class is inherited like other member functions. Object of
derived class, access the constructor of base class like normal functions.