Hybrid Inheritance in C++
Hybrid Inheritance in C++
Hybrid Inheritance : Hybrid inheritance is combination of two or more inheritances such
as single , multiple , multilevel or Hierarchical inheritances. In hybrid inheritance, within the same class, we can have elements of single inheritance , multiple inheritance , multilevel inheritance , and hierarchical inheritance.
SYNTAX :
class Base {
... .. ...
};
class Derived1 : virtual public Base {
... .. ...
};
class Derived2 : virtual public Base {
... .. ...
};
class Derived3 : public Derived1, public Derived2 {
... ... ...
};
CODE :
#include <iostream>
using namespace std;
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;
}
OUTPUT :
Sum= 14
Topics Covered :
- hybrid inheritance in c++
- virtual inheritance in c++
- virtual inheritance c++ example
- hybrid inheritance
- c++ tutorial
- c++ virtual inheritance
- c++ tutorial for beginners
Thanks for reading this blog. Hope you get satisfied with the blog and definitely this blog must have valued your time and effort of reading.
Take a time to connect our other digital creations such as Instagram , Facebook and Youtube.
Social Media Links of Tech DCode :
YouTube : https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
Instagram : https://www.instagram.com/thetechdcode/
Facebook Page : https://www.facebook.com/thetechdcode
Twitter : https://twitter.com/thetechdcode
Telegram Channel : https://t.me/thetechdcode
Tech DCode Linktree : https://linktr.ee/thetechdcode
My Personal Handles : https://linktr.ee/virtualshivamin
Social Media Links of SHIVAM SINGH (OWNER) :
Instagram : https://www.instagram.com/virtualshivamin/
Facebook Page : https://www.facebook.com/virtualshivamin/
Twitter : https://twitter.com/virtualshivamin/
Personal Linktree : https://linktr.ee/virtualshivamin