#include #include template class vector2d { T x, y; public: vector2d() : x(0), y(0) {} vector2d(T ix, T iy) : x(ix), y(iy) {} T magnitude() const { // this is a pointer to the instance this was called on return sqrt(x*x + y*y); } vector2d operator+(const vector2d& other) const { vector2d result(x + other.x, y + other.y); return result; } vector2d operator-(const vector2d& other) const { vector2d result(x - other.x, y - other.y); return result; } T dot(const vector2d& other) const { return x * other.x + y * other.y; } ~vector2d(){ std::cout << "Destructor Called\n"; } std::string to_string() const { return std::to_string(x) + ", " + std::to_string(y); } }; template std::string to_string(const vector2d& to_convert) { return to_convert.to_string(); } template std::ostream& operator<<(std::ostream& out, const vector2d& to_print){ out << to_string(to_print); return out; } using namespace std; int main(){ vector2d a(4.2, 5.6); vector2d b(3.1, -26.2); cout << "Size of a: " << sizeof(a) << endl; // a.x = 7.4; cout << a.magnitude() << endl; // cout << a.x << endl; vector2d c; // a.add(b) c = a + b; // Goal for the end cout << a + b << endl; cout << "a dot b = " << a.dot(b) << endl; }