Answer to Question #237970 in C++ for SOM

Question #237970

WAP in C++ to find multiplication of two complex numbers using constructor overloading. The first constructor which takes no argument is used to create objects which are not initialized, second which takes one argument is used to initialize real and imag parts to ‘equal values and third which takes two argument is used to initialized real and imag to two different values.


1
Expert's answer
2021-09-16T13:04:26-0400
#include<bits/stdc++.h>
using namespace std;
class Complex {
    public:
        int r, imag;
    Complex(int tempR = 0, int tempImag = 0)
    {
        r = tempR;
        imag = tempImag;
    }
    Complex sumComplex(Complex x1, Complex x2)
    {
    
        Complex temp;
        temp.r = x1.r + x2.r;
  
        temp.imag = x1.imag + x2.imag;
  
        return temp;
    }
};
int main()
{
 
    Complex x1(7, 4);
    cout<<"Complex number 1 : "<< x1.r
                << " + i"<< x1.imag<<endl;
 
    
    Complex x2(12, 15);
 
    cout<<"Complex number 2 : "<< x2.r
                << " + i"<< x2.imag<<endl;
 
    Complex x3;
 
    x3 = x3.sumComplex(x1, x2);
 
    cout<<"Sum of complex number : "
                       << x3.r << " + i"
                       << x3.imag;
}

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