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

Write the definition for a class named Vecton2D that stores information about a

ID: 3802857 • Letter: W

Question

Write the definition for a class named Vecton2D that stores information about a two-dimensional vector. The class should have methods 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 By Next, overload the and operators so that you can write the following code Vector 2D v; cin v cout Finally, write a main program that tests the three overloaded operators Sample output (10,0) (0,10) 0 (0,10) (10, 10) 100 (10, 10) (5,4) 90 You may use the following main0 function:

Explanation / 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


*/