In: Computer Science
C++
Please read the question carefully and match the output example given at the end!
Question
In this project you will implement operator overloading for a
two dimensional Vector class. Begin with the declaration of the
Vector class on the final page. 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. Addition is pairwise, x is added to x, and y 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
by 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 B v1 - v2: (-2.3, -1.9)
Your task is to create a program divided into three files:
main.cpp, Vector.cpp, and Vector.h. The header (Vector.h) contains
only the declaration of the class found on the final page.
Vector.cpp implements the methods and operators of the Vector. The
program, main.cpp uses the Vector class to produce the program
output below. Note, there are two vectors supplied by the
user.
Output D Enter the space separated x and y values for the first
vector (v1): 23.1 15.3 Vector v1: (23.1, 15.3) Enter the space
separated x and y values for the second vector (v2): 1.7 2.3 Vector
v2: (1.7, 2.3) v1 + v2: (24.8, 17.6) v1 - v2: (21.4, 13)
#include <iostream>
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 "(<x>, <y>)"
where * <x> is the floating point value for
x
* <y> 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 subtraction 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; };
Editable code:
Main.CPP:
#include<iostream>
#include "Vector.h"
using namespace std;
int main()
{
double x, y;
cout << "Enter Vector 1 x and y value";
cin >> x>>y;
Vector v1(x, y);
cout << "Enter Vector 2 x and y value";
cin >> x>> y;
Vector v2(x, y);
cout << "\nVector v1 :"<<v1;
cout << "\nVector v2 :"<<v2;
cout << "\nv1+v2 :"<<v1 + v2;
cout << "\nv1-v2 :" << v1 - v2;
cout << "\n\n";
system("pause");
return 0;
}
Vector.CPP:
#include "Vector.h"
#include <cmath>
Vector::Vector()
{
x = 0.0;
y = 0.0;
}
Vector::Vector(float xVal, float yVal)
{
x = xVal;
y = yVal;
}
float Vector::getX() const
{
return x;
}
float Vector::getY() const
{
return y;
}
Vector Vector::operator+(const Vector &v) const
{
return Vector(x + v.x, y + v.y);
}
Vector Vector::operator-(const Vector &v) const
{
return Vector(x - v.x, y - v.y);
}
ostream& operator<<(ostream& os, const Vector &v)
{
os << v.getX()<<" ";
os << v.getY();
return os;
}
Vector.h:
#pragma once
#include <iostream>
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 "(<x>,< y>)" where * <x> is the floating point value for x
* <y> 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 subtraction 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;
};