INHERITANCE
The capability of a class to derive properties and characteristics from another
class is called Inheritance.
Inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class”
and the existing class is known as the “base class” or “parent class”. The derived
class now is said to be inherited from the base class.
When we say derived class inherits the base class, it means, the derived class
inherits all the properties of the base class, without changing the properties of
base class and may add new features to its own. These new features in the derived
class will not affect the base class.
*Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
*Super Class: The class whose properties are inherited by a subclass is called Base
Class or Superclass.
Types Of Inheritance:-
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance
1. Single Inheritance: In single inheritance, a class is allowed to inherit from
only one class. i.e. one subclass is inherited by one base class only.
__________________________________________________________________
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class Car : public Vehicle {
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
_________________________________________________________________
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can
inherit from more than one class. i.e one subclass is inherited from more than one
base class.
__________________________________________________________________
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
class Car : public Vehicle, public FourWheeler {
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
________________________________________________________________
3. Multilevel Inheritance: In this type of inheritance, a derived class is created
from another derived class.
________________________________________________________________
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
________________________________________________________________
4. Hierarchical Inheritance: In this type of inheritance, more than one subclass is
inherited from a single base class. i.e. more than one derived class is created
from a single base class.
_______________________________________________________________
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Car : public Vehicle {
};
class Bus : public Vehicle {
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
_______________________________________________________________
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining
more than one type of inheritance
_______________________________________________________________
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
class Car : public Vehicle {
};
class Bus : public Vehicle, public Fare {
};
int main()
{
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
_______________________________________________________________