Assignment N5
Assignment N5
#include <iostream.h>
#include <conio.h>
class Complex {
private:
float real;
float imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
Complex operator*(const Complex& c)
{
Complex temp;
temp.real = (real * c.real) - (imag * c.imag);
temp.imag = (real * c.imag) + (imag * c.real);
return temp;
}
void display() {
if (imag < 0)
cout << real << " - " << -imag << "i" << endl;
else
cout << real << " + " << imag << "i" << endl;
}
};
void main()
{
clrscr();
Complex c1(3, 2); // 3 + 2i
Complex c2(1, 7); // 1 + 7i
Complex c3 = c1 * c2;
cout << "First complex number: ";
c1.display();
cout << "Second complex number: ";
c2.display();
cout << "Result of multiplication: ";
c3.display();
getch();
}
/*
First complex number: 3 + 2i
Second complex number: 1 + 7i
Result of multiplication: -11 + 23i
*/