INHERITANCE
INHERITANCE
Introduction
Inheritance is one of the most useful and essential characteristics of
object-oriented programming. The existing classes are main
components inheritance. The new classes are created from existing
one. The properties of existing classes are simply extended to the new
classes. The new classes created using such methods are known as
derived classes and the existing classes are known as base classes.
The relationship between the base and derived class is known as kind
of relationship. The object of the derived class can access members
of base class as well as derived class. The object of base class can not
access the members of derived class.
-Reusability means reuse of properties of base class in the derived
class.
-The base class is called as super class, parent or ancestor and the
derived class as subclass, child or descendant.
-The procedure of creating a new class from one or more existing
classes is termed as inheritance.
Access Specifiers
-The public members of a class can be accessed by objects directly
outside the class without member function of the class.
-The private members of a class can be accessed by the public
member functions of the same class.
-The protected members of a class can be accessed in derived class
without member function of the same class.
Syntax for creating Derived class
Class derived_class name : Access specifier Base class_name
{
----------------------
---------------------- // Body of derived class
----------------------
};
-The derived class and base class are separated by colon(:) .The
access specifier may be private , public or protected. In the absence of
the access specifier, the default is private.
-The access specifiers decide whether the characteristics of the base
class are derived privately or publicly.
-The derived class also has own set of member data and member
functions.
Syntax for Types of declaration
(1) Class B : public A
{
// members of class B
};
(2) Class B : private A
{
//members of class B
};
(3) Class B : A //By default private
{
//members of class B
};
(4) Class B : protected A
{
//members of class B
};
(5) Struct B : A //By default public
{
//members of B
};
#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Public:
Int x;
};
Class B : public A //derived class
{
Public:
Int y;
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.x=20;
b.y=30;
cout<<”\n Member of A :”<<b.x;
cout<<”\n Member of B :”<<b.y;
return 0;
}
O/P : Member of A : 20
Member of B : 30
#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Private:
Int x;
Public:
A()
{
X=20;
}
Void show()
{
Cout<<”\n x = “ << x;
}
};
Class B : public A //derived class
{
Public:
Int y;
B()
{
Y=30;
}
Void show()
{
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30
#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Public:
Int x;
};
Class B : private A //derived class
{
Public:
Int y;
B()
{
X=20;
Y=30;
}
Void show()
{
Cout<<”\n x = “<<x;
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30
#include<iostream.h>
#includde<conio.h>
Class A //base class
{
Protected:
Int x;
};
Class B : private A //derived class
{
Int y;
Public:
B()
{
X=20;
Y=30;
}
Void show()
{
Cout<<”\n x = “<<x;
Cout<<”\n y = “<<y;
}
};
Int main()
{
Clrscr();
Bb; //object of derived class
b.show();
return 0;
}
O/P : x=20
Y=30
NOTES :
- All private members of the class are accessible to public
members of the same class. They can not be inherited.
- The derived class can access private members of the base class
using member function of the base class.
- All the protected members of the class are available to its
derived classes and can be accessed without the use of member
functions of base class
- If any class is prepared for deriving classes , it is advisable to
declare all members of base class as protected so that derived
classes can access the members directly .
- All the public members of the class are accessible toits derived
class . There is no restrictions for accessing elements.
- The access specifier required while deriving classes is either
private or public .If not specified , private is default for class and
public for structures.
- Constructors and destructors are declared in public section of
the class. If declared in private section the object declared will
not be initialized and compiler flag an error.
- The private , public and protected(visibility sections) can be
defined several times in any sequence.
Types of Inheritances
The class can be inherited from another class depending on the
following points:
1.Number of base classes: The program can use one or more base
classes to derive a single class.
2.Nested derivation : the derived class can be used as base class
and new class can be derived from it.
Depending on the above points inheritance is classified as
follows :
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Multi path Inheritance
Single Inheritance
When single class is derived from a single base class and the derived
class is not used as base class ,such type of inheritance is called as
Single Inheritance .
BASE CLASS
DERIVED CLASS
Multiple Inheritance
When two or more base classes are used to derive a single class is
called , is called as Multiple Inheritance.
DERIVED CLASS
#include<iostream.h>
#include<conio.h>
{
Class A
{
Protected :
Int a;
};
Class B
{
Protected :
Int b ;
};
Class C
{
Protected :
Int c;
};
Class D
{
Protected :
Int d;
};
Class E : public A ,B , C , D
{
Int e ;
Public :
Void getdata()
{
Cout<<”\n Enter value of a , b , c , d and e :” ;
Cin>>a>>b>>c>>d>>e ;
}
Void showdata()
{
Cout<<”\n a=”<<a<<”b=”<<b<<”c=”<<c
<< ”d=”<<d<<”e=”<<e ;
}
};
Int main()
{
Clrscr();
Ex;
x.getdata();
x.showdata();
}
Hierarchical Inheritance
When a single class is used for derivation of two or more classes , it is
known as Hierarchical Inheritance.
BASE CLASS
Multilevel Inheritance
When a class is derived from another derived class i.e. derived class
acts as a base , such type of inheritance is known as Multilevel
Inheritance.
BASE CLASS
DERIVED CLASS
CHILD CLASS
Name : Balaji
Age : 26
Sex: M
Height : 5 feet
Weight : 45kg
Hybrid Inheritance
The combination one or two type of inheritance is known as Hybrid
Inheritance.
BASE CLASS
DERIVED CLASS BASE CLASS
CHILD CLASS
O/P :
Enter following information
Name : Mahesh
Gender : M
Age : 25
Height : 5.2
Weight : 55
City : BBSR
Pincode : 751001
Game : Cricket
Entered Information
Name : Mahesh
Gender : M
Age : 25
Height : 5.2
Weight : 55
City : BBSR
Pincode : 751001
Game : Cricket
Multipath Inheritance
When a class is derived from two or more classes that are derived
from same base class , such type of inheritance is called Multipath
Inheritance.
BASE CLASS
CHILD CLASS
Multipath inheritance consists of many types of inheritances such as
multiple , multilevel and hierarchical .
Consider the following example :
Class A1
{
Protected :
Int a1;
};
Class A2:public A1
{
Protected :
Int a2;
};
Class A3 : public A1
{
Protected :
Int a3;
};
Class A4 : public A2 ,A3
{
Int a4;
};
In the above example , class A2 and A3 are derived from same base
class i.e. A1 by hierarchical inheritance. The class A1 and A2 both
access variable a1 of class A1. The class A4 is derived from class A2
and A3 by multiple inheritance. The derived class A4 has two sets of
data members of class A1, which creates ambiguity problem. If we try
to access variable a1 of class A1 in A4 class ,the compiler shows
error.
#include<iostream.h>
#include<conio.h>
Class A //base class
{
Public :
A()
{
Cout<<”\n zero-argument constructor of base class A” ;
}
~A()
{
Cout<<”\n Destructor of the class A “ ;
}
};
Class B //base class
{
Public :
B()
{
Cout<<”\n zero-argument constructor of base class B” ;
}
~B()
{
Cout<<”\n Destructor of the class B “ ;
}
};
Class C : public A, public B //derivation of class
{
Public :
C()
{
Cout<<”\n zero-argument constructor of base class C” ;
}
~C()
{
Cout<<”\n Destructor of the class C “ ;
}
};
Void main ()
{
Clrscr();
C ob1; //object declaration
}
O/P :
zero-argument constructor of base class A
zero-argument constructor of base class B
zero-argument constructor of base class C
Destructor of the class C
Destructor of the class B
Destructor of the class A
2)Base class with various constructors and Derived class with one
constructor
Program 13 :Write a program to declare multiple constructors in
base class and single constructor in derived class.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I()
{
Cout<<”\n zero-argument base class constructor” ;
}
I(int x)
{
Cout<<”\n one argument base class constructor “ ;
}
};
Class II : public I //base class
{
Int y;
Public :
II(int j)
{
Cout<<”\n one argument derived class constructor ” ;
Y=j;
}
};
Void main ()
{
Clrscr();
II i(2); //object declaration
}
O/P :
Zero-argument base class constructor
One argument derived class constructor
3)Base and Derived classes without default constructor
Program 14 : Write a program to declare base and derived class
without default constructor .
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I(int k)
{
X = k;
Cout<<”\n one-argument base class constructor” ;
}
};
Class II : public I //derived class
{
Int y;
Public :
II(int j) : I(j)
{
Cout<<”\n one-argument derived class constructor” ;
Y=j;
}
};
Void main ()
{
Clrscr();
II i(2); //object declaration
}
O/P :
One argument base class constructor
One argument derived class constructors
4)Constructors and Multiple Inheritance
Program 15 :Write a program to derive a class using multiple base
classes .Observe the execution of constructor when object of derived
class is declared .
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II : //base class
{
public :
II()
{
Cout<<”\n zero argument constructor of class II ” ;
}
};
Class III : public II , I
{
Public :
III()
{
Cout<<”\n zero argument constructor of base class III “ ;
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :
Zero argument constructor of base class II
Zero argument constructor of base class I
Zero argument constructor of base class III
5)Constructors in Multiple Inheritances with Explicit call
Program 16 : Write a program to derive a class using multiple base
classes . Invoke the constructors of base classes explicitly.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II : //base class
{
public :
II()
{
Cout<<”\n zero argument constructor of class II ” ;
}
};
Class III : public II , I
{
Public :
III() : II() , I()
{
Cout<<”\n zero argument constructor of base class III “ ;
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :
zero argument constructor of class II
zero argument constructor of class I
zero argument constructor of class III
6)Multiple Inheritance and Virtual Class
Program 17 :Write a program to derive a class using multiple base
classes. Invoke the constructors of base classes explicitly . Declare
any one base class as virtual.
#include<iostream.h>
#include<conio.h>
Class I //base class
{
Public :
Int x;
I()
{
Cout<<”\n zero-argument constructor of base class I” ;
}
};
Class II //base class
{
Public :
II()
{
Cout<<”\n Zero argument constructor of base class II ” ;
}
};
Class III : public II , virtual I //base class
{
Public :
III() : II() , I()
{
Cout<<”\n zero argument constructor of base class III ” ;
}
};
Void main ()
{
Clrscr();
III i; //object declaration
}
O/P :