Question: **** use visual studio C++, thank you**** In this project, you will implement operator overloading for a two-dimensional Vector class. Begin with the download of
**** use visual studio C++, thank you****
In this project, you will implement operator overloading for a two-dimensional Vector class. Begin with the download of main.cpp which implements the definition of the Vector class and, its driver program. There are three operators that must be overloaded insertion (<<), addition (+), and subtraction (-).
insertion (<<):
The insertion operator is used to send the values of a vector to an ostream object, if a vector has two values 2.0 and 3.1 it must insert into the string (2.0, 3.1). For example, code Segment A will produce the Output A
Segment A
Vector v(2.2, 3.1)
cout << Vector v1: << v << endl;
Output A
Vector v1: (2.2, 3.1)
addition (+):
The addition operator is used to sum two vectors together. The addition is pairwise, x is added to x, and y is added to y. The result of the operation is a new object that contains the sum of the two vectors as a single vector. Code given in Segment B produces the Output B
Segment B
Vector v1(2.0, 3.1), v2(4.3, 5.0);
cout << v1 + v2: << v1 + v2 << endl;
Output B
v1 + v2: (6.3, 8.1)
subtraction(-):
The subtraction operator is used to reduce one vector by another. Subtraction is pairwise, x is reduced x, and y is reduced y. The result of the operation is a new object that contains the difference of the two vectors as a single vector. Code given in Segment C produces the Output C
Segment C
Vector v1(2.0, 3.1), v2(4.3, 5.0);
cout << v1 - v2: << v1 - v2 << endl;
Output C
v1 - v2: (-2.3, -1.9)
Your task is to implement the methods and operators body of the Vector class. The program, main.cpp uses the Vector class to produce the program output below. Note, there are two vectors supplied by the user. You should get the same output.
******This the code*****
#include using namespace std; class Vector { public: /* Default constructor, sets x and y to zero */ Vector(); /* Parameterized constructor, sets x to Xval, and y to yVal */ Vector(float xVal, float yVal); /* Returns the floating point values of the x and y */ float getX() const; float getY() const; /* * Inserts into the stream the string "(, )" where * is the floating point value for x * is the floating point value for y */ friend ostream& operator<<(ostream& os, const Vector &v); /* * Sums two Vectors v1 and v2 together returning a new vector v3 * Where addition is defined as * v3.x = v1.x + v2.x * v3.y = v1.y + v2.y */ Vector operator+(const Vector &other) const; /* * Subtracts v2 from v1 returning a new vector v3 * Where addition is defined as * v3.x = v1.x - v2.x * v3.y = v1.y - v2.y */ Vector operator-(const Vector &other) const; private: float x, y; }; //--------------------------Start your code from here //--------------------------------------------------- int main() { float x, y; cout << "Enter the space separated x and y values for the first vector (v1): "; cin >> x >> y; Vector v1(x, y); cout << "Vector v1: " << v1 << endl; cout << "Enter the space separated x and y values for the second vector (v2): "; cin >> x >> y; Vector v2(x, y); cout << "Vector v2: " << v2 << endl; cout << "v1 + v2: " << v1 + v2 << endl; cout << "v1 - v2: " << v1 - v2 << endl; return 0; } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
