Write a Cpp program to demonstrate the given hybrid inheritance using the concept of Virtual Base Class.

All QuestionsCategory: Cpp LanguageWrite a Cpp program to demonstrate the given hybrid inheritance using the concept of Virtual Base Class.
Chetan Shidling Staff asked 5 years ago

I need code.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
class grandparent
{
public:
void gpfunc()
{
cout<<“gp class function\n”;
}
};
class parent1 : public virtual grandparent
{
public:
void p1func()
{
cout<<“p1 class function\n”;
}
};
class parent2 : public virtual grandparent
{
public:
void p2func()
{
cout<<“p2 class function\n”;
}
};
class child : public parent1, public parent2
{
public:
void chfunc()
{
cout<<“child class function\n”;
}
};
int main()
{
child c;
c.gpfunc();
c.p1func();
c.p2func();
c.chfunc();
return 0;
}