In this assignment the student will implement the overloaded

In this assignment, the student will implement the overloaded operators on a pre-defined objects that represents a Vector. Use the following code that declares a vector class as a starting point:

Solution

vector.cpp

==================================================================

#include \"vector.h\"


void Vector::CalculateMagnitude()
{
   _magnitude=sqrt(pow(abs(_v_x),2)+pow(abs(_v_y),2));
}

Vector::Vector()
{
   _v_x=0.0;
   _v_y=0.0;
   CalculateMagnitude();

}

Vector::Vector(double _v_x,double _v_y)
{
   this->_v_x=_v_x;
       this->_v_y=_v_y;
   CalculateMagnitude();

}
    void Vector::SetVX(double _v_x)
    {
        this->_v_x=_v_x;
        CalculateMagnitude();
    }

    void Vector::SetVY(double _v_y)
    {
        this->_v_y=_v_y;
        CalculateMagnitude();
    }

    //GETTERS
    double Vector::GetVX()
    {
        return _v_x;
    }
    double Vector::GetVY()
    {
        return _v_y;
    }
    double Vector::GetMagnitude()
    {
        return _magnitude;
    }
    //OPERATIONS
    Vector Vector::AddVector(Vector addMe)
    {
        Vector returnme;

        returnme.SetVX(_v_x+addMe.GetVX());
        returnme.SetVY(_v_y+addMe.GetVY());

        return returnme;

    }
    Vector Vector::SubtractVector(Vector SubtractMe)
    {
        Vector returnme;
        returnme.SetVX(_v_x-SubtractMe.GetVX());
        returnme.SetVY(_v_y-SubtractMe.GetVY());

        return returnme;
    }

    void Vector::Display()
    {
        cout<<\"<\"<<this->_v_x<<\", \"<<this->_v_y<<\">\";
    }

    //OPEARTOR OVERLOAD
    Vector Vector::operator+ (Vector addMe)
    {
        Vector returnme;
        returnme.SetVX(_v_x+addMe.GetVX());
        returnme.SetVY(_v_y+addMe.GetVY());

        return returnme;


    }

    Vector Vector::operator-(Vector SubtractMe)
    {
        Vector returnme;
        returnme.SetVX(_v_x-SubtractMe.GetVX());
        returnme.SetVY(_v_y-SubtractMe.GetVY());

        return returnme;


    }

    /*Vector operator+ (Vector addMe1,Vector addMe2)
    {
        Vector returnme;
        returnme.SetVX(addMe1.GetVX()+addMe2.GetVX());
        returnme.SetVY(addMe1.GetVX()+addMe2.GetVY());

       

        return returnme;
    }


    Vector operator- (Vector SubtractMe1,Vector SubtractMe2)
    {
        Vector returnme;
        returnme.SetVX(SubtractMe1.GetVX()-SubtractMe2.GetVX());
        returnme.SetVY(SubtractMe1.GetVX()-SubtractMe2.GetVY());

       

        return returnme;
    }*/

    ostream& operator<<(ostream& o,Vector& v )
    {
       o<<\"<\"<<v._v_x<<\", \"<<v._v_y<<\">\";
       return o;
    }


    Vector Vector::operator=(Vector& Copy)
    {
        Vector returnme;
        returnme.SetVX(Copy.GetVX());
        returnme.SetVY(Copy.GetVX());

       

        return returnme;
    }

=========================================================

vector.h

#ifndef __VECTOR__
#define __VECTOR__
#include<bits/stdc++.h>
using namespace std;
//class sepcificaation
class Vector
{
private:
    double _v_x;
    double _v_y;
    double _magnitude;

    //HELPER METHODS
public:
    void CalculateMagnitude();


    //COONSTRUCTOR
    Vector();
    Vector(double _v_x,double _v_y);


    //SETORS

    void SetVX(double _v_x);
    void SetVY(double _v_y);

    //GETTERS
    double GetVX();
    double GetVY();
    double GetMagnitude();

    //OPERATIONS
    Vector AddVector(Vector addMe);
    Vector SubtractVector(Vector SubtractMe);

    //DISPLAY
    void Display();


    //OPERATORS INTERNAL
    Vector operator+ (Vector addMe);
    Vector operator-(Vector SubtractMe);
    Vector operator=(Vector& Copy);

    //   OPERATORS INTERNAL

    //friend Vector operator+ (Vector addMe1,Vector addMe2);
    //friend Vector operator-(Vector SubtractMe1,Vector SubtractMe2);
    friend ostream& operator<<(ostream& o,Vector& v );

};

#endif

==============================================

main.cpp

#include<bits/stdc++.h>
#include \"vector.cpp\"
using namespace std;


int main(int argc, char const *argv[])
{
   Vector c1(2,3);
   Vector c2(2,3);
   Vector c3=c1+c2;
   cout<<\"C3 is \"<<c3;

   Vector c4=c3;
   cout<<\"\ C4 is \"<<c4;


   Vector c5=c1-c2;
   cout<<\"\ C5 is \"<<c5;

  
   return 0;
}

==============================Outptut:=====================

akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
C3 is <4, 6>
C4 is <4, 6>

C5 is <4, 6>

In this assignment, the student will implement the overloaded operators on a pre-defined objects that represents a Vector. Use the following code that declares
In this assignment, the student will implement the overloaded operators on a pre-defined objects that represents a Vector. Use the following code that declares
In this assignment, the student will implement the overloaded operators on a pre-defined objects that represents a Vector. Use the following code that declares
In this assignment, the student will implement the overloaded operators on a pre-defined objects that represents a Vector. Use the following code that declares

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site