Inheritance (Unit V)
Inheritance (Unit V)
1)Inheritance:- Inheritance is the capability of one class to acquire properties and characteristics
from another class. Inheritance makes the code reusable. When we inherit an existing class, all
its method and fields become available in the new class, hence code is reused.
I.Single Inheritance:- in this type of inheritance one derived class inherits from only one base
class. It is the most simplest form of inheritance.
Ex:-
II)Multiple Inheritance:-
In this type of inheritance a single derived class may inherit from two or more than two
base classes.
IV) Mulitlevel Inheritance:-In this type of inheritance the derived class inherits from a class,
which in turn inherits from some other class. The super class for one is sub class for the other
2) Types of Derivation:-
i.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.
ii.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.
iii.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.
class A
{
public:int x;
protected:int y;
private:int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
3 Inheritance
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
3)Multiple Inheritance:- Multiple Inheritance is a feature of C++ where a class can inherit
from more than one classes. i.e one sub class is inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
// body of subclass
};
Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for every
base class must be specified.
#include<iostream>
using namespace std;
class Vehicle
{
public: Vehicle()
{
cout << "This is a Vehicle\n";
}
};
4 Inheritance
class FourWheeler
{
public: FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
4)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";
}
};
5 Inheritance
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
5)Hierarchical Inheritance :- In this type of inheritance, more than one sub class is inherited
from a single base class. i.e. more than one derived class is created from a single base class.
6 Inheritance
#include<iostream>
using namespace std;
class Vehicle
{
Public : Vehicle()
{
cout << "This is a Vehicle\n";
}
};
};
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
6)Hybrid (Virtual) Inheritance :- Hybrid Inheritance is implemented by combining more than
one type of inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:
7 Inheritance
#include<iostream>
using namespace std;
class Vehicle
{
Public : Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class Fare
{
Public : Fare()
{
cout << "Fare of Vehicle\n";
}
};
};
};
int main()
{
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle