Online Practice Problem Vector Class Implementation using C++ Programming Example Code For Assignment lab Homework task
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Vector
{
private:
int x,y,z;
public:
Vector (int,int,int);
Vector ();
friend Vector operator+ (const Vector &n1, const Vector &n2);
friend Vector operator-(const Vector &n1, const Vector &n2);
friend Vector operator-(const Vector &n1);
void show();
};
;
Vector::Vector(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
Vector::Vector()
{
x=0;
y=0;
z=0;
}
Vector operator +(const Vector &n1, const Vector &n2) //Addition
{
Vector res;
res.x = n1.x + n2.x;
res.y = n1.y + n2.y;
res.z = n1.z + n2.z;
return res;
}
Vector operator -(const Vector &n1, const Vector &n2) //Subtraction
{
Vector res;
res.x = n1.x - n2.x;
res.y = n1.y - n2.y;
res.z = n1.z - n2.z;
return res;
}
Vector operator -(const Vector &n1) //Subtraction
{
Vector res;
res.x = -n1.x ;
res.y = -n1.y;
res.z = -n1.z;
return res;
}
void Vector::show() //Subtraction
{
cout<< x<<"|"<<y<<"|"<<z;
}
///////////////////////////////////////////////////////////////////////////////////
int main()
{
Vector v1(1,2,3);
Vector v2(1,2,3);
Vector v3;
v3=v1+v2;
v3=-v1;
v3.show();
system("pause");
}