I have an assignment for school where I have to rewrite a class so that it properly utilized the overloaded operators to perform basic operations, specifically the +, -, =, and << operators. I have been given a specification and an implementation. The implementation is as follows:
#include "Vector.h"
// helper methods
void Vector::CalculateMagnitude() {
this->_magnitude = sqrt( pow(abs(this->_v_x), 2) + pow(abs(this->_v_y), 2) );
}
// constructors
Vector::Vector() {
this->_v_x = 0.0;
this->_v_y = 0.0;
CalculateMagnitude();
}
Vector::Vector(double _v_x, double _v_y) {
this->_v_x = _v_x;
this->_v_y = _v_y;
CalculateMagnitude();
}
// setters
void Vector::SetVX(double _v_x) {
this->_v_x = _v_x;
CalculateMagnitude();
}
void Vector::SetVY(double _v_y) {
this->_v_y = _v_y;
CalculateMagnitude();
}
// getters
double Vector::GetVX() {
return this->_v_x;
}
double Vector::GetVY() {
return this->_v_y;
}
double Vector::GetMagnitude() {
return this->_magnitude;
}
// operations
Vector Vector::AddVector(Vector addMe) {
// create a temp vector
Vector returnMe;
// add corresponding vector components
returnMe.SetVX(this->_v_x + addMe.GetVX());
returnMe.SetVY(this->_v_y + addMe.GetVY());
// return the tempVector
return returnMe;
}
Vector Vector::SubtractVector(Vector subtractMe) {
// create a tempVector
Vector returnMe;
// subtract corresponding vector components
returnMe.SetVX(this->_v_x - subtractMe.GetVX());
returnMe.SetVY(this->_v_y - subtractMe.GetVY());
// return the tempVector
return returnMe;
}
// display methods
void Vector::Display() {
cout << "<" << this->_v_x << ", " << this->_v_y << ">";
}
I am wondering if I achieve this by getting rid of the section under "// operations"? Also, what do I need to add to the specification file? I have been having a really hard time with this class lately and I'm not grasping concepts the way I used to. Any help with this assignment would be appreciated. I really am not asking for anyone to do the assignment for me obviously, I really want to learn and get good at this but I just need a little help getting started.
If any additional information is needed, please let me know and I can update this question.