Wednesday, June 21, 2017

The Diamond Problem & Virtual Inheritance

/*This code was written by A. El-Gadi of Tripoli University for educational purposes. It illustrates the solution of the diamond problem in C++ using virtual inheritance. For any questions  and discussion use the comment section below.*/
 
#include<iostream>

using namespace std;


class Bar
{
public:
    int k;
    void DoSomething() { k=10; }
};

class Foo : virtual public Bar
{
public:
    int h;
    void DoSpecific() { k=k+4; h=5; }
};

class Moo : virtual public Bar
{
public:
    int h;
    void DoSpecific() { k=k+4; h=5; }
};

int main(){Bar b1; Foo f1; Moo m1;

b1.DoSomething();
f1.DoSomething();
m1.DoSpecific();

cout<<b1.k;
return 0;
}

No comments:

Post a Comment