Unit 3 (Topic 2)
Unit 3 (Topic 2)
Unit – 3 (Topic – 2)
Inheritance
Prepared By
Prof. Shah Brijesh
Inheritance
Inheritance:
-> Reusability is another important feature of OOP.
-> The reuse of a class that has already been tested, debugged and
used many times can save us the effort of developing and testing
the same again.
-> C++ also 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
other programmers 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 (Derivation).
-> The old class is referred to as the base class(super class or
parent class) and the new one is called the derived class (sub class
or child class).
Inheritance
Inheritance:
Defined Derived Classes:
-> A derived class can be defined by specifying its relationship with the base class in addition to its
own details.
-> The general form of defining a derived class is:
B
D
-> Example - 1: (Public Derivation)
class A
{
int a;
public:
void getA(); void putA(); };
Inheritance
Inheritance:
1. Single Inheritance:
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
class B : public A
{
int b;
public:
void getB();
void putB();
};
Inheritance
Inheritance:
1. Single Inheritance:
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
void main()
{
A x;
B y;
clrscr();
x.getA();
x.getB();
y.getA();
y.getB();
Inheritance
Inheritance:
1. Single Inheritance:
x.putA();
x.putB();
y.putA();
y.putB();
}
class A B : public A
private a b
public getA() getB()
putA() putB()
getA()
putA()
Note : -> Here we derived class A(Base class) into class B publicly .
-> So, public members of class A(base class) are also public
members of class B (Derived class)
-> Therefore with the help of object of class B we can access
public members of class A also.
Inheritance
Inheritance:
1. Single Inheritance:
-> Example - 2: (Private Derivation)
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : private A
{
int b;
public:
void getB();
void putB(); };
void B :: getB()
{
cout<<“Enter b:”;
cin>>b; }
void B :: putB()
{
cout<<“\n b = “<<b; }
void main()
{
A x; B y;
x.getA(); x.getB();
y.getA(); y.getB();
Inheritance
Inheritance:
1. Single Inheritance:
x.putA();
x.putB();
y.putA();
y.putB();
}
class A B : public A
private a b
getA()
putA()
public getA() getB()
putA() putB()
Note : -> Here we derived class A(Base class) into class B privately.
-> So, public members of class A(base class) are private
members of class B (Derived class)
-> Therefore we can not access public members of class A
with the help of class B.
Inheritance
Inheritance:
2. Multiple Inheritance:
-> A class can inherit the attribute of two or more classes as show in below fig.
B-1 B-2 B-3
Note : -> Here we derived class A and B(Base class) into class C publicly.
-> So, public members of class A and B (base class) are public members of class C (Derived
class)
-> Therefore we can access public members of class A and B with the help of class C.
Inheritance
Inheritance:
3. Hierarchical Inheritance:
-> The traits (members) of one class may be inherited by
more than one class.
-> This process is known as hierarchical inheritance.
-> Below figure shows the hierarchical inheritance
Note : -> Here we derived class A (Base class) into class B and C publicly.
-> So, public members of class A are public members of class B and class C
-> Therefore we can access public members of class A with the help of class B.
-> as well as public members of class A with the help of class C
-> but we can not access members of class B from object of class C and members of class C
from objects of class B.
Inheritance
Inheritance:
4. Multi Level Inheritance:
-> The mechanism of deriving a class from another derived class is known as
multilevel inheritance.
-> Below figure shows the multi level inheritance.
-> The class A serves as a base class for the derived class B, which in turn serves as
a base class for the derived class C.
-> The class B is known as intermediate base class since it provides a link for the
inheritance between A and C.
-> The chain ABC is known as inheritance path.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public B
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
void main()
{
C x;
x.getA();
x.getB();
x.getC();
x.putA();
x.putB();
x.putC();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C : public B c getC(), putC(), getA(), putA(), getB(), putB()
Note : -> Here we derived class A into class B publicly & derived class C from class B Publicly.
-> So, We can access public members of class A from class B and public members of class A
& B From class C.
Inheritance
Inheritance:
5. Hybrid Inheritance:
-> Combination of Two or more types of inheritance (Multiple Inheritance,
Hierarchical Inheritance & Multi Level Inheritance) is known as hybrid inheritance.
-> Below figure shows the Hybrid inheritance.
B C
-> The class A serves as a base class for the derived class B, which in turn serves as
a base class for the derived class D & The class C is also serve as a base class for
the derived class D.
-> Here we combine multilevel inheritance and multiple inheritance.
-> Hybrid inheritance is also known as multi path ineritance.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
class D : public B, public C
{
int d;
public:
void getD();
void putD();
};
void D :: getD()
{
cout<<“Enter d:”;
cin>>d;
}
void D :: putD()
{
cout<<“\n d = “<<d;
}
Inheritance
Inheritance:
void main()
{
D x;
x.getA();
x.getB();
x.getC();
x.getD();
x.putA();
x.putB();
x.putC();
x.putD();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C c getC(), putC()
D : public B, public C d getD(), putD(), , getA(), putA(), getB(), putB()
getC(), putC()
Note : -> Here we derived class A into class B publicly & derived class C from class B and D Publicly.
-> So, We can access public members of class A, class B and class C from class D.
Inheritance
Inheritance:
Virtual Base Class:
-> Consider a situation where all the three kinds of inheritance (multiple, multi
level and hierarchical) are involved.
-> This is illustrated in below figure.
Grand Parent
Parent 1 Parent 2
Child
-> The ‘child’ has two direct base classes ‘parent1’ and ‘parent2’ which themselves
have a common base class ‘grandparent’.
-> The ‘child’ inherits the traits of ‘grandparent’ via two separate paths.
-> It can also inherit directly as shown by the broken line.
-> The ‘grandparent’ is sometimes referred to as indirect base class.
-> Inheritance by the ‘child’ as shown in above fig. Might pose some problems.
-> All the public and protected members of ‘grandparent’ are inherited into ‘child’
twice, first via ‘parent1’ and again via ‘parent2’.
Inheritance
Inheritance:
Virtual Base Class:
-> This means, ‘child’ would have duplicate sets of the members inherited from
‘grandparent’.
-> This introduces ambiguity.
-> The duplication of inherited members due to these multiple paths can be
avoided by making the common base class (ancestor class) as virtual base class
while declaring the direct or intermediate base classes which is shown as follows:
class A class B1 : virtual public A
{ {
……………. ……………….
}; };
class B2 : public virtual A class D : public B1, public B2
{ {
……………. ……………….
}; };
-> When a class is made a virtual base class, C++ takes necessary care to see that
only one copy of that class is inherited, regardless of how many inheritance paths
exist between the virtual base class and derived class.
Note : The keywords virtual and public may be used in either order.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : virtual public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public virtual A
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
class D : public B, public C
{
int d;
public:
void getD();
void putD();
};
void D :: getD()
{
cout<<“Enter d:”;
cin>>d;
}
void D :: putD()
{
cout<<“\n d = “<<d;
}
Inheritance
Inheritance:
void main()
{
D x;
x.getA();
x.getB();
x.getC();
x.getD();
x.putA();
x.putB();
x.putC();
x.putD();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C c getC(), putC()
D : public B, public C d getD(), putD(), , getA(), putA(), getB(), putB()
getC(), putC()
Note : -> Here we derived class A into class B and class C publicly & derived class B and class C into
class D Publicly.
-> So, We can access public members of class A, class B and class C from class D.
Inheritance
Inheritance:
Making a private member inheritable:
-> We know that a private member of a base class cannot be inherited and therefore it is not
available for the derived class directly.
-> To inherit private members of a base class C++ provides a third visibility modifier, protected,
which serve a limited purpose in inheritance.
-> A member declared as protected is accessible by the member functions within its class and any
class immediately derived from it.
-> It cannot be accessed by the functions outside these two classes.
-> A class can now use all the three visibility modes as illustrated below:
class A
{
private:
…………….
…………….
protected:
…………….
…………….
public:
…………….
…………….
};
-> When a protected member is inherited in public mode, it becomes protected in the derived class
too and therefore is accessible by the member functions of the derived class.
Inheritance
Inheritance:
Making a private member inheritable:
-> It is also ready for further inheritance.
-> A protected member, inherited in the private mode derivation,
becomes private in the derived class.
-> Although it is not available for further inheritance
-> It is also possible to inherit a base class in protected mode.
-> In protected derivation, both the public and protected members of the
base class become protected members of the derived class.
-> Below table summarizes how the visibility of base class members
undergoes modifications in all the three types of derivation.