#include #include class vector2d { double x, y; public: vector2d() : x(0), y(0) {} vector2d(double ix, double iy) : x(ix), y(iy) {} double 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; } double dot(const vector2d& other) const { return x * other.x + y * other.y; } ~vector2d(){ std::cout << "Destructor Called\n"; } friend std::string to_string(const vector2d&); }; std::string to_string(const vector2d& to_convert) { return std::to_string(to_convert.x) + ", " + std::to_string(to_convert.y); } 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; }