Answer to Question #262748 in C++ for Hiyu

Question #262748

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 and friend function.


1
Expert's answer
2021-11-09T17:48:25-0500
#include<iostream>
using namespace std;
class Complex{
     private:
        int real, imag;
     public:
         Complex(){
         
         }
         Complex(int r, int i){
             real = r;
             imag= i;
         }
         friend Complex operator *(Complex b);
         friend Complex operator +(Complex b);
         
         void display(){
         cout<<real<<" + "<<imag<<"i"<<endl;
         }
};
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 = c.real + b.real;
    c.imag = c.imag + b.imag;
    return c;
}


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