fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class MyVector
  6. {
  7. public:
  8. double x, y, z;
  9. MyVector& operator *= (double c)
  10. {
  11. x *= c;
  12. y *= c;
  13. z *= c;
  14. return *this;
  15. }
  16. };
  17.  
  18. MyVector operator*(const MyVector &v, double c) {
  19. MyVector temp(v);
  20. temp *= c;
  21. return temp;
  22. }
  23.  
  24. MyVector operator*(double c, const MyVector &v) {
  25. return v*c; // using MyVector operator*(const MyVector &v, double c)
  26. }
  27.  
  28. ostream& operator<<(ostream&os, const MyVector& v) {
  29. return os << '{' << v.x << ',' << v.y << ',' << v.z << '}';
  30. }
  31.  
  32. int main() {
  33. MyVector v; v.x=1; v.y=2; v.z=3;
  34.  
  35. MyVector v1 = v*2;
  36. cout << v1 << endl;
  37. MyVector v2 = 2*v;
  38. cout << v2 << endl;
  39. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
{2,4,6}
{2,4,6}