Problem: PLEASE COMPLETE THE CODE WHEREVER NECESSARY AND OUTPUT SHOULD BE AS BEL
ID: 3729489 • Letter: P
Question
Problem: PLEASE COMPLETE THE CODE WHEREVER NECESSARY AND OUTPUT SHOULD BE AS BELOW
Write the definition for a class named Vector2D 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-dimentional vectors A and B is equal to (Ax * Bx ) + (Ay * By)
Finally, write a main program that tests the three overloaded operators.
(10,0 ) * (0,10) = 0
(0,10) * ( 10,10) = 100
(10,10)*(5,4) = 90
Input
Enter two number for vector 1
10 0
Enter two number for vector 2
0 10
Enter two number for vector 3
0 10
Enter two number for vector 4
10 10
Enter two number for vector 5
10 10
Enter two number for vector 6
5 4
Output
(10,0) * (0,10) = 0
(0,10) * (10,10) = 100
(10,10) * (5,4) = 90
//new.cpp
// Operator Overloading
#include <iostream>
using namespace std;
class Vector2D {
public:
private:
};
Vector2D Vector2D::operator *(const Vector2D& number)
{
}
void Vector2D :: setx(int nx)
{
}
void Vector2D :: sety(int nx)
{
…..
}
// declare function sety()
int Vector2D:: getx() const
{
…
}
//declare function get y
int Vector2D:: gety() const
{
…
}
int main( )
{
int x,y;
Vector2D v1, v2, v3, v4, v5, v6;
cout << "Enter 2 number for vector 1" << endl;
cin >> x >> y;
…
}
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//completed cpp code
#include <iostream>
using namespace std;
class Vector2D {
public:
//defining all method prototypes
int operator *(const Vector2D&);
void setx(int);
void sety(int);
int getx() const;
int gety() const;
private:
//defining attributes
int x;
int y;
};
/* '*' operator overloaded method to find and return the dot product of this vector
and another vector */
int Vector2D::operator *(const Vector2D& number)
{
//formula for dot product=(Ax * Bx ) + (Ay * By)
int result=(x*number.getx())+(y*number.gety());
return result;
}
//sets the value of x
void Vector2D :: setx(int nx)
{
x=nx;
}
//sets the value of y
void Vector2D :: sety(int ny)
{
y=ny;
}
//gets the value of x
int Vector2D:: getx() const
{
return x;
}
//gets the value of y
int Vector2D:: gety() const
{
return y;
}
int main()
{
int x,y;
Vector2D v1, v2, v3, v4, v5, v6;
cout << "Enter 2 number for vector 1" << endl;
cin >> x >> y;
//setting x,y values for v1
v1.setx(x);
v1.sety(y);
cout << "Enter 2 number for vector 2" << endl;
cin >> x >> y;
//setting x,y values for v2
v2.setx(x);
v2.sety(y);
cout << "Enter 2 number for vector 3" << endl;
cin >> x >> y;
//setting x,y values for v3
v3.setx(x);
v3.sety(y);
cout << "Enter 2 number for vector 4" << endl;
cin >> x >> y;
//setting x,y values for v4
v4.setx(x);
v4.sety(y);
cout << "Enter 2 number for vector 5" << endl;
cin >> x >> y;
//setting x,y values for v5
v5.setx(x);
v5.sety(y);
cout << "Enter 2 number for vector 6" << endl;
cin >> x >> y;
//setting x,y values for v6
v6.setx(x);
v6.sety(y);
//displaying dot product of v1 and v2
cout<<"Vector 1 ("<<v1.getx()<<","<<v1.gety()<<") * Vector 2 ("<<v2.getx()<<","<<v2.gety()<<")= "<<v1*v2<<endl;
//displaying dot product of v3 and v4
cout<<"Vector 3 ("<<v3.getx()<<","<<v3.gety()<<") * Vector 4 ("<<v4.getx()<<","<<v4.gety()<<")= "<<v3*v4<<endl;
//displaying dot product of v5 and v6
cout<<"Vector 5 ("<<v5.getx()<<","<<v5.gety()<<") * Vector 6 ("<<v6.getx()<<","<<v6.gety()<<")= "<<v5*v6<<endl;
return 0;
}
/*OUTPUT*/
Enter 2 number for vector 1
10 0
Enter 2 number for vector 2
0 10
Enter 2 number for vector 3
0 10
Enter 2 number for vector 4
10 10
Enter 2 number for vector 5
10 10
Enter 2 number for vector 6
5 4
Vector 1 (10,0) * Vector 2 (0,10)= 0
Vector 3 (0,10) * Vector 4 (10,10)= 100
Vector 5 (10,10) * Vector 6 (5,4)= 90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.