Inheritance
Dept. Of Computer Science &Engineering
Chapter Contents
2
Inheritance
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
6. Multipath
Inheritance
3
In C++, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically.
In such way, you can reuse, extend or modify the attributes and behaviors
which are defined in other class.
Types Of Inheritance:
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Inheritance
4
Derived Classes:
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
class derived_class_name : visibility-mode base_class_name
{
// body of the derived class.
}
Inheritance
5
derived_class_name: It is the name of the derived class.
visibility mode: The visibility mode specifies whether the features of the base
class are publicly inherited or privately inherited. It can be public or private.
base_class_name: It is the name of the base class.
When the base class is privately inherited by the derived class, public members
of the base class becomes the private members of the derived class. Therefore,
the public members of the base class are not accessible by the objects of the
derived class only by the member functions of the derived class.
When the base class is publicly inherited by the derived class, public members of
the base class also become the public members of the derived class. Therefore,
the public members of the base class are accessible by the objects of the derived
class as well as by the member functions of the base class.
Note: 1. In C++, the default mode of visibility is private.
2. The private members of the base class are never inherited.
Single Inheritance
6
Single inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
When one class inherits another class, it is known as single level inheritance.
Let's see the example of single level inheritance which inherits the fields only.
Example-1
7
#include <iostream>
using namespace std;
class Account {
public:
float salary ;
};
class Programmer: public Account {
public:
float bonus ;
};
int main() {
Programmer p1;
p1.salary=60000; p1.bonous=5000;
cout<<"Salary: "<<p1.salary<<endl; cout<<"Bonus: "<<p1.bonus<<endl;
return 0; }
Single Level Inheritance: Inheriting Methods
8
#include <iostream>
using namespace std;
int main(void) {
class Animal {
Dog d1;
public:
d1.eat();
void eat() {
d1.bark();
cout<<"Eating..."<<endl;
return 0;
}
}
};
O/P:
class Dog: public Animal
Eating...
{
Barking...
public:
void bark(){
cout<<"Barking...";
}
};
Example-3
9
#include <iostream>
class B : public A
using namespace std;
{
class A
public:
{
void display()
int a , b;
{
public:
cout <<"Multiplication of a and b is : "< < c;
int mul(int x,int y)
}
{
};
a= x ; b= y;
int main()
int c = a*b;
{
B b;
}
b.display();
};
return 0;
}
Visibility of Inherited Members
10
Base class visibility Derived class visibility
Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
Multi Level Inheritance Example
11
Multilevel inheritance is a process of deriving a class from another
derived class.
When one class inherits another class which is further inherited by
another class, it is known as multi level inheritance in C++.
Inheritance is transitive so the last derived class acquires all the
members of all its base classes.
Example
12
#include <iostream> class BabyDog: public Dog
using namespace std; {
class Animal { public:
public: void weep() {
cout<<"Weeping...";
void eat() {
}
cout<<"Eating..."<<endl;
};
} int main(void) {
}; BabyDog d1;
class Dog: public Animal d1.eat();
{ d1.bark();
d1.weep();
public:
return 0;
void bark(){
}
cout<<"Barking..."<<endl;
}
};
Multiple Inheritance
13
Multiple inheritance is the process of deriving a new class that inherits
the attributes from two or more classes.
Syntax
14
class D : visibility B-1, visibility B-2, ...
{
// Body of the class-D;
}
Multiple Inheritance
15
#include<iostream>
using namespace std;
class A int main()
{ {
public: C c;
A() { cout << "A's constructor called" << endl; } return 0;
}; }
class B Output:
{
A's constructor called
public:
B's constructor called
B() { cout << "B's constructor called" << endl; }
C's constructor called
};
class C: public A, public B // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
Example-1
16
#include <iostream>
using namespace std; class Triangle : public Shape
{
class Shape // Declaration of base class.
public:
{ int triangle_area()
public: {
int a, b; float result = 0.5*a*b;
void get_data(int n,int m) return result;
{ }
};
a= n; b = m;
main(){
} Rectangle r; Triangle t;
}; int length,breadth,base,height;
class Rectangle : public Shape cout << "Enter the length and breadth of a rectangle: ";
{ cin>>length>>breadth;
public: r.get_data(length,breadth);
int m = r.rect_area();
int rect_area()
cout << "Area of the rectangle is : " <<m ;
{ cout << "Enter the base and height of the triangle: " ;
int result = a*b; cin>>base>>height;
return result; t.get_data(base,height);
} float n = t.triangle_area();
}; cout <<"Area of the triangle is : " << n ;
return 0;
}
Hybrid Inheritance
17
Hybrid inheritance is a combination of more than one type of inheritance.
Virtual base class in C++
18
Virtual base classes are used in virtual inheritance in a way of preventing multiple
“instances” of a given class appearing in an inheritance hierarchy when using
multiple inheritances.
Example
19
class ClassA
class ClassD : public ClassB, public ClassC
{
{
public:
public:
int a;
int d;
};
};
class ClassB : virtual public ClassA
void main()
{ {
public: ClassD obj;
int b; obj.a = 10; //Statement 1
}; obj.a = 100; //Statement 2
class ClassC : virtual public ClassA obj.b=20;obj.c=30; obj.d = 40;
{ cout<< "\n A : "<< obj.a; // 100
public: cout<< "\n B : "<< obj.b; // 20
int c; cout<< "\n C : "<< obj.c; // 30
}; cout<< "\n D : "<< obj.d; // 40
}
Order of Constructor Call with Inheritance in C++
20
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default constructor
is executed and then the derived class's constructor finishes execution.
Points to Remember
1. Whether derived class's default constructor is called or parameterized is called, base
class's default constructor is always called inside them.
2. To call base class's parameterized constructor inside derived class's parameterized
constructor, we must mention it explicitly while declaring derived class's
parameterized constructor.
21