In vector algebra we define a three dimensional vector v to be a on ordered trip
ID: 3815225 • Letter: I
Question
In vector algebra we define a three dimensional vector v to be a on ordered triple v = (x, y, z) where x, y and z are real numbers. We also define vector addition to be component wise this means that: If v(s, t, u) and w = (x, y, z) then v + w = (s+x, t+y, u+z). Create a new vector class in C++ that has a constructor that initializes its instances to (0, 0, 0). Add a set Components function that will mutate (modify) the vector instance and set its components to the three parameters x, y, z passes to the function respectively. Add an add function add(v) that adds v to the current vector. Add a display function that displays the vector as triple (x, y, z) Write a main function that creates two instances and correctly add them display all three vectors. My Complex class should serve as a good guideline.Explanation / Answer
#include<iostream>
using namespace std;
class vector
{
public:
int x,y,z;
vector()
{
x=0;
y=0;
z=0;
}
void read(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
vector operator +(vector b)
{
vector c;
c.x=x+b.x;
c.y=y+b.y;
c.z=z+b.z;
return c;
}
void display()
{
cout<<"("<<x<<","<<y<<","<<z<<")";
}
};
int main()
{
vector v1,v2,v3;
v1.read(1,2,3);
cout<<"First Vector Is : ";
v1.display();
v2.read(4,5,6);
cout<<" Second Vector Is : ";
v2.display();
v3=v1+v2;
cout<<" The Sum of Vectors : ";
v3.display();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.