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

For the Vector2D class, overload the + operator so that it returns the sum of tw

ID: 3682831 • Letter: F

Question

For the Vector2D class, overload the + operator so that it returns the sum of two vectors. The sum of two-dimensional vectors A and B is a new vector with x component given by Ax + Bx and y component given Ay + By. Note you will need to define a two argument constructor as well.
Overload the insertion operator << such that vectors are printed as (Ax,AY)
Overload the array operator [] so that vec_object[x] outputs the x component and vec_object[y] outputs the y component.

int main()
{
   Vector2D A, B, C;
   A.setX(1.0);
   A.setY(2.0);
   B.setX(3.0);
   B.setY(4.0);
   cout << A << " " << B << endl;
   cout << "The dot product A.B is " << A*B << endl;
   C = A + B;
   cout << C << endl;
   cout << C['x'] << " " << C['y'] << endl;
   return 0;
}

Explanation / Answer

//Vector2D.h
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include<iostream>
using namespace std;
class Vector2D
{
private:
   int x;
   int y;


public:
   //constructor
   Vector2D();
   void setX(int );
   void setY(int );

   int getX();
   int getY();


   //overloded operaors
   friend ostream & operator << (ostream &out, Vector2D &vec);
   friend Vector2D operator + (Vector2D &vec1,Vector2D &vec2);
   int operator [] (char ch);
};


#endif VECTOR2D_H

------------------------------------------------------------------------------------------------------------------------------

//Implementation file of Vector2D.h
//Vector2D.cpp
#include<iostream>
#include "Vector2D.h"
using namespace std;

//default constrcutor
Vector2D::Vector2D()
{
   x=0;
   y=0;
}

//Setter method to set x
void Vector2D::setX(int x)
{
   this->x=x;
}

//Setter method to set y
void Vector2D::setY(int y)
{
   this->y=y;
}

//Return x value
int Vector2D::getX()
{
   return x;
}

//Return y value
int Vector2D::getY()
{
   return y;
}

//Overload method of << opertor
ostream& operator <<(ostream &out, Vector2D &vec)
{
   out << "(" << vec.getX() <<","<<vec.getY()<<")"<<endl;
    return out;
}

//Overload subscript [] operator that retunrns
//the xcomponet of y coponent of vector
int Vector2D::operator [] (char ch)
{
   if(ch=='x')
       return x;
   else if(ch=='y')
       return y;
}

//Overload + operator that takes two vectors and return
//result vector
Vector2D operator + (Vector2D &vec1,Vector2D &vec2)
{
   Vector2D temp;
   temp.setX(vec1.getX()+vec2.getX());
   temp.setY(vec1.getY()+vec2.getY());

   return temp;
}
------------------------------------------------------------------------------------------------------------------------------

//Test program of vector2d class
#include<iostream>
//include header file Vector2D.h
#include "Vector2D.h"
using namespace std;

int main()
{
   Vector2D A, B, C;
   A.setX(1.0);
   A.setY(2.0);
   B.setX(3.0);
   B.setY(4.0);
   cout << A <<B<< endl;
   cout << "The A+B is ";
   //Calling overloaded + operator on two vectors
   C = A + B;
   cout<<"C= ";
   cout << C << endl;
   //Calling overloaded subscript []
   cout << C['x'] << " " << C['y'] << endl;

   system("pause");
   return 0;
}

-----------------------------------------------------------------------------------------------------------------------------

Sample output:

(1,2)
(3,4)

The A+B is C= (4,6)

4 6