Develop a C++ program to negate the complex number by using unary minus operator overloading
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Complex{
public:
int real, img;
Complex(int real, int img)
{
this->real = real;
this->img = img;
}
void operator-()
{
this->real = -real;
this->img = -img;
}
};
int main()
{
int real, img;
cout<<"Enter Real part of complex number : ";
cin>>real;
cout<<endl<<"Enter Imaginary part of complex number : ";
cin>>img;
Complex c1(real, img);
cout<<endl<<"Before Negation of Complex number : ";
cout<<"("<<c1.real<<") + ("<<c1.img<<")i";
c1.operator-();
cout<<endl<<"After Negation of Complex number : ";
cout<<"("<<c1.real<<") + ("<<c1.img<<")i";
}
Comments
Leave a comment