Skip to content

Instantly share code, notes, and snippets.

@poanchen
Created September 19, 2017 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poanchen/0219a280947fac16c73ca8c1fb37d07c to your computer and use it in GitHub Desktop.
Save poanchen/0219a280947fac16c73ca8c1fb37d07c to your computer and use it in GitHub Desktop.
Create an interface class with a pure virtual function. Create another class that inherits from it, and give a concrete implementation of the pure virtual function.
// https://repl.it/LEHR
#include <iostream>
using namespace std;
class HelloWorldInterfaceA
{
public:
virtual void printHelloWorldA() = 0;
};
class HelloWorldInterfaceB : public virtual HelloWorldInterfaceA
{
public:
virtual void printHelloWorldB() = 0;
};
class HelloWorldObjectA : public virtual HelloWorldInterfaceA
{
public:
virtual void printHelloWorldA()
{
cout << "Hello World from A\n";
}
};
class HelloWorldObjectB : public HelloWorldObjectA, public virtual HelloWorldInterfaceB
{
public:
virtual void printHelloWorldB()
{
cout << "Hello World from B\n";
}
};
int main()
{
HelloWorldObjectB helloWorldObjectB;
for (int i = 0; i < 2; i++) {
helloWorldObjectB.printHelloWorldB();
}
for (int j = 0; j < 3; j++) {
helloWorldObjectB.printHelloWorldA();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment