1
0

15_vectors.cpp 487 B

123456789101112131415161718192021222324252627282930
  1. #include <iostream>
  2. using namespace std;
  3. class Vector
  4. {
  5. public:
  6. int x;
  7. int y;
  8. Vector(int x, int y) : x(x), y(y) {}
  9. Vector operator+(const Vector &right)
  10. {
  11. Vector result(x + right.x, y + right.y);
  12. return result;
  13. }
  14. };
  15. std::ostream &operator<<(std::ostream &Str, Vector const &v)
  16. {
  17. Str << "[" << v.x << "," << v.y << "]";
  18. return Str;
  19. }
  20. int main()
  21. {
  22. Vector x(2, 3);
  23. Vector y(4, 5);
  24. cout << "Sum of vectors " << x << " and " << y << " is " << x + y << endl;
  25. return 0;
  26. }