Problem1:

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



Vector2D v;

cin >> v;

cout << v;



Finally, write a main program that tests the three overloaded operators.

Sample output (bolded text denote input from user)



(10,0) * (0,10) = 0

(0,10) * (10,10) = 100

(10,10) * (5,4) = 90

You may use the following main() function:

int main()

{

Vector2D v1, v2, v3, v4;

cin>>v1>>v2>>v3>>v4;

cout << v1 <<" * "<< v2<< " = " << v1*v2 << endl;

cout << v2 <<" * "<< v3<< " = " << v2*v3 << endl;

cout << v3 <<" * "<< v4<< " = " << v3*v4 << endl;

return 0;



}

WHAT I HAVE SO FAR:

    # include <iostream>
    # include <vector>
    # include<cmath>


class Vector2D
{
    public:
            Vector2D(void);
            Vector2D(double nx, double ny);
            Vector2D(bool v);
            void normalize(void);
            double magnitude(void);
            void full_dump(void) const;
            bool valid;
            double x,y;

};

double dot(Vector2D v, Vector2D w);

Vector2D operator+(Vector2D v, Vector2D w);

Vector2D operator*(Vector2D v, Vector2D w);

Vector2D operator*(Vector2D v, double w);

Vector2D operator*(double w, Vector2D v);

inline Vector2D::Vector2D(void)
{
    x=y=0;
    valid=true;
}

inline Vector2D::Vector2D(double nx, double ny)
{
    x=nx;
    y=ny;
    valid=true;
}

inline Vector2D::Vector2D(bool v)
{
    x=y=0;
    valid=v;
}

inline Vector2D operator+(Vector2D v, Vector2D w)
{
    return Vector2D(v.x + w.x, v.y + w.y);
}

inline Vector2D operator*(Vector2D v, Vector2D w)
{
    return Vector2D(v.x * w.x, v.y * w.y);
}

inline Vector2D operator*(Vector2D v, double w)
{
    return Vector2D(v.x * w, v.y * w);
}

inline Vector2D operator*(double w, Vector2D v)
{
    return v*w;
}

inline Vector2D operator/(Vector2D v, Vector2D w)
{
    return Vector2D (v.x / w.x, v.y / w.y);
}

inline Vector2D operator/(Vector2D v, double w)
{
    return Vector2D (v.x / w, v.y / w);
}

inline Vector2D operator/(double w, Vector2D v)
{
    return Vector2D (w / v.x, w / v.y);
}

inline void Vector2D::normalize(void)
{
    double mag=magnitude();
    x/=mag;
    y/=mag;
}

inline double Vector2D::magnitude(void)
{
    return sqrt(x*x+y*y);
}

inline Vector2D cross(Vector2D v, Vector2D w)
{
    return Vector2D(0,0);
}

inline double dot(Vector2D v, Vector2D w)
{
    Vector2D x=w*v;
    return x.x + x.y;
}
int main()

{

Vector2D v1, v2, v3, v4;

cin>>v1>>v2>>v3>>v4;

cout << v1 <<" * "<< v2<< " = " << v1*v2 << endl;

cout << v2 <<" * "<< v3<< " = " << v2*v3 << endl;

cout << v3 <<" * "<< v4<< " = " << v3*v4 << endl;

return 0;

}

obviously what I have for the main function wont work, I need help fixing it so that the output will be the same as the example given because im not sure how to overload it. any help is much appreciated! thanks!

For the main to work you would have to change the output lines to

cout << v1 <<" * "<< v2<< " = " << dot(v1, v2) << endl;

All of your methods pass by value, Vector2D does not contain 2 much data, 2 doubles and a bool so about 20 bytes but I would say it is beginning to get to the point where passing in const references would not hurt

double dot(Vector2D v, Vector2D w);

becomes

double dot(const Vector2D& v, const Vector2D& w);

with the way you have already implemented operator*

Vector2D operator*(Vector2D v, Vector2D w);

there is no way for you to overload the operator so that it produces a dot product because your method dot and operator* have the same parameter list. I suspect you were not supposed to implement operator* as a multiplication in the first place.

This article has been dead for over six months. Start a new discussion instead.