Professional Documents
Culture Documents
C++ Inheritance
C++ Inheritance
Contents.
1)Introduction
2)Need for inheritance
3)Types of inheritance
4)Definition of derived and base classes.
5)Visibility modes
6)Inheritance and constructors and destructors.
7)Virtual base Classes
8)Nesting of classes
9)Relation between classes
Introduction.
B
C
C
Hybrid
Hierarchical
A C
A
B B
A B
B C D
C
C
E F G H I J
Types of inheritance.
X BASE CLASS
Y DERIVED CLASS
2) MULTIPLE INHERITANCE:
When a derived class is inherited from multiple
base classes is known as Multiple Inheritance
X Y
Z DERIVED CLASS
3) HIERARCHICAL INHERITANCE:
When many derived classes are inherited from
a single base class it is called Hierarchical
Inheritance
BASE CLASS
Z
W x Y
DERIVED CLASSES
4) MULTILEVEL INHERITANCE:
When a subclass or derived class is inherited
from another derived class is known as
Multilevel Inheritance
X Base class
of Y
Derived class
Y of X
Base class of Z
Z Derived class of Y
5) HYBRID INHERITANCE:
Hybrid Inheritance combines two or more forms of Inheritance.
i.e. Inheritance-When a subclass inherits
from multiple base classes and all of its
base classes inherit from a single base
class, this form of inheritance is called
hybrid inheritance
W
X Y Z
X Y
A
Z B C
a b
ACCESSIBILITY OF BASE CLASS MEMBERS.
PRIVATE Yes No No
Definition of derived and base classes.
The Visibility Mode controls the visibility and
availability of inherited base class members in the
derived class.
X private Y public
Y public Z protected
Z protected
2) Private visibility mode:The private derivation
means the derived class can access the public and
protected members of the class privately. i.e public
and protected members of the base class become private
members of the derived class. They cannot be inherited
further if the derived happens to be the base class of
any other class.
Class Super
{
Private: int x;void check(void);
Public: int y;void display(void);
Protected: int z; void getval(void);
};
Class Sub: Public Super
{
private: int s; void init(void);
pubilc:int b; readit(void);
Protected: int c; writeit(void);
};
3) Protected visibility mode: The protected derivation
of a class means that the derived class can access the
public and protected members of the base class
protectedly.i.e the public and protected members of
the base class become protected members of the derived
class.
Class Super
{
};
Class Sub: protected Super
{
};
Deriving publicly is a way of saying “ is a type of”.
=> Inheritance and the Base class
1. Member intended to be inherited and at the same
time intended to be available to every function ,
even to non-members ,should be declared as public
members in the base class.
2. Member intended to be inherited but not intended
to be public, should be declared as protected
members in the base class.
3. Member not intended to be inherited should be
declared as private members in the base class.
=> The significance of visibility mode:
• INHERITANCE AND CONSTRUCTORS AND
DESTRUCTORS:
When an object of a derived class is created,
the program first calls the constructor of base
class, then the constructor for the derived
class. This is because the constructor of derived
class may build upon data members from base
class; hence base class object has to be
constructed first. When an object of a derived
class expires , first the derived class destructor
is invoked , followed by the base class
destructor
.
class Super
{ .
. //same as before
};
Class Sub: public Super
{
};
int main()
{
Sub Ob1;
}
While Ob1 is created, first the constructor
Super:: Super( ) is invoked and then Sub::sub( ) is
invoked. Also when ob1 expires, at the main ( )
,firstly Sub::~Sub() is invoked and then Super::
~Super( ) is invoked.
Sometimes , the base class contractors require
arguments to construct their object.
In such situation, it is the responsibility of the
derived class constructor to pass those arguments
required by the corresponding base class.
Derived:: Derived(type x, type y,….): Base(x,y)
{
}
=>Passing Arguments through to a Base class
Constructor
Class Base
{
int a;
Float b;
Public: Base(int I, float j)
{
A=I;
B=j;
};
Class Derived: public Base
{
Public: Derived (int p,float q):Base(p, q)
{ }
}; Even if thederived
constructor does not need a
parameter for itself , yet
it accepts parameters for it
base class
Class Base
{
int a;
Float b;
Public: Base(int I, float j)
{
A=I;
B=j;
};
Class Derived: public Base
{
int x;
float y;
Public: Derived (int i,float j,int p,float q):Base(p,
q)
{ }
}; the derived constructor is
accepting parameters for
itself as well as for its
base class
Note:
1.The private members of the base class are
visible in the derived class but they are not
directly accessible.
int test;
Class Base {
int test;
public: void getit()
{ cin>>test;
}
};
Class Derived: public Base
{
public: void check()
{
test++;//not allow
};
Abstract class: A class that serves only as
a base class from which other classes
are derived , but no objects of this
base class type exist, is known as
Abstract class.
Making a private Member Inheritable:
(a). By making the visibility mode of
the private member as public.
(b). By making the visibility mode of
the private member as protected.
=>
Not Inheritance Private
Base class
Protected
Public
Private Private
Protected Protected
Public Public
Private
Protected
Public
Constructors in multiple inheritance:
The base classes are constructed in the
order in which they appear in the
declaration of the derived class.
The base class constructors are called
and executed before executing the
statements in the body of the derived
constructor.
VIRTUAL BASE CLASS: An element of
ambiguity can be introduced into a c++
program when multiple base classes are
inherited.
Base
D1 D2
D3
• #include<iostream.h>
void main()
Class Base{ public:int a
{
};
D3 ob;
Class D1:public Base
ob.a=25; //ambiguous
{
ob.b=50;
public: int b;
};
ob.c=75;
Class D2:public Base ob.total=ob.a+ob.b+ob.c;
{ public:int c; //ambiguous
}; cout<<ob.a<<”\t”<<ob.b<<“\t”<<
Class D3:public D1, public D2 ob.c<<“\t”<<ob.total<<“\n”;
{ }
public: int total;
};
1.Solved the above problem using scope resolution
operator.
void main()
{
D3 ob;
Ob.D1::a=25; //SCOPE resolved , used D1’s a
ob.b=50;
ob.c=75;
ob.total=ob.D1::a+ob.b+ob.c;
cout<<ob.D1::a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”;
}
2.Solved the above problem using virtual Base class.
The dreaded diamond refers to a class structure in which
a particular class appears more than once in a class’s
inheritance hierarchy.
Base
D1 D2
D3
virtual D1 virtual D2
D3
#include<iostream.h>
void main()
Class Base{ public:int a
{ clrscr();
};
D3 ob;
Class D1:virtual public Base
ob.a=25; //Now unambiguous
{
ob.b=50;
public: int b;
};
ob.c=75;
Class D2:virtual public Base ob.total=ob.a+ob.b+ob.c;
{ public:int c; //unambiguous
}; cout<<ob.a<<”\t”<<ob.b<<“\t”<
Class D3:public D1, public D2 <ob.c<<“\t”<<ob.total<<“\n”;
{ }
public: int total;
};
OVERRIDING AND HIDING A BASE CLASS
METHOD IN A DERIVED CLASS