Write a program to overload unary operator for complex class. Overload the pre and post decrement operator to work for the statements
C1++;
++C1;
Overload any operator of your choice to find the modulus of a complex number. [ Note: void return type]
Also rewrite this program to overload the operators as friend function.
#include<iostream>
using namespace std;
class Complex{
private:
public:
int real, imag;
Complex(){
}
Complex(int r, int i){
real = r;
imag = i;
}
void operator ++(){
real ++;
imag++;
}
void operator ++(int){
++real;
++imag;
}
void display(){
cout << real << " + i" << imag << endl;
}
void operator % (Complex b){
imag= imag % b.imag;
real = real % b.real;
}
friend Complex operator ++ (Complex t);
};
int main(){
Complex C2(8,9), C3(10,20);
C2.display();
++C2;
C2.display();
C2++;
C2.display();
C2 % C3;
C2.display();
}
Comments
Thank you so much AssignmentExpert team
Leave a comment