MULTI-LEVEL INHERITANCE:
If a class is derived from a
derived class, then the inheritance type is called as multi-level inheritance.
In
the above example class A is base class and class B is inherited from class A
and class C is a derived class inherited from class B
From
the above example we can conclude that class B becomes the base class for class
C.This is called as multi-level inheritance.
Syntax:
class <derived class name B>:
<access specifier><base class name A>
class <derived class name C>:
<access specifier><base class name B>
Example 6:
#include<iostream.h>
#include<conio.h>
class
A
{
public:
int a;
void setA(int x)
{
a=x;
}
void showA()
{
cout<<”\nA=”<<a;
}
};
class
B: public A
{
public:
int b;
void setB(int x)
{
b=x;
}
void showB()
{
cout<<”\nB=”<<b;
}
};
class
C: public B
{
public:
int c;
void setC(int x)
{
c=x;
}
void showC()
{
cout<<”\nC=”<<c;
}
};
void
main()
{
A objA;
B objB;
C objC;
clrscr();
//calling class A methods
cout<<”\ncalling class A methods”;
objA.setA(10);
objA.showA();
//calling class B methods
cout<<”\n calling class B methods”;
objB.setA(10);
objB.setB(20);
objB.showA();
objB.showB();
//calling class C methods
cout<<”\ncalling class C methods”;
objC.setB(20);
objC.setC(30);
objC.showB();
objC.showC();
getch();
}
Output
Calling
class A methods
A=10
Calling
class B methods
A=10
B=20
Calling
class C methods
B=20,
C=30
No comments:
Post a Comment