C++ Hybrid inheritance

The method of combining any two or more forms of inheritance in single form is called hybrid inheritance.

Program:

/*PROGRAM TO IMPLEMENT HYBRID INHERITANCE*/

#include<conio.h>

#include<iostream.h>

class base

{

int a;

public:

base()

{

cout<<“Enter a: “;

cin>>a;

}

int show()

{

cout<<“a = “<<a<<endl;

return(a);

}

~base()

{

cout<<“Destructor of base class is executed”<<endl;

}

};

class derived1:public base

{

int b;

public:

derived1():base()

{

cout<<“Enter b: “;

cin>>b;

}

int show1()

{

cout<<“b = “<<b<<endl;

return(b);

}

~derived1()

{

cout<<“Destructor of derived1 class is

executed”<<endl;

}

};

class derived2:public derived1

{

int a,b,c,sum;

public:

derived2():derived1()

{

cout<<“Enter c: “;

cin>>c;

}

void show2()

{

a=show();

b=show1();

cout<<“c = “<<c<<endl;

sum=a+b+c;

cout<<“Sum of given numbers: “<<sum<<endl;

}

~derived2()

{

cout<<“Destructor of derived2 class is

executed”<<endl;

}

};

class derived3:public derived1

{

int a,b,c,sum;

public:

derived3():derived1()

{

cout<<“Enter c: “;

cin>>c;

};

void show3()

{

cout<<“c = “<<c<<endl;

a=show();

b=show1();

sum=a+b+c;

cout<<“Sum of given numbers: “<<sum<<endl;

}

~derived3()

{

cout<<“Destructor of derived3 class is

executed”<<endl;

}

};

void main()

{

clrscr();

cout<<“Getting data for first object”<<endl;

{

derived3 d3;

d3.show3();

}

cout<<“Enter data for second object”<<endl;

{

derived2 d2;

d2.show2();

}

getch();

}

Leave a Reply

Your email address will not be published. Required fields are marked *