06. Inheritance
06. Inheritance
Oriented
Programming
Using
C ++
Inheritance
Abdul Wahab
Lecturer
University of Science and Technology Bannu
[email protected]
1
Inheritance
The process of creating new classes from one or more existing classes is
known as Inheritance.
The new classes is known as ‘Derived Classes’ and the existing classes
are known as ‘Base Classes’.
2
Inheritance
Base Class
Reusability is the main purpose of Feature A
inheritance
Feature B
Feature C
The new class can have its own set
of member variables and functions.
Defined in
Feature D Derived Class
Feature A
Derived Class
3
General Syntax of Inheritance
The Syntax for deriving a new class from an existing class is as follow:
4
Types of Inheritance
5
Types of Inheritance w.r.t.
Accessibility
6
Public Inheritance
All the public members of the Base class can be accessed directly in the
derived class.
Private data of the base class is not inherited, they can be accessed only
using public member functions of the base class.
7
A Simple Example:
class A
void main()
{
public: {
int x; clrscr();
};
B obj;
class B : public A
{ obj.x=20;
public: obj.y=30;
int y;
obj.show();
void show()
}
{
cout<<“X= ”<<x<<“ Y= ”<<y;
}
};
8
Output
X=20 Y=30
9
Example with Private Data:
class B : public A
class A
{
{
int x; int y;
public:
public: B() { y=30; }
A() { x=20; } void show()
{
void show_x()
{ show_x();
cout<< "X= "<<x; cout<<" Y= "<<y;
} }
}; };
void main()
{
clrscr();
B obj;
obj.show();
}
10
Output
X=20 Y=30
11
Example with Protected Data:
class B : public A
class A
{
{
protected: int y;
int x; public:
B() { y=30; }
public: void show()
A()
{
{
x=20; cout<<“ X= “<<x<<" Y= "<<y;
} }
}; };
void main()
{
clrscr();
B obj;
obj.show();
}
12
Output
X=20 Y=30
13
Private Inheritance
Public members of the Base class become private for the derived class,
and they cannot be accessed outside the class (i.e. accessing directly
with the object)
Member functions of the derived class are used to access the derived
private members.
14
Example :
class A
void main()
{
public: {
int x; clrscr();
}; B obj;
obj.show();
}
class B : private A
{
public:
int y;
B() { x=20; y=30; }
void show()
{
cout<<“ X= “<<x<<" Y= "<<y;
}
};
15
Output
X=20 Y=30
16
Derived class access mode
Base class access Public Derivation Private Derivation Protected
specifier Derivation
Public Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
17
Types of Inheritance w.r.t.
18
Types of Inheritance w.r.t Base Classes
1. Single Inheritance
X
When only one class is used as a Base
class and there is no derivation from
the derived class Y
2. Multiple Inheritance
When one class is derived from two or X Y
more Base classes
19
Types of Inheritance w.r.t Base Classes
3. Multilevel Inheritance X
When a class is derived from another
Derived class, i.e. derived class acts Y
as a Base class
Z
4. Hierarchical Inheritance
When two or more classes are derived W
from a single Base class
X Y Z
20
Types of Inheritance w.r.t Base Classes
W
5. Multi path Inheritance
When a class is derived from two or
X Y
more classes that are derived from
same Base class.
Z
6. Hybrid Inheritance
X
The combination of two or more types
of inheritance.
Y Z
21
Example of Simple Inheritance:
class ITEM
{
void main()
protected:
int ItemNo ; {
}; clrscr();
item X;
class item : public ITEM X.set();
{
X.show();
float price ;
public: }
void set()
{
cout<<"\nEnter Item No: ";
cin>>ItemNo;
cout<<"\nEnter Price : ";
cin>>price;
}
void show()
{ cout<<"\nItem No: "<<ItemNo;
cout<<"\nPrice : "<<price;
}
};
22
Output
23
Example of Multiple Inheritance:
class A
{ protected: int a; };
void main()
class B
{ protected: int b ; }; {
clrscr();
class C
D obj;
{ protected: int c ; };
obj.set();
class D : public A, B, C obj.show();
{
int d;
}
public:
void set()
{
cout<<"\nEnter vlues for a, b, c and d: ";
cin>>a>>b>>c>>d;
}
void show()
{
cout<<"\nA= "<<a; cout<<"\nB= "<<b;
cout<<"\nC= "<<c; cout<<"\nD= "<<d;
}
};
24
Output
25
Example of Multi path Inheritance
26
Example of Multi path Inheritance (Contd…)
void main()
{
clrscr();
A4 obj;
obj.set();
obj.show();
}
The error or ambiguity can be avoid by specifying the keyword “Virtual” before the
Base class specifier (in derivation of new classes from it). For example:
27
Output
A1= 10
A2= 20
A3= 30
A4= 40
28
Constructor, Destructor and Inheritance
If the base class has parameterized constructor then it is essential for the
derived class to have a constructor.
29
Example:
class A class C: public B
{ {
public : public:
A() C()
{ cout<<"\nConstructor of Base (A)"; } { cout<<"\nConstructor of Derived (C)"; }
~A() ~C()
{ cout<<"\nDestructor of Base (A)"; } {cout<<"\nDestructor of Derived (C)";}
}; };
class B: public A
{ void main()
public: {
B() clrscr();
{ cout<<"\nConstructor of Derived (B)"; }
C obj;
~B() }
{ cout<<"\nDestructor of Derived (B)"; }
};
30
Output
31
Base & Derived Classes without Default Constructor
32
Output
33
Note
If a derived class constructor does not use any arguments, Still it must
declare one or more arguments, if the Base class takes one or more
arguments as:
according to situation.
34
Have a Good Day!