Module 4
Module 4
ii. The name of the derived class is to be given after the keyword class
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.
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
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
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.
When a derived class inherits only from one base class, it is known as single inheritance
When a derived class inherits from multiple base classes it is known as multiple inheritance
When more than one derived classes are created from a single base class , it is known as
Hierarchical 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>
{
private :
char name[20];
int rno;
public:
void acceptname()
{
cin>>rno>>name;
}
void displayname()
{
};
class exam : public student //derived class with single base class
public:
void acceptmark()
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
};
int main()
exam e1;
e1.acceptname();
e1.acceptmark();
e1.displayname();
e1.displaymark();
return 0;
Output
Roll no :-1201
Name :-KANNAN
Marks Obtained
Language.. 100
English .. 100
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
Multiple Inheritance
#include <iostream>
{
private :
char name[20];
int rno;
public:
void acceptname()
{
cin>>rno>>name;
}
void displayname()
};
{
int dd,mm,yy;
public:
void acceptdob()
cin>>dd>>mm>>yy>>cl;
void displaydob()
cout<<"\n class:-"<<cl;
};
public:
{
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
};
int main()
{
exam e1;
e1.acceptname();
e1.acceptdob();
e1.acceptmark();
e1.displayname();
e1.displaydob();
e1.displaymark();
return 0;
}
Output:
Roll no :-1201
Name :- MEENA
Marks Obtained
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Multilevel Inheritance
#include <iostream>
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
void displayname()
}};
public:
{
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6; }
void displaymark(){
};
public:
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
}
};
int main()
result r1;
r1.acceptname();
r1.acceptmark();
r1.displayname();
r1.displaymark();
r1.showresult();
return 0;
}
Output:
Roll no :-1201
Name :-SARATHI
Marks Obtained
Language... 96
English... 98
Physics... 100
Chemistry... 100
Comp.sci... 100
Maths... 100
Hierarchical inheritance
#include <iostream>
private :
char name[20];
int rno;
public:
void acceptname()
{
cin>>rno>>name;
void displayname()
};
public:
void acceptmark()
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
};
public:
void acceptmark()
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark()
}
};
int main()
qexam q1;
hexam h1;
q1.acceptname();
q1.acceptmark();
h1.acceptname();
h1.acceptmark();
q1.displayname();
h1.displaymark();
q1.displaymark();
return 0;
Output
95 96 100 98 100 99
Roll no :-1201
Name :-KANNAN
Language.. 95
English .. 96
Physics .. 100
Chemistry.. 98
Comp.sci.. 100
Maths .. 99
Roll no :-1201
Name :-KANNAN
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Hybrid inheritance
# include <iostream>
private :
char name[20];
int rno;
public:
void acceptname()
cin>>rno>>name;
void displayname()
};
public:
void acceptmark()
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
void displaymark() {
};
class detail
int dd,mm,yy;
char cl[4];
public:
void acceptdob()
cin>>dd>>mm>>yy>>cl;
void displaydob()
};
public:
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
}
};
int main()
result r1;
r1.acceptmark();
r1.acceptdob();
r1.displaydob();
r1.displaymark();
r1.showresult();
return 0;
}
Output:
MARKS STATEMENT
Roll no :-1201
Name :-RAGU
Marks Obtained
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
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.
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.
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.