C++ program to overload binary plus operator for vectors using friend function.

#include<iostream.h>
#include<conio.h>
class vector{
float x,y,z;
public:
vector(){
x=y=z=0;
}
vector(float x1,float y1,float z1){
x=x1;
y=y1;
z=z1;
}
void display(){
cout<<"( "<<x<<")i + ("<<y<<")j + ("<<z<<")k"<<endl;
}
friend vector operator+(vector &v1,vector &v2);
};
vector operator+(vector &v1,vector &v2){
vector temp;
temp.x=v1.x+v2.x;
temp.y=v1.y+v2.y;
temp.z=v1.z+v2.z;
return temp;
}
void main(){
vector v1(3,7,5),v2(3,2,1),v3;
clrscr();
v1.display();
v2.display();
v3=v1+v2;
v3.display();
getch();
}

No comments:

Post a Comment