/*
By making the inheritance virtual, Derived3 will have only one copy of Base, which eliminates the ambiguity. Here's how this works:
Virtual inheritance ensures that only one copy of Base exists, even though Derived3 inherits Base through both Derived1 and Derived2.
When Derived3 calls print(), there is no ambiguity; it knows exactly which Base class function to use because there is only
one instance of Base.
Diagram Representation:
Base
/ \
Derived1 Derived2
\ /
Derived3
Without virtual inheritance, Derived3 would have two instances of Base. With virtual inheritance, there's just one shared Base instance.
*/
#include <iostream>
class Base {
public:
void print() {
std::cout << "Base class" << std::endl;
}
};
class Derived1 : virtual public Base {
public:
void derived1Print() {
std::cout << "Derived1 class" << std::endl;
}
};
class Derived2 : virtual public Base {
public:
void derived2Print() {
std::cout << "Derived2 class" << std::endl;
}
};
class Derived3 : public Derived1, public Derived2 {
public:
void derived3Print() {
std::cout << "Derived3 class" << std::endl;
}
};
int main() {
Derived3 d3;
d3.print(); // Now, there is no ambiguity in calling the base class function
d3.derived1Print();
d3.derived2Print();
d3.derived3Print();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: