fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Parent
  5. {
  6. public:
  7. Parent(int childId): id(childId) {}
  8. void printId() {cout << id << endl;}
  9. protected:
  10. int id;
  11. };
  12.  
  13. class Child1: public Parent
  14. {
  15. public:
  16. Child1(): Parent(42) {}
  17. void foo() {cout << "My id is " << id << endl;}
  18. };
  19.  
  20. class Child2: public Parent
  21. {
  22. public:
  23. Child2(): Parent(5566) {}
  24. void bar() {cout << id << " is the best!" << endl;}
  25. };
  26.  
  27. int main() {
  28. Child1 c1;
  29. c1.printId();
  30. c1.foo();
  31.  
  32. Child2 c2;
  33. c2.printId();
  34. c2.bar();
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
42
My id is 42
5566
5566 is the best!