Answer to Question #255649 in C++ for Neil

Question #255649

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.


1
Expert's answer
2021-10-25T03:02:12-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Neil
26.10.21, 17:27

Thank you so much AssignmentExpert team

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS