Multilevel Inheritance
When a class is
derived from another derived class is called as multilevel inheritance.
It is implemented
by defining at least three classes. In multilevel inheritance, there is one
base class and the remaining two are derived class.
Diagrammatic
representation:
Syntax
class A {/* ... */ };
class B: public A { /* ... */ };
class C :public B{ /* ... */ };
class A {/* ... */ };
class B: public A { /* ... */ };
class C :public B{ /* ... */ };
Sample program:
class A
{
public: void display ()
{
cout<<"Base class content.";
}
};
class B : public A
{
….....
};
class C: public B
{
….......
};
{
public: void display ()
{
cout<<"Base class content.";
}
};
class B : public A
{
….....
};
class C: public B
{
….......
};
int main()
{
C c;
c.display();
return 0;
}
{
C c;
c.display();
return 0;
}
Output:
base class content
Advantages:
- Allows the code to be reused
as many times as needed. The base class once defined and once it is
compiled, it need not be reworked.
- Saves time and effort as the
main code need not be written again.
Disadvantages
- In Inheritance base class
and child classes are tightly coupled. Hence If you change the code of
parent class, it will get affects to the all the child classes.
- In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly.
Multilevel Inheritance
Reviewed by NEERAJ SRIVASTAVA
on
3:59:00 PM
Rating:
No comments: