Answer to Question #257982 in C++ for Amit

Question #257982

Write a program to design a class representing complex numbers and having functionality of performing addition and multiplication of two complex numbers using operator overloading.


1
Expert's answer
2021-10-29T15:20:42-0400
#include<iostream>
using namespace std;
class Complex{
	private:
		int real, imag;
	public:
		Complex(){
			
		}
		Complex(int r, int i){
			real = r;
			imag= i;
		}
		
		Complex operator *(Complex b){
			Complex c;
			c.real = this->real * b.real;
			c.imag = this->imag * b.imag;
			return c;
		}
		
		Complex operator +(Complex b){
			Complex c;
			c.real = this->real + b.real;
			c.imag = this->imag + b.imag;
			return c;
		}
		
		void display(){
			cout<<real<<" + "<<imag<<"i"<<endl;
		}
};


int main(){
	Complex c1(12, 10), c2(4,5), c3, c4;
	c3 = c1 + c2;
	c4 = c1 * c2;
	cout<<"The addition of two complex numbers is: "; c3.display();
	cout<<"The multiplication of two complex numbers is:  ";c4.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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS