fork download
  1. #include <iostream>
  2.  
  3. class A {
  4. private:
  5. int a;
  6. public:
  7. A(int a) : a(a) { }
  8. void print() { std::cout << a; }
  9.  
  10. A& operator+=(const A &other) {
  11. a += other.a;
  12. return *this;
  13. }
  14. };
  15.  
  16. class B : public A {
  17. private:
  18. int b;
  19. public:
  20. B(int a, int b) : A(a), b(b) { }
  21. void print() {
  22. std::cout << '(';
  23. A::print();
  24. std::cout << ", " << b << ')' << std::endl;
  25. }
  26.  
  27. B& operator+=(const B &other) {
  28. A::operator+=(other);
  29. b += other.b;
  30. return *this;
  31. }
  32. };
  33.  
  34. int main() {
  35. B b1(1, 2);
  36. B b2(3, 4);
  37. b1.print();
  38. b1 += b2;
  39. b1.print();
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
(1, 2)
(4, 6)