fork download
  1. #include <iostream>
  2.  
  3. class A {
  4. public:
  5. A() : x(-1) {}
  6. A(int num) : x(num) {}
  7. int getX() { return x; }
  8. private:
  9. const int x;
  10. };
  11.  
  12. class B : public A {
  13. public:
  14. B() : A() {}
  15. B(int num) : A(num) {}
  16. };
  17.  
  18. class C : public B {
  19. public:
  20. C(int num) : B() {}
  21. };
  22.  
  23. int main() {
  24. B b(2);
  25. C c(5);
  26.  
  27. //should print 2
  28. std::cout << "b: " << b.getX() << std::endl;
  29.  
  30. //should print -1
  31. std::cout << "c: " << c.getX() << std::endl;
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
b: 2
c: -1