In vector algebra we define a three dimensional vector v to be a on ordered trip
ID: 3818971 • 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
/*
C++ program that test Vector class and print
results of add method.
*/
//MyComplex.cpp
//include header files
#include<iostream>
//inclue Vector class
#include "Vector.h"
using namespace std;
int main()
{
//create two objects of vector class
Vector v(1,2,3);
Vector w(1,2,3);
cout<<"v";
v.display();
cout<<"w";
w.display();
cout<<"Result :";
//calling add method
v.add(w);
v.display();
//pause program output on console.
system("pause");
return 0;
}
------------------------------------------------------------------------
//Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
using namespace std;
//Vector class defintion
class Vector
{
//private data members
private :
double x;
double y;
double z;
public:
//contructor
Vector()
{
x=y=z=0;
}
//parameterized contructor
Vector(double x,double y,double z)
{
setX(x);
setY(y);
setZ(z);
}
//Set x to member variable
void setX(double x)
{
this->x=x;
}
//Set y to member variable
void setY(double y)
{
this->y=y;
}
//Set z to member variable
void setZ(double z)
{
this->z=z;
}
//Method add that takes vector reference v and add to this class vector
void add(Vector &v)
{
this->x=this->x+v.x;
this->y=this->y+v.y;
this->z=this->z+v.z;
}
//print vector in the form of (x,y,z)
void display()
{
cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
}
};
#endif VECTOR_H
------------------------------------------------------------------------
Sample Output:
v(1,2,3)
w(1,2,3)
Result :(2,4,6)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.