fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct bb {
  5. virtual void a() {cout << "bb-a" << endl; }
  6. virtual void b() {cout << "bb-b" << endl; }
  7. };
  8.  
  9. struct d1 : public bb {
  10. virtual void a() {cout << "d1-a" << endl; }
  11. private:
  12. virtual void b() {cout << "d1-b" << endl; }
  13. };
  14.  
  15. struct d2 : public bb {
  16. virtual void b() {cout << "d2-b" << endl; }
  17. private:
  18. virtual void a() {cout << "d2-a" << endl; }
  19. };
  20.  
  21. int main() {
  22. d1 x1;
  23. x1.a();
  24. // x1.b(); // <<== This will not compile
  25. d2 x2;
  26. // x2.a(); // <<== This will not compile
  27. x2.b();
  28. bb &bx1(x1);
  29. bx1.a();
  30. bx1.b();
  31. bb &bx2(x2);
  32. bx2.a();
  33. bx2.b();
  34. return 0;
  35. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
d1-a
d2-b
d1-a
d1-b
d2-a
d2-b