Develop a C++ program to negate the complex number by using unary minus operator overloading
Runtime input
2
-3
/*
C++ program to negate the complex number by using unary minus operator overloading
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class ClassComplex{
public:
int num, imaginaryNum;
//Default constructor
ClassComplex(){
}
//Parameterized constructor
ClassComplex(int num, int imaginaryNum){
this->num = num;
this->imaginaryNum = imaginaryNum;
}
//Unary minus operator overloading
void operator-(){
this->num = -num;
this->imaginaryNum = -imaginaryNum;
}
//Default constructor
~ClassComplex(){
}
};
int main(){
int num, imaginaryNum;
cout<<"Enter real number part of the complex number: ";
cin>>num;
cout<<endl<<"Enter part of the complex number: ";
cin>>imaginaryNum;
ClassComplex cc(num, imaginaryNum );
cout<<endl;
cout<<"Before complex number negation: ";
cout<<"["<<cc.num<<"] + ["<<cc.imaginaryNum<<"]i";
cc.operator-();
cout<<endl;
cout<<"After complex number negation: ";
cout<<"["<<cc.num<<"] + ["<<cc.imaginaryNum<<"]i";
}
Comments
Leave a comment