Multilevel inheritance in C++ is a type of inheritance where a derived class inherits from a base class, which in turn inherits from another base class. In other words, a class inherits properties and methods from a parent class and that parent class in turn inherits properties and methods from its parent class. This creates a hierarchy of classes, where each class inherits properties and methods from its parent class, and the parent class inherits properties and methods from its parent class, and so on.
Multilevel inheritance allows for code reuse, as a derived class can use the properties and methods of its base class, and the base class can use the properties and methods of its base class, and so on. It also allows for creating a hierarchy of classes, where each class can be specialized for a specific task, while still inheriting properties and methods from its parent class.
Here is an example of multilevel inheritance in C++:
class GrandParent {
public:
int x;
void func1() { /* code */ }
};
class Parent : public GrandParent {
public:
int y;
void func2() { /* code */ }
};
class Child : public Parent {
public:
int z;
void func3() { /* code */ }
};
In this example, the class "Child" inherits properties and methods from class "Parent" and class "Parent" inherits properties and methods from class "GrandParent". The class "Child" can use x, y, z and the methods func1(), func2(), func3() inherited from the base classes.
In summary, Multilevel Inheritance in C++ allows a class to inherit properties and methods from its parent class, which in turn inherits properties and methods from its parent class, creating a hierarchy of classes. It allows for code reuse and for creating specialized classes that inherit properties and methods from more general classes.
Comments
Post a Comment