0

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.

  • What do you mean by "specification file"? What do you mean by "achieve this"? – Dmitry Kuzminov Apr 23 '20 at 21:04
  • Maybe you should read documentation about operator overloading: https://en.cppreference.com/w/cpp/language/operators. – dasfex Apr 23 '20 at 21:06
  • You don't overload any operators. – sweenish Apr 23 '20 at 21:06
  • Anyway, your implementation of `AddVector` may be improved. First, you shouldn't pass the argument by value, prefer passing by const reference: `Vector Vector::AddVector(const Vector &addMe)`. Next, much better would be to initialize `returnMe` in the constructor. – Dmitry Kuzminov Apr 23 '20 at 21:06
  • The specification file is Vector.h which was provided for me. By achieve this, I mean being able to perform this task of utilizing operator overloading – Nick Hammer Apr 23 '20 at 21:06
  • ^^^ or, since you're passing your arguments to these operations by value, you can save yourself a rebuild in most of them by just utilizing that by-value argument as your return value. Ex: in AddVector, `addMe` is by-value. So just do `addMe.SetVX(this->_v_x + addMe.GetVX());` etc, then return `addMe`. The `returnMe` is then avoided. – WhozCraig Apr 23 '20 at 21:07
  • Helpful reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Apr 23 '20 at 21:12
  • my assignment is to use operator overloading to do the adding and subtracting. I believe I am supposed to get rid of AddVector and SubtractVector methods altogether and rewrite using operator overloading. This is what I need help with – Nick Hammer Apr 23 '20 at 21:14
  • @NickHammer -- Why remove the functions? Just call them from the overloaded operator functions. – PaulMcKenzie Apr 23 '20 at 21:18

0 Answers0