Hybrid Inheritance in C++.
- Hybrid inheritance is the process of combining of more than one type of inheritance.
- Following syntax will make you understand hybrid inheritance.
class A
{
.........
};
class B : public A
{
..........
} ;
class C
{
...........
};
class D : public B, public C
{
...........
};
// Hybrid inheritance implementation.
#include <iostream.h>
#include <conio.h>
class A
{
public:
int x;
};
class B : public A
{
public:
B() //constructor to initialize x in base class A
{
x = 10;
}
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};
int main()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
}
Expected Output:-
Sum= 14
// Hybrid inheritance implementation.
#include <iostream.h>
#include <conio.h>
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Expected Output:-
Enter the value of 'a' :
10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000
Tags:
c++ by mskuthar
Hybrid Inheritance in C++.
Hybrid Inheritance.
Inheritance in c++
mskuthar
www.mskuthar.blogspot.com