C++ program to overload unary minus 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 void operator-(vector &v);
};
void operator-(vector &v){
v.x=-v.x;
v.y=-v.y;
v.z=-v.z;
}
void main(){
vector v1(3,7,5);
clrscr();
v1.display();
// Unary Operator overloading
-v1;
v1.display();
getch();
}

No comments:

Post a Comment