Skip to main content

Inheritance

Inheritance means deviring properties of one class into another class.

class A => Parent /Base / Super class

class B => Child /Derived /Sub-class


#include <iostream>
using namespace std;

class A
{
public:
void showA()
{
cout<<"I am showA()"<<endl;
}
};
class B
{
public:
void showB()
{
cout<<"I am showB()"<<endl;
}
};

int main() {
A objA;
objA.showA();

B objB;
objB.showAB();

return 0;

}

Output

I am showA()
I am showB()

Explaination

create class A write access specifier public, create a showA() method.
print I am showA()
create another class B write access specifier public, create a showB() method.
print I am showB()
In main function create object of A class and using objA object call showA() method
In next line create object of B class and using objB object call showA() method


#include <iostream>
using namespace std;

class A
{
public:
void showA()
{
cout<<"I am showA()"<<endl;
}
};
class B : public A
{
public:
void showB()
{
cout<<"I am showB()"<<endl;
}
};

int main() {
B objB;
objB.showB();
objB.showA();

return 0;

}

Output

I am showA()
I am showB()

Explaination

create class A write access specifier public, create a showA() method.
print I am showA()
create another class B and inherite the properties of A and useing pulbic access specifier,
In next line write access specifier public, create a showB() method.
print I am showB()
In main function create object of B class objB.
using objB call showB() and showA() method.

Types of Inheritance​

  1. Single level inheritance

  2. Multilevel inheritance

  3. multiple inheritance

  4. Hierarchical inheritance

1. Single level inheritance​

The inheritance in which class B is inherited from a class A is known as Single level inheritance


#include <iostream>
using namespace std;

class A
{
public:
void showA()
{
cout<<"I am showA()"<<endl;
}
};
class B : public A
{
public:
void showB()
{
cout<<"I am showB()"<<endl;
}
};

int main() {
c objC;
objA.showA();

B objB;
objB.showAB();

return 0;

}

Output

I am showA()
I am showB()

Explaination

create class A write access specifier public, create a showA() method.
print I am showA()
create another class B and inherite the properties of A and useing pulbic access specifier,
In next line write access specifier public, create a showB() method.
print I am showB()
In main function create object of B class objB.
using objB call showB() and showA() method.

2. Multilevel inheritance​

The Multilevel inheritance means if a class A , class B and class C . class C is inherited from class B and
class B is inherited from class A is known as Multilevel inheritance.


#include <iostream>
using namespace std;

class A
{
public:
void showA()
{
cout<<"I am showA()"<<endl;
}
};
class B : public A
{
public:
void showB()
{
cout<<"I am showB()"<<endl;
}
};
class C : public B
{
public:
void showC()
{
cout<<"I am showC()"<<endl;
}
};

int main() {
C obj;
obj.showC();
obj.showB();
obj.showA();

return 0;

}

Output

I am showC()
I am showB()
I am showA()

Explaination

create class A write access specifier public, create a showA() method.
print I am showA()
create another class B and inherite the properties of A and using pulbic access specifier,
In next line write access specifier public, create a showB() method.
print I am showB()
create another class C and inherite the properties of B and using pulbic access specifier,
In main function create object of C class objC.
using objC call showC() showB() and showA() method.

3. multiple inheritance​

In multiple inheritance means that a subclass can inherit from two or more superclass.


#include <iostream>
using namespace std;

class Mother
{
public:
void showMother()
{
cout<<"Mother : ABC"<<endl;
}
};
class Father
{
public:
void showFather()
{
cout<<"Father : PQR"<<endl;
}
};
class Child : public Mother, public Father
{
public:
void showChild()
{
cout<<" I am Child"<<endl;
}
};

int main() {
Child obj;
obj.showChild();
obj.showMother();
obj.showFather();

return 0;

}

Output

I am Child
Mother : ABC
Father : PQR

Explaination

create class Mother write access specifier public, create a showMother() method.
print Mother : ABC
create class Father write access specifier public, create a showFather() method.
print Father : PQR
create another class Child and inherite the properties of Mother and Father using pulbic access specifier,
In next line write access specifier public, create a showB() method.
print I am Child
In main function create object of Child class obj.
using obj call showMother() showFather() method.

4. Hierarchical inheritance​

In Hierarchical inheritance class RBI is a parent class and class BOI, class SBI is a child class.

#include <iostream>
using namespace std;

class RBI
{
public:
void showRBI()
{
cout<<"According to RBI guidlines RBI is 10% "<<endl;
}
};
class BOI : public RBI
{
public:
void showBank()
{
cout<<"I am BOI"<<endl;
}
};
class SBI : public RBI
{
public:
void showBank()
{
cout<<"I am SBI"<<endl;
}
};

int main() {
BOI obj1;
obj1.showBank();
obj1.showRBI();

SBI obj2;
obj2.showBank();
obj2.showRBI();

return 0;

}

Output

I am BOI
According to RBI guidlines RBI is 10%
I am SBI
According to RBI guidlines RBI is 10%

Explaination

create class RBI write access specifier public, create a showRBI() method.
print According to RBI guidlines RBI is 10%
create class BOI and inherite the properties of RBI, write access specifier public, create a showBOI() method.
print I am BOI
create another class SBI and inherite the properties of RBI, write access specifier public.
In next line create a showSBI() method.
print I am SBI
In main function create object of BOI class obj1, and call showBank(), and showRBI() method
create object of SBI class obj2, and call showBank(), and showRBI() method