fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class three_d {
  5. int x, y, z;
  6. public:
  7. three_d(int a, int b, int c) {x=a; y=b, z=c; }
  8. three_d operator+(three_d op2);
  9. friend ostream &operator<<(ostream &stream, const three_d &obj);
  10. };
  11. ostream &operator<< (ostream &stream, const three_d &obj)
  12. {
  13. stream << obj.x << ", ";
  14. stream << obj.y << ", ";
  15. stream << obj.z << endl;
  16. return stream;
  17. }
  18. three_d three_d::operator+ (three_d op2)
  19. {
  20. return three_d(x + op2.x, y + op2.y, z + op2.z);
  21. }
  22.  
  23. int main()
  24. {
  25. three_d a(1, 2, 3), b(2, 3, 4);
  26. cout << a+b << endl; // 32
  27. return 0;
  28. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
3, 5, 7