一、问题描述
1、定义一个复数类Complex,使得下面的代码能够工作。
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
#include <iostream>
using namespace std;
class Complex {
public:Complex(float i, float j);Complex (float x); //函数重载void add(Complex t);void show();
private:float shi;float xu;
};
Complex::Complex(float x)
{
shi = x;xu = 0;
}
Complex::Complex(float i, float j ) //定义复数的实部和虚部为浮点型
{
shi = i;xu = j;
}
void Complex:: add(Complex t) //定义复数相加函数
{
shi = shi + t.shi;xu = xu + t.xu;
}
void Complex::show()
{
cout << shi << "+" << xu << "*i" << endl;
}int main() {
Complex c1(3, 5);Complex c2(4.5);c1.add(c2);c1.show();return 0;}
运行图