fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. public:
  7. virtual void foo() { cout << "A" << endl; }
  8. };
  9.  
  10. class B : public A
  11. {
  12. public:
  13. virtual void foo() { cout << "B" << endl; }
  14. };
  15.  
  16. int main()
  17. {
  18. B b;
  19. b.foo(); //--> B
  20.  
  21. A a = b;
  22. a.foo(); //--> A
  23.  
  24. A& ra = b;
  25. ra.foo(); //--> B
  26.  
  27. return 0x0;
  28. }
Success #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
B
A
B