fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace simple {
  5.  
  6. class InterfaceA {
  7. public:
  8. virtual ~InterfaceA() {}
  9.  
  10. virtual int MethodOne() const = 0;
  11. };
  12.  
  13. class InterfaceB : virtual public InterfaceA { // <-- note the virtual keyword
  14. public:
  15. ~InterfaceB() override {}
  16.  
  17. virtual int MethodTwo() const = 0;
  18. };
  19.  
  20. class ImplA : virtual public InterfaceA { // <-- note the virtual keyword
  21. public:
  22. ImplA(int to_return);
  23. ~ImplA() override {}
  24.  
  25. int MethodOne() const override;
  26.  
  27. private:
  28. int to_return_;
  29. };
  30.  
  31. class ImplB : public InterfaceB, public ImplA {
  32. public:
  33. ImplB();
  34. ~ImplB() override {}
  35.  
  36. //int MethodOne() const override;
  37. int MethodTwo() const override;
  38. };
  39.  
  40. ImplA::ImplA(int to_return) : to_return_(to_return) {}
  41. int ImplA::MethodOne() const { return to_return_; }
  42.  
  43. ImplB::ImplB() : ImplA(5) {}
  44. // int ImplB::MethodOne() const { return ImplA::MethodOne(); }
  45. int ImplB::MethodTwo() const { return 2; }
  46. } // namespace simple
  47.  
  48.  
  49. int main() {
  50. simple::ImplA implA(100);
  51. cout << implA.MethodOne() << endl << endl;
  52.  
  53. simple::ImplB implB;
  54. cout << implB.MethodOne() << endl;
  55. cout << implB.MethodTwo() << endl;
  56.  
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
100

5
2