C++ program to overload unary - minus operator for complex numbers using member function.

#include<iostream.h>
#include<conio.h>
class complex{
float a,b;
public:
complex(){
a=b=0;
}
complex(float real,float image){
a=real;
b=image;
}
void display(){
cout<<a<<"+("<<b<<")i"<<endl;
}
void operator-();
};
void complex::operator-(){
a=-a;
b=-b;
}
void main(){
complex c1(2,-5);
clrscr();
c1.display();
// Unary Operator overloading
-c1;
c1.display();
getch();
}

No comments:

Post a Comment