#include <iostream>
using namespace std;
class Complex {
public:
double real;
double imag;
Complex(double r = 0, double i = 0);
void print();
Complex operator+(Complex c);
Complex operator*(Complex c);
};
Complex::Complex(double r, double i)
{
real = r; imag = i;
}
Complex Complex::operator+(Complex c)
{
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
Complex Complex::operator*(Complex c)
{
Complex temp;
temp.real = real * c.real - imag * c.imag;
temp.imag = real * c.imag + imag * c.real;
return temp;
}
void Complex::print()
{
cout << "total real=" << real << " " << "total imag=" << imag << endl;
}
int main()
{
Complex com1(2.3, 4.6), com2(3.6, 2.8), total1, total2;
total1 = com1 + com2;
total1.print();
total2 = com1 * com2;
C++实验7
最新推荐文章于 2023-03-13 09:23:15 发布