Write the definition for a class name Vector2D that stores information about a t
ID: 3680470 • Letter: W
Question
Write the definition for a class name Vector2D that stores information about a two-dimensional vector. The class should have functions to get and the set the x and y components, 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 X Bx) + (Ay X By). Save the Vector2D class declaration in a file called vector2d.h. Save the implementation of the class in a file called vector2d.cpp. Below is the testing program.
#include "vector2D.h"
int main()
{
Vector2D A, B;
A.setX(1.0);
A.setY(2.0);
B.setX(3.0);
B.setY(4.0);
cout << "The dot product A.B is " << A*B << endl;
return 0;
}
Explanation / Answer
vector2D.h
class Vector2D{
public:
double X,Y;
void setX(double x){
X=x;
}
void setY(double y){
Y=y;
}
double operator*(Vector2D &b){
double sum=this->X *b.X+this->Y*b.Y;
return sum;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.