Tuesday, May 22, 2018

ADDITION OF TWO VECTORS USING THE CONCEPT OF OPERATOR OVERLOADING 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();
       Vector operator + (Vector);
};
void Vector :: getdata()
{
 cout<<"Enter the value of x,y,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 Vector :: operator + (Vector ob)
{
 Vector temp;
 temp.x=x+ob.x;
 temp.y=y+ob.y;
 temp.z=z+ob.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