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.
#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;
}
Comments
Leave a comment