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, three_d &obj);
  10. friend ostream &operator<<(ostream &stream, const three_d &obj);
  11. operator int() {cout << "converting to int"<< endl; return x*y*z;}
  12. };
  13. ostream &operator<< (ostream &stream, three_d &obj)
  14. {
  15. stream << "(non-const) ";
  16. stream << obj.x << ", ";
  17. stream << obj.y << ", ";
  18. stream << obj.z << endl;
  19. return stream;
  20. }
  21. ostream &operator<< (ostream &stream, const three_d &obj)
  22. {
  23. stream << "(const) ";
  24. stream << obj.x << ", ";
  25. stream << obj.y << ", ";
  26. stream << obj.z << endl;
  27. return stream;
  28. }
  29. three_d three_d::operator+ (three_d op2)
  30. {
  31. cout << "adding" << endl;
  32. x+=op2.x;
  33. y+=op2.y;
  34. z+=op2.z;
  35. return *this;
  36. }
  37.  
  38. int main()
  39. {
  40. three_d a(1, 2, 3), b(2, 3, 4);
  41. cout << a << b;
  42. cout << "b+100" << endl;
  43. cout << b+100 << endl; //31 line
  44. cout << "a+b" << endl;
  45. cout << a+b << endl; // 32
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 2900KB
stdin
Standard input is empty
stdout
(non-const) 1, 2, 3
(non-const) 2, 3, 4
b+100
converting to int
124
a+b
adding
(const) 3, 5, 7