Overloading Arithmetic Operators for the Vector
Class
Adding two vectors is very simple when you use x,y coordinates. You just add the two x components to get the x component of the answer and add the two y components to get the y component of the answer. From this description, you might be tempted to use this code:
Vector Vector::operator+(const Vector & b) const{ Vector sum; sum.x = x + b.x; sum.y = y + b.y; return sum; // incomplete version}
And this would be fine if the object stored only the x and y components. Unfortunately, this version of the code fails to set the polar values. You could fix this problem by adding more code:
Vector Vector::operator+(const Vector & b) const{ Vector sum; sum.x = x + b.x; sum.y ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.