In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes.
There are different types of inheritance in C++:
Single Inheritance:
A derived(child class,sub class) class inherits from only one base(parents class,super class) class.
class Base {
// Base class members
};
class Derived : public Base {
// Derived class members
};
Example
#include<iostream.h>
#include<conio.h>
class a
{
public:
void get()
{
cout<<“a class”<<endl;
}};
class b:public a
{
public:
void set()
{
cout<<“b class”;
}};
void main()
{
b ob1;
clrscr();
ob1.get();
ob1.set();
getch();
}
Multiple Inheritance:
A derived class can inherit from multiple base classes.
class Base1 {
// Base1 class members
};
class Base2 {
// Base2 class members
};
class Derived : public Base1, public Base2 {
// Derived class members
};
Example
#include<iostream.h>
#include<conio.h>
class a
{
public:
void disp()
{
cout<<“the function of a class”<<endl;
}};
class b
{
public:
void disp1()
{
cout<<“the function of b class”<<endl;
}};
class c:public a, public b
{
public:
void disp2()
{
cout<<“the function of c class”;
}};
void main()
{
c ob1;
clrscr();
ob1.disp();
ob1.disp1();
ob1.disp2();
getch();
}
Multilevel Inheritance:
A derived class serves as a base class for another class.
class Base {
// Base class members
};
class Derived1 : public Base {
// Derived1 class members
};
class Derived2 : public Derived1 {
// Derived2 class members
};
Example
#include<iostream.h>
#include<conio.h>
class a
{
public:
void get()
{
cout<<“a class”<<endl;
}};
class b:public a
{
public:
void set()
{
cout<<“b class”<<endl;
}};
class c:public b
{
public:
void put()
{
cout<<“c class”<<endl;
}};
void main()
{
c ob1;
clrscr();
ob1.get();
ob1.set();
ob1.put();
getch();
}
Hierarchical Inheritance:
Multiple classes are derived from a single base class.
class Base {
// Base class members
};
class Derived1 : public Base {
// Derived1 class members
};
class Derived2 : public Base {
// Derived2 class members
};
Example
#include<iostream.h>
#include<conio.h>
class a
{
public:
void show()
{
cout<<“this is function of A class”<<endl;
}};
class b:public a
{
public:
void show1()
{
cout<<“this is function of b class”<<endl;
}};
class c:public a
{
public:
void show2()
{
cout<<“this is function of c class”<<endl;
}};
void main()
{
b ob1;
c ob2;
clrscr();
ob1.show();
ob1.show1();
ob2.show();
ob2.show2();
getch();
}
Hybrid Inheritance:
A combination of any of the above types.
class Base {
// Base class members
};
class Derived1 : public Base {
// Derived1 class members
};
class Derived2 : public Base {
// Derived2 class members
};
class FinalDerived : public Derived1, public Derived2 {
// FinalDerived class members
};
In C++, you use access specifiers (public, protected, private) to define the visibility of the base class members in the derived class. The most common access specifier is public, which means public members of the base class remain public in the derived class.
class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};
class Derived : public Base {
// publicMember is accessible
// protectedMember is accessible
// privateMember is not accessible
};