fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A {
  5. virtual void f(int a=1) = 0;
  6. };
  7. struct B:A {
  8. void f(int a) override ;
  9. };
  10. struct C:A {
  11. void f(int a=2) override ;
  12. };
  13. void B::f(int a) // definition
  14. {
  15. cout<<"B"<<a<<endl;
  16. }
  17. void C::f(int a) // definition
  18. {
  19. cout<<"C"<<a<<endl;
  20. }
  21. int main() {
  22. B b;
  23. C c;
  24. A *x=&c, *y=&b; // points to the same objects but using a polymorphic pointer
  25. x->f(); // default value defined for A::f() but with the implementation of C ==> C1
  26. y->f(); // default value defined for A::f() but with the implementation of B ==> B1
  27. // b.f(); // default not defined for B::f();
  28. c.f(); // default value defined for C::f(); ==> C2
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
C1
B1
C2