Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Vector Overlo ads Use the following code that declares a vector class as a start

ID: 3669065 • Letter: V

Question

Vector Overloads

Use the following code that declares a vector class as a starting point:

SPECIFICATION

#ifndef __VECTOR__

#define __VECTOR__

#include <iostream>

#include <math.h>

using namespace std;

// CLASS SPECIFICATION

class Vector {

private:

// DATA MEMBERS

double _v_x;

double _v_y;

double _magnitude;

// HELPER METHODS

void CalculateMagnitude();

public:

// CONSTRUCTOR(S)

Vector();

Vector(double _v_x, double _v_y);

// SETTERS

void SetVX(double _v_x);

void SetVY(double _v_y);

// GETTERS

double GetVX();

double GetVY();

double GetMagnitude();

// OPERATIONS

Vector AddVector(Vector addMe);

Vector SubtractVector(Vector subtractMe);

// DISPLAY METHODS

void Display();

};

#endif

IMPLEMENTATION

#include "W07-03-Vector.h"

// METHOD IMPLEMENTATIONS

// helper methods

voidVector::CalculateMagnitude() {

_magnitude = sqrt( pow(abs(_v_x), 2) + pow(abs(_v_y), 2) );

}

// constructors

Vector::Vector() {

_v_x = 0.0;

_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

voidVector::SetVX(double _v_x) {

this->_v_x = _v_x;

CalculateMagnitude();

}

voidVector::SetVY(double _v_y) {

this->_v_y = _v_y;

CalculateMagnitude();

}

// getters

doubleVector::GetVX() {

return _v_x;

}

doubleVector::GetVY() {

return _v_y;

}

doubleVector::GetMagnitude() {

return _magnitude;

}

// operations

Vector Vector::AddVector(Vector addMe) {

// create a temp vector

Vector returnMe;

// add corresponding vector components

returnMe.SetVX(_v_x + addMe.GetVX());

returnMe.SetVY(_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(_v_x - subtractMe.GetVX());

returnMe.SetVY(_v_y - subtractMe.GetVY());

// return the tempVector

return returnMe;

}

// display methods

voidVector::Display() {

cout << "<" << this->_v_x << ", " << this->_v_y << ">";

}

Rewrite the Vector class so that it properly utilizes overloaded operators to perform basic operations. Specifically, overload the +, -, =, and << operators. The = operator should be overloaded to allow the user to make a direct assignment to an already existing vector. The following code should work after the overload:

Vector myFirstVector(4, 5);

Vector mySecondVector;

mySecondVector = myFirstVector;

The + and - operators should be overloaded to allow the user to intuitively add and subtract two vectors and store the result with the overloaded = operator. The following code should work after the overload:

resultVector = myFirstVector + mySecondVector;

resultVector = myFirstVector - mySecondVector;

The << operator should be overloaded to cause the vector to neatly display on the screen using vector notation. Use the format illustrated in the example and mimic those results when writing this operator overload. The following code would cause the vector to display on the screen followed by a newline:

cout << myFirstVector << endl;

After you have written your class, write a driver that demonstrates each of the overloaded operations. Declare some Vectors and put arbitrary data in them. Make assignments, add the vectors, subtract them, and display them on the screen with cout.

Explanation / Answer

#include 02 #include 03 04 using namespace std; 05 06 int main(int argc, char** argv) { 07       08     vector vectorOne(10,5); 09       10     vector vectorTwo(vectorOne); 11       12     return EXIT_SUCCESS; 13 }