HYBRID INHERITANCE:
If
a class is inherited with the combinations of one or more basic inheritance
types, then it is called as hybrid inheritance.
Example 7:
#include<iostream.h>
#include<conio.h>
class
A
{
public:
int a;
void setA(int x)
{
a=x;
}
void showA()
{
cout<<”\nA=”<<a;
}
};
class
C
{
public:
int C;
void setC(int x)
{
c=x;
}
void showC()
{
cout<<”\nC=”<<c;
}
};
class
B:public A
{
public:
int b;
void setB(int x)
{
b=x;
}
void showB()
{
cout<<”\nB=”<<b;
}
};
classD:
public B, public C
{
public:
int b;
void setB(int x)
{
b=x;
}
void showB()
{
cout<<”\nB=”<<b;
}
};
void
main()
{
A objA;
B objB;
C objC;
D objD;
clrscr();
//calling class A methods
cout<<”\ncalling class A methods”;
objA.setA(10);
objA.showA();
//calling class B methods
cout<<”\ncalling class B methods”;
objB.setA(10);
objB.setB(20);
objB.showA();
objB.showB();
//calling class C methods
cout<<”\ncalling class C methods”;
objC.setC(30);
objC.showC();
//calling class D methods
cout<<”\ncalling class D methods”;
objD.setB(10);
objD.setC(20);
objD.showB();
objD.showC();
}
Output:
Calling
class A methods
A=10
Calling
class B methods
A=10
B=20
Calling
class C methods
C=30
Calling
class D methods
B=10
C=20
No comments:
Post a Comment