White the definition for a class named Vector 2D that stores information about a
ID: 3803082 • Letter: W
Question
White the definition for a class named Vector 2D that stores information about a two dimensional vector. The class should have method to get and set the x component and the y component, where x and y are integers. Next, overload the * operator so that it returns the dot product of two vectors. The dot product of two dimensional vectors A and B is equal to (Ax * Bx) + (Ay * B) Next, overload the operators so that you can write the following code Finally, write a main program that tests the three overloaded operators. Sample output You may use the following main() function int main() {Vector 2D v1, v2; cin >>v1 >>v2; coutExplanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <limits.h>
#include <time.h>
using namespace std;
class Vector2D
{
private:
int x; // 0 to infinite
int y; // 0
public:
// required constructors
Vector2D(){
x = 0;
y = 0;
}
Vector2D(int f, int i){
x = f;
y = i;
}
friend ostream &operator<<( ostream &output,
const Vector2D &v )
{
if(v.x == 0 && v.y == 0)
output << "0";
else
output << "(" << v.x << "," << v.y << ")";
return output;
}
friend istream &operator>>( istream &input, Vector2D &v )
{
input >> v.x >> v.y;
return input;
}
Vector2D operator *(Vector2D v) /* Operator Function */
{
Vector2D temp;
temp.x=x*v.x;
temp.y=y*v.y;
return temp;
}
};
int main()
{
Vector2D v1, v2;
cout << " Input x and y for a vector: ";
cin >> v1;
cout << " Input x and y for a vector: ";
cin >> v2;
cout << v1 <<" * "<< v2<< " = " << v1*v2 << endl;
return 0;
}
/*
output:
Input x and y for a vector: 10 0
Input x and y for a vector: 0 10
(10,0) * (0,10) = 0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.