Learn C++ Inheritance :: Multilevel Inheritance
By Yilmaz Yoru May 24, 2021
Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. If a class is derived from another derived class then it is called multilevel inheritance. In other terms, multilevel inheritance method is using a class which has more than one parent class. Multilevel Inheritance differences from the Multiple Inheritance because of it has a previous class in each levels of new class definitions.
Here is a multilevel inheritance example,
For example, if we take lives as a base class then animals are the derived class which has features of animals and then mammals are the also derived class that is derived from sub-class animal which inherit all the features of lives.
From this example, we prepared a Multilevel Inheritance example below,
Here all lives has weight, and animals has a type value and mammals has number of legs. We define an elephant object as a mammal, and we are able to reach all properties and methods of mammals, animals derived classes and lives base class.
By Yilmaz Yoru May 24, 2021
Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. If a class is derived from another derived class then it is called multilevel inheritance. In other terms, multilevel inheritance method is using a class which has more than one parent class. Multilevel Inheritance differences from the Multiple Inheritance because of it has a previous class in each levels of new class definitions.
Here is a multilevel inheritance example,
C++:
class base_class_name // base class
{
// base properties and functions
};
class class_name1 : <acessspecifier> base_class_name // derived class from base_class_name
{
// properties and functions
};
class class_name2 : <accessspecifier> class_name1 // derived from derived class class_name1
{
// properties and functions
};
C++:
class lives // base class
{
// base properties and functions
};
class animals : public lives // derived from lives
{
// properties and functions
};
class mammals : public animals // derived from derived class animals
{
// properties and functions
};
C++:
#include <iostream>
class lives //single base class
{
public:
int weight;
void set_weight(int w)
{
weight = w;
}
};
class animals : public lives // derived class from base class
{
public:
int type; // 0:Sea 1:Ground 2:Air Animal
void set_type(int t)
{
type = t;
}
};
class mammals : public animals // derived from class derive1
{
private:
int num_of_legs;
public:
void set_numoflegs(int l)
{
num_of_legs=l;
}
void info()
{
std::cout << "Weight:" << weight << '\n';
std::cout << "Type:" << type << '\n';
std::cout << "Number of Legs:" << num_of_legs << '\n';
}
};
int main()
{
mammals elephant;
elephant.set_weight(5000);
elephant.set_type(1); // 0:Sea 1:Ground 2:Air Animal
elephant.set_numoflegs(4);
elephant.info();
getchar();
return 0;
}