`
Inheritance
Inheritance is a machenism in which one class inherit the property of other class. In other word we can say that when one class access the property of another class called Inheritance. When a class
inherits the property of a class it means it can access all the data member and member function of that class except private element.
In this type of programming mainly two types of classes are used.
- Parent / super / Base class
- Child / sub / Derived class
Parent / super / Base class
The class which is inherited by another class is called Parent or super or Base class.
Child / sub / Derived class
The class which is inherites the property of another class is called Child or sub or Derived class.
Types of Inheritance
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Single Inheritance
In this type of inheritance only two classes are used in which one is inherited by another. In other words we can say that a class which contains only one base class
and only one derived class is called single inheritance.
Example :
#include< iostream.h >
class addition // base class
{
public: //access specifier
void add()
{
int a,b=30,c=40;
a=b+c;
cout<< "Add = "<< a<<"\n";
}
};
class subtraction:public addition// derived class addition is inherited by subtraction
{
public: //access specifier
void sub()
{
int a,b=40,c=10;
a=b-c;
cout<< "Sub = "<< a<<"\n";
}
};
void main()
{
subtraction obj;
obj.add(); // calling base class function
obj.sub(); // calling derived class function
}
Output:
In the above example you can see that there is only two classes are used in which addition in inherited by subtraction we can call function add() and sub().
Multilevel Inheritance
When first class is inherited by second class, second class is inheritd by third class and so on called Multilevel Inheritance. In this type of inheritance each derived class
is the base class for the next class.
#include< iostream.h >
class addition // base class
{
public: //access specifier
void add()
{
int a,b=30,c=40;
a=b+c;
cout<< "Add = "<< a<<"\n";
}
};
class subtraction:public addition// Here class addition is inherited by class subtraction
{
public: //access specifier
void sub()
{
int a,b=40,c=10;
a=b-c;
cout<< "Sub = "<< a<<"\n";
}
};
class multipication:public subtraction // Here class subtraction is inherited by multipication
{
public: //access specifier
void mul()
{
int a,b=10,c=10;
a=b*c;
cout<< "multiply = "<< a<<"\n";
}
};
void main()
{
multipication obj;
obj.add(); // calling base class function
obj.sub(); // calling derived class function
obj.mul();
}
Output:
Add = 70
Sub = 30
multiply = 100
Multiple Inheritance
When two or more than two classes are inherited by a single class simultaneously called multiple inherotance. In other word we can say that A class which has contains one or more base class and only one derived class called multiple inheritance.
#include< iostream.h >
class addition // first base class
{
public: //access specifier
void add()
{
int a,b=30,c=40;
a=b+c;
cout<< "Add = "<< a<<"\n";
}
};
class subtraction // second base class
{
public: //access specifier
void sub()
{
int a,b=40,c=10;
a=b-c;
cout<< "Sub = "<< a<<"\n";
}
};
class multipication:public addition, public subtraction // Here class additon and subtraction are inherited by multiplication
{
public: //access specifier
void mul()
{
int a,b=10,c=10;
a=b*c;
cout<< "multiply = "<< a<<"\n";
}
};
void main()
{
multipication obj;
obj.add(); // calling first base class function
obj.sub(); // calling second base class function
obj.mul(); // calling derived class function
}
Output:
Add = 70
Sub = 30
multiply = 100