0% found this document useful (0 votes)
2 views

06. Inheritance

The document provides an overview of inheritance in Object-Oriented Programming using C++. It explains the concepts of base and derived classes, types of inheritance, and access specifiers, along with examples of single, multiple, and multilevel inheritance. Additionally, it discusses constructors and destructors in the context of inheritance.

Uploaded by

Abdul Wahab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

06. Inheritance

The document provides an overview of inheritance in Object-Oriented Programming using C++. It explains the concepts of base and derived classes, types of inheritance, and access specifiers, along with examples of single, multiple, and multilevel inheritance. Additionally, it discusses constructors and destructors in the context of inheritance.

Uploaded by

Abdul Wahab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Object

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 properties of existing classes are simply extended to the new


classes.

 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

Feature B Defined in Base


Class, accessible
Feature C from Derived
Class

Derived Class

3
General Syntax of Inheritance

 The Syntax for deriving a new class from an existing class is as follow:

class Derived_Class : Access_Specifier Base_Class


{
// new members of the derived class
};

 Generally, the access specified is to be specifier. In the absence of


access specifier, the default is private.

4
Types of Inheritance

 With respect to Accessibility


1. Public Inheritance
2. Private Inheritance

 With respect to number of Base classes


1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Multi path Inheritance
6. Hybrid 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.

 If data is to be kept inaccessible from outside the class, still inherited in to


the derived class then the data must be declared inside „Protected‟
access specifier

 Protected access specifier is same as private, except that it is accessible


from the derived 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.

Number of Base Classes

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

Enter Item No: 345


Enter Price: 67.99

Item No: 345


Price: 67.99

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

Enter values for a, b, c and d: 1 2 3 4


A= 1
B= 2
C= 3
D=4

25
Example of Multi path Inheritance

class A1 class A4 : public A2, A3


{ {
protected: int a4;
int a1 ; public:
};
void set()
{ a1=10; // Member Ambiguous Error
class A2 : public A1 a2=20;
{ a3=30;
protected: a4=40;
int a2 ;
}
};
void show()
{
class A3 : public A1 cout<<"\nA1= "<<a1; // Member Ambiguous Error
{ cout<<"\nA2= "<<a2;
protected:
cout<<"\nA3= "<<a3;
int a3 ;
}; cout<<"\nA4= "<<a4;
}
};

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:

class A2 : virtual public A1


class A3 : virtual public A1

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.

 In inheritance, when an object of a derived class is created, the


constructor of its base class is first executed, then the constructor of
derived class is executed.

 Destructors are executed in reverse order of constructor execution.

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

Constructor of Base (A)


Constructor of Derived (B)
Constructor of Derived (C)
Destructor of Derived (C)
Destructor of Derived (B)
Destructor of Base (A)

31
Base & Derived Classes without Default Constructor

class Base1 class Derived: public Base1, Base2


{ {
protected: int c;
int a; public:
public : Derived(int x, int y, int z): Base1(y), Base2(z)
Base1(int x) { c=x;
{ a=x; cout<<"\nConstructor of Derived"; }
cout<<"\nConstructor of Base1"; }
}; void show()
{ cout<<"\n"<<a<<" "<<b<<" "<<c; }
class Base2 };
{
protected: void main()
int b; {
public: clrscr();
Base2(int x)
{ b=x; Derived obj(1,3,5);
cout<<"\nConstructor of Base2"; } obj.show();
}; }

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:

Derived (int x, int y): Base1(x), Base2(y)


or
Derived (int z): Base1(z), Base2(z)

according to situation.

34
Have a Good Day!

You might also like