0% found this document useful (0 votes)
13 views7 pages

Inheritance (Unit V)

INHERITANCE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Inheritance (Unit V)

INHERITANCE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

1 Inheritance

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.

Types of Inheritance:-We have five different types of inheritance. Namely

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.

III) Hierarchical Inheritance:-


In this type of inheritance, multiple derived classes inherits from a single base class
Ex:-
2 Inheritance

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

V) Hybrid Inheritance:- Hybrid inheritance is combination of Hirarchical and Multilevel


inheritance

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
};

class D : private A // 'private' is default for classes


{
// x is private
// y is private
// z is not accessible from D
};

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";
}
};

class Car : public Vehicle, public FourWheeler


{

};

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

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
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";
}
};

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
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";
}
};

class Car : public Vehicle


{

};

class Bus : public Vehicle, public Fare


{

};

int main()
{
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle

You might also like