Created
September 19, 2017 04:10
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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