fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class Vector {
  5. T values [2];
  6. public:
  7. Vector (T first, T second){
  8. values[0]=first; values[1]=second;
  9. }
  10. int &operator[](int index){
  11. return values[index];
  12. }
  13. Vector<T> operator+(Vector<T> &other_vec){
  14. return Vector<T> (values[0]+other_vec[0],values[1]+other_vec[1]);
  15. }
  16. Vector<T> operator-(Vector<T> &other_vec){
  17. return Vector<T> (values[0]-other_vec[0],values[1]-other_vec[1]);
  18. }
  19. };
  20.  
  21. template <typename U>
  22. Vector<U> operator*(Vector<U> &a, int &b){
  23. return Vector<U> (a[0]*b, a[1]*b);
  24. }
  25. template <typename U>
  26. Vector<U> operator*(int &b, Vector<U> &a){
  27. return Vector<U> (a[0]*b, a[1]*b);
  28. }
  29.  
  30. int main(){
  31. Vector<int> test (1,2);
  32. Vector<int> test2 (3,4);
  33. int a = 5;
  34. std::cout << test[0] << " " << test[1] << std::endl;
  35. Vector<int> test3 = a*test;
  36. std::cout << test3[0] << " " << test3[1] << std::endl;
  37.  
  38. return 0;
  39. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty