C++ program to overload binary plus operator for vectors using member 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;
}
vector operator+(vector &v);
};
vector vector::operator+(vector &v){
vector temp;
temp.x=x+v.x;
temp.y=y+v.y;
temp.z=z+v.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