# How to Create your own Vector Class with Example Code?

How to Create your own Vector Class  with public or private keyword and using friend function with Example Code?
1. Implement the following class?

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();
};
Solution:

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;
}

• Title : How to Create your own Vector Class with Example Code?
• Posted by :
• Date : 13:20
• Labels :