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

1. Overload the + symbol to become vector addition. //In the example below how c

ID: 3573555 • Letter: 1

Question

1. Overload the + symbol to become vector addition. //In the example below how can I convert void add(vector v) to vector operator+(vector v)

2. Overload the rational == to yield true if v and w has the same length.

3. Write a generic function that calculates the length for an arbitrary “measurable” type using If v = (s, t, u , ... ,) Then length(v) = s2 + t2 + ... Meaning that the length is the square root of the sum of the components squared

4. Write a main function that creates two instances and correctly add using + and =, display all three vectors and the length of each of the three vectors

#include<iostream>
using namespace std;
//class definition
class vector
{
int x,y,z;//private and thus not accessible by non member function
public:
vector()
{
x=0;y=0;z=0;//it initializes x=y=z=0 this is contructor definition
}
void setcomponents(int r,int s,int t)
{
x=r;y=s;z=t; //sets x,y,z according to passed parameters
}

void add(vector v)
{
vector w;   
w.x=x+(v.x);w.y=y+(v.y);w.z=z+(v.z); //this adds the passed and current parameters of class vector and stores to the third vector
cout<<"sum is ("<<w.x<<","<<w.y<<","<<w.z<<")"<<endl;//prints the sum
}
void display()
{
cout<<"vector is ("<<x<<","<<y<<","<<z<<")"<<endl;//displays vector
}

};

int main()
{
vector n,m;//creating instances
n.setcomponents(2,3,4);m.setcomponents(1,4,5);//setting x,y,z to passed parameters
n.display();m.display(); //and displays
n.add(m); //adds
return 0;

}

Explanation / Answer

Please refer to comments for details.

#include<iostream>
#include<cmath>
using namespace std;
class vector
{
   int x,y,z;//private and thus not accessible by non member function
   public:
   vector()
   {
       x=0;y=0;z=0;//it initializes x=y=z=0 this is contructor definition
   }
   void setcomponents(int r,int s,int t)
   {
       x=r;y=s;z=t; //sets x,y,z according to passed parameters
   }
   friend vector operator+(const vector &v1, const vector &v2); // operator +
   friend bool operator==(const vector &v1, const vector &v2); // operator ==

   void length(){ // length function
       cout<<"Length is:"<< sqrt(x*x + y*y + z*z)<<endl;
   }

   /*void add(vector v)
   {
       vector w;   
       w.x=x+(v.x);w.y=y+(v.y);w.z=z+(v.z); //this adds the passed and current parameters of class vector and stores to the third vector
       cout<<"sum is ("<<w.x<<","<<w.y<<","<<w.z<<")"<<endl;//prints the sum
   }*/
   void display()
   {
       cout<<"vector is ("<<x<<","<<y<<","<<z<<")"<<endl;//displays vector
   }
};
vector operator+(const vector &v1, const vector &v2){
   vector tmp;
   tmp.x = v1.x+v2.x;
   tmp.y = v1.y+v2.y;
   tmp.z = v1.z+v2.z;
   return tmp;
}
bool operator==(const vector &v1, const vector &v2){
   //since no concept is introduced for size in given program so for now comapring the values of vector
   if (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z){
       return true;
   }
   return false;
}
int main()
{
   vector n,m;//creating instances
   n.setcomponents(2,3,4);m.setcomponents(1,4,5);//setting x,y,z to passed parameters
   n.display();n.length();
   m.display(); m.length();//and displays
   //n.add(m); // old custom add method
   cout<<"Testing + operator ";
   vector addition = n+m; // overloaded + operator method
   addition.display();addition.length(); // display
   vector addition2 = n+m;
   cout<<"Testing == operator ";
   addition.display();
   cout<<"and"<<endl;
   addition2.display();
   if (addition2 == addition){
       cout<<"are Equal vetors ";
   }else{
       cout<<"are Unequal vectors ";
   }
   return 0;
}