Tuesday, May 22, 2018

ADDITION OF TWO VECTORS USING FRIEND FUNCTION IN C++

#include<iostream.h>
#include<conio.h>
class Vector
{
 public:
       int x;
       int y;
       int z;
       Vector()
       {
         x=0;
         y=0;
         z=0;
       }
       void getdata();
       void show();
       friend Vector operator + (Vector &v1,Vector &v2);
};
void Vector :: getdata()
{
 cout<<"Enter the value of x,y and z in the form of xi + yj +zk :"<<endl;
 cin>>x>>y>>z;
}
void Vector :: show()
{
 cout<<"Sum="<<x<<"i + "<<y<<"j + "<<z<<"k";
}
Vector operator + (Vector &p1,Vector &p2)
{
 Vector temp;
 temp.x=p1.x+p2.x;
 temp.y=p1.y+p2.y;
 temp.z=p1.z+p2.z;
 return(temp);
}
void main()
{
 Vector v1,v2,v3;
 clrscr();
 v1.getdata();
 v2.getdata();
 v3=v1+v2;
 v3.show();
 getch();
}

                                                  OUTPUT


No comments:

Post a Comment