Unit 5 Inheritance
Unit 5 Inheritance
Inheritance
Modes of Inheritance
Public mode: If we derive a sub class from a public base class. Then the public member of the
base class will become public in the derived class and protected members of the base class
will become protected in derived class.
Protected mode: If we derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected in derived class.
Private mode: If we derive a sub class from a Private base class. Then both public member and
protected members of the base class will become Private in derived class.
Note: The private members in the base class cannot be directly accessed in the derived class,
while protected members can be directly accessed.
Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.
Where 'A' is the base class, and 'B' is the derived class.
Example:
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output:
Salary: 60000
Bonus: 5000
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Output:
Eating...
Barking...
Weeping...
Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a base
class.
Syntax:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
Example:
#include <iostream>
using namespace std;
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
cout<<"Enter the length and breadth of rectangle:"<<endl;
cin>>length>>breadth;
r.get_data(length,breadth);
cout<< "Area of the rectangle is :"<<r.rect_area()<<endl;
Output:
Enter the length and breadth of a rectangle:
8
6
Area of the rectangle is : 48
Enter the base and height of the triangle:
6
4
Area of the triangle is : 12
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
}
};
class B : public A
{
class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of c is : " << endl;
cin>>c;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Output:
Enter the value of 'a' :
6
Enter the value of 'b' :
7
Enter the value of c is :
8
Multiplication of a,b,c is: 336
class A
{
public:
void display()
{
cout << "Class A" <<endl;
}
};
class B
{
public:
void display()
{
cout << "Class B" <<endl;
}
};
int main()
{
C c;
c.A::display();
c.B::display();
return 0;
}
Output:
Class A
Class B
class base {
public:
base() { cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};
int main()
{
derived ob;
return 0;
}
Output:
Constructing base
Constructing derived
Destructing derived
Destructing base
class base1 {
public:
base1() { cout << "Constructing base1\n"; }
~base1() { cout << "Destructing base1\n"; }
};
class base2 {
public:
int main()
{
derived ob;
return 0;
}
Output:
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base2
Destructing base1
How to call the parameterized constructor of base class in derived class constructor?
// C++ program to show how to call parameterised Constructor
// of base class when derived class's Constructor is called
#include <iostream>
using namespace std;
// base class
class Parent {
int x;
public:
// base class's parameterised constructor
Parent(int i)
{
x = i;
cout << "Inside base class's parameterised "
"constructor"
<< endl;
}
};
// sub class
class Child : public Parent {
// main function
int main()
{
// creating object of class Child
Child obj1(10);
return 0;
}
Output:
Inside base class's parameterised constructor
Inside sub class's parameterised constructor
Example:
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello from A \n";
}
};
int main()
{
Output:
Hello from A