Unit 5 Inheritance
Unit 5 Inheritance
Intro
Inheritance is the capability of class to derive properties and
characteristics from another class.
It is one of the important feature of OOP
New classes are created using the existing classes
The new class created is called “derived” class or “child” class
The existing class is known as “base” class or “parent” class
Derived class inherits the base class along with modification of its
own
For example: A Dog class can inherit Animal class. Here
Animal is base or parent class and Dog is child or derived
class.
Based on number of base classes with respect to derived
classes there are following types of inheritance:
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
Defining Derived Classes
A derived class can be defined by specifying its
relationship with the base class in addition to its own
implementation details.
Syntax :
class derived_class: visibility_mode base_class
{
// data members and member functions of derived_class
}
Examples
class Dog: private Animal{ private definition
// members of Dog
}
}
visibility_modes
1)Public visibility means “public members” of base class becomes “public
members” of derived class and hence they are accessible to the objects of the
derived class
2)Private visibility means “public members” of base class becomes “private
members” of the derived class and hence they can be accessed only by the
member functions
3)Protected visibility means it can be accessed within class and from any class
derived from this class.
Summary:
Private – visible to its own member function only
Protected – visible to member functions and derived class
Public – visible to all functions and objects
Overriding member function
We can define data member and member
function with same name in both base and
derived class
In this case, the function in derived class will be
executed which means derived class function
overrides the base class function
class Parent {
public:
void getData() { }
};
}
void main(){
Child c1;
c1.getData();
}
When the statement obj.getData() gets
executed, the function getData() of the derived
class executed. This means the derived class
function override the base class function.
The scope resolution operator (::) can be used
to access base class function through an object
of the derived class.
Types of inheritance
Single Inheritance
The derived class with only one base class is
called single inheritance.
Parent Base
Class
Child Derived
Class
Fig : Single Inheritance
Single Inheritance with
public access Specifier.
Single Inheritance with
private access Specifier.
Multiple Inheritance
The derived class with more than one base
class is called multiple inheritance.
Base Class Base Class Base Class Base Class
B1 B2 B3 Bn
D Derived Class
Syntax:
class derived:visibility base1,visibilityb2,..
{
// members of derived
}
Multilevel Inheritance
The derived class from base class flowing
through intermediate classes is called multilevel
inheritance.
A
C
Multilevel Inheritance
Example
Hierarchical inheritance
If base class inherits to more than one derived
classes then it is called hierarchical inheritance.
A
B C D
Hybrid Inheritance
The inheritance in which the derivation of a class
involves more than one form of any inheritance is
called hybrid inheritance.
Basically in C++ hybrid inheritance is
combination of two or more types of inheritance.
It can also be called multi path inheritance.
BaseClass
DerivedClass1 DerivedClass2
DerivedClass3
Hybrid Inheritance
In hybrid inheritance there are two or more
types of inheritance.
The attributes of base class into DerivedClass3
in the figure will inherit due to which
DerivedClass3 would have duplicate sets of
members
So to avoid the problem we use DerivedClass1
and DerivedClass2 as virtual base class as
follows :
class DerivedClass1 : virtual public BaseClass{
// members
};
B C
D
The Problem:
In the figure, both B and C inherit class A. They both
have copy of A
Class D inherits both B and C, hence D has two
copies of class A (one from B and one from C)
If we want to access data member of class A from D,
we need to specify the path from which data member
will be accessed(either from B or C).
In this case compiler cant differentiate between two
copies of class A in Class D.
There are two ways to avoid C++ ambiguity.
One is using virtual base class as described earlier in
hybrid inheritance
Another is using scope resolution operator
To use scope resolution operator, we can specify
members as:
object.B::display();
object.B::x = 10;