4. INTRODUCTION
INHERITANCE - An essential characteristics of object oriented programming.
Existing classes are main components.
New classes are created from existing class.
i.e. properties of existing class are extended to new class.
Existing classes are called BASE CLASS.
Newley created classes are called DERIVED CLASS.
Definition : The procedure of creating a new class from one or more existing
classes is termed as inheritance.
Relationship between base and derived class is known as KIND OF
RELATIONSHIP.
Accessibility : Object of derived class can access members of base class as well as
derived class, but object of base class cannot access members of derived classes.
4
6. REUSABILITY :It means the reuse of properties of base class in the derived
class.
Reusability is achieved using inheritance.
Base class is also called as : super class, parent class or ancestor class
Derived class is also called as sub class, child class or descendent class.
A class can be derived for more than one class and also it is possible to
derive a class from previously derived class.
6
7. 7
MEMBER A
MEMBER B
MEMBER A
MEMBER B
MEMBER B
MEMBER C
BASE
CLASS
DERIVED CLASS
DERIVED CLASS
MEMBER A
MEMBER C
MEMBER D
8. ACCESS SPECIFIERS AND INHERITANCE
Access specifiers : public, private and protected.
Public: object can access the public members of the class
Private: object cannot access the private members of the class
Protected: only object of derived class can access the protected members of the
base class
Syntax :
class_name of derived class : access specifier name of base_class
{
…………
………….
………….
};
8
9. e.g.
(1) Class B : public A
{
//members of class B
};
Here class A – base class, class B --- derived
class which is derived publicly.
Object of class B can access all the public
members of class A.
(2) Class B : private A //private derivation
{
//members of class B
};
Here class A – base class, class B --- derived
class which is derived privately.
(3) Class B : A //by default private
derivation
{
//members of class B
};
Here class A – base class, class B --- derived class
which is derived privately.
(4) Class B : protected A //protected
derivation
{
//members of class B
};
Here class A – base class, class B --- derived class
which is derived protected.
9
10. When class is derived publicly, then all the public members of the base class can be
accessed directly in the derived class and it cannot access the private members of
the base class.
10
PUBLIC INHERITANCE
Public members
Private members
OBJECT
Base class Derived class
11. PUBLIC INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : public A //derived class
{
public :
int y;
};
Void main()
{
B b;
b.x = 20;
b.y = 30;
cout<<“n Member of A : “<<b.x;
cout<<“n Member of B : “<<b.y;
}
Output :
Member of A :20
Member of B :30
11
12. Object of privately derived class cannot directly access the public members of base
class
Thus member functions are used to access members
12
PRIVATE INHERITANCE
Public members
OBJECT
Base class Derived class
Public members
functions
Private members
13. PRIVATE INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : private A //derived class
{
public :
int y;
B( ) //constructor
{
x = 20;
y = 40;
}
void show( )
{ cout<<“n X= “<<x;
cout<< “n Y= “<<y;
}
};
Void main()
{
B b; //object creation
b . show( );
}
Output :
X =20
Y=40
13
14. Object of privately derived class cannot directly access the public members of base
class
Thus member functions are used to access members
14
PROTECTED INHERITANCE
Public members
OBJECT
Base class Derived class
Public members
functions
Private members
15. PROTECTED INHERITANCE
e.g.
Class A
{
public:
int x;
};
Class B : private A //derived class
{
public :
int y;
B( ) //constructor
{
x = 20;
y = 40;
}
void show( )
{ cout<<“n X= “<<x;
cout<< “n Y= “<<y;
}
};
Void main()
{
B b; //object creation
b . show( );
}
Output :
X =20
Y=40
15
17. SINGLE INHERITANCE
Only one class is derived from a single class
Further the derived class is not used as a base class.
Newley created class receives entire characteristics of base class.
17
Class ABC Base class
Class abc Derived class
18. class Vehicle //base class
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Car : public Vehicle //derived
{
public:
void print()
{
cout<< "This is a Car";
}
};
18
Void main()
{
Car obj;
obj.show();
obj.print();
}
OUTPUT:
This is a Vehicle
This is a Car
Class Vehicle
Class Car
19. MULTIPLE INHERITANCE
A class is derived from more than one base classes
19
Class A
Class E Derived class
Class B Class C
21. class Vehicle //base
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Four_Wheeler //base
{
public:
void display()
{
cout<< "This is a Four wheeler";
}
};
21
class Car : public Vehicle, Four_wheeler
{
public:
void print()
{
cout<< "This is a Car";
}
};
Void main()
{
Car obj;
obj.show();
obj.display();
obj.print();
}
OUTPUT:
This is a Vehicle
This is a Four wheeler
This is a Car
22. HIERARCHICAL INHERITANCE
A- Base class, B,C & D derived
Further B,C & D are not used for deriving classes
22
Class A
Class C
Class B Class D
24. class Vehicle
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Car : public Vehicle
{
public:
void display()
{
cout<< "This is a Car";
}
};
24
class Bus : public Vehicle
{
public:
void print()
{
cout<< "This is a Bus";
}
};
Void main()
{
Car obj1;
Bus obj2;
obj1.show();
obj1.display();
obj2.show();
obj2.print();
}
OUTPUT:
This is a Vehicle
This is a Car
This is a Vehicle
This is a Bus
25. MULTILEVEL INHERITANCE
A- Base class, B derived from A , C- derived form C
C inherits all the features of A & B.
25
Class A
Class C
Class B
Base class1
Base class2
27. class Vehicle
{
public:
void show()
{
cout<< "This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Car";
}
};
27
class Car : public Four_wheel
{
public:
void print()
{
cout<< "This is a Bus";
}
};
Void main()
{
Car obj1;
Bus obj2;
obj1.show();
obj1.display();
obj2.show();
obj2.print();
}
OUTPUT:
This is a Vehicle
This is a Car
This is a Vehicle
This is a Bus
28. HYBRID INHERITANCE
A- Base class, B derived from A , Class D - derived form B & C
D inherits all the features of B & C directly and inherits features of class A indirectly
through B
28
Class A
Class D
Class B
Base class
Class C
Derived class
30. class Vehicle
{
public:
void show()
{
cout<< “This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Four_Wheeler";
}
};
class Fare
{
public:
void dip()
{
cout<< “Fare is high";
}
};
30
class Bus : public Four_wheel, Fare
{
public:
void print()
{
cout<< “This is a Bus";
}
};
Void main()
{
Bus obj;
obj.print();
obj.dip();
obj.display();
obj.show();
}
OUTPUT:
This is a Bus
Fare is high
This is a Four Wheeler
This is a Vehicle
31. MULTIPATH INHERITANCE
A- Base class, B and C derived from A , Class D - derived form B & C
D inherits all the features of B & C directly and inherits features of class A indirectly
through B & C
Ambiguity occurs : because the main base class is inherited twice.
To avoid ambiguity a keyword “virtual” is used.
31
Class A
Class D
Class B
Main Base class
Class C
Derived class
33. class Vehicle
{
public:
void show()
{
cout<< “This is a Vehicle";
}
};
class Four_Wheel: public Vehicle
{
public:
void display()
{
cout<< "This is a Four_Wheeler";
}
};
class Fare : public Vehicle
{
public:
void dip()
{
cout<< “Fare is high";
}
};
33
class Bus : public Four_wheel, Fare
{
public:
void print()
{
cout<< “This is a Bus";
}
};
Void main()
{
Bus obj;
obj.print();
obj.dip();
obj.display();
obj.show();
}
OUTPUT:
This is a Bus
Fare is high
This is a Four Wheeler
This is a Vehicle
34. VIRTUAL BASE CLASS
In multipath inheritance child class could have duplicate sets of members
inherited from a single base class.
Use : To avoid ambiguity due to multipath inheritance.
When classes are declared virtual complier takes essential caution to
avoid duplication.
Keyword “virtual” is used with the base class when it is inherited.
34
35. e.g.
Class A1
{
protected:
int a1;
};
Class A2 : virtual public A1
{
protected:
int a2;
};
Class A3 : virtual public A1
{
protected:
int a3;
};
35
e.g.
Class A4 : public A2,A3
{
protected:
int a4;
};
Here using object of A4, it can access
a1,a2,a3 and a4.
Class A1
Class A4
Class A3
Class A2
36. ABSTRACT CLASS
when a class is not used for creating objects then it is called abstract class.
Abstract class can act as base class only.
It is used for inheriting and not used for object creation.
An abstract class gives a skeleton or structure using which other classes are
created.
36
37. 37
Class A
{
public:
int x ;
class B
{
public:
int y ;
};
};
Class C : public A, A :: B
{
public:
int z ;
void show ( )
{
cout<<“X=“<<x;
cout<<“Y=“<<y;
cout<<“Z=“<<z;
}
C ( int j, int k, int l)
{
x=j;
y=k;
z=l;
}
};
Void main ( )
{
C c (4,7,1);
c.show();
}
OUTPUT:
X = 4
Y = 7
Z = 1
38. ADVANTAGES
Provides reusability
The derived classes extend the properties of base class
Generate more dominant objects.
Same Base class can be used by number of derived class.
All Derived class has same properties of base class.
38
39. DISADVANTAGES
Inappropriate use of inheritance makes programs more
complex.
Invoking member functions using objects create more
compiler overhead.
In Class hierarchy various data elements remains unused.
All Memory allocated is not utilized.
39