#include<iostream>
using namespace std;
class complex
{
private:
double real;
double imag;
public:
complex(double r=0,double i=0):real(r),imag(i){}
complex operator + (complex &c)
{return complex(c.real+real,c.imag+imag);}
complex operator - (complex &c)
{return complex(real-c.real,imag-c.imag);}
void display()
{
cout<<"该复数为"<<imag<<"i+"<<real<<endl;
}
};
int main()
{
complex a(1,2);
complex b(2,3),c(0,0);
a.display();
c=a-b;
c.display();
return 0;
}
本文展示了一个简单的C++程序,用于实现复数的基本运算,包括加法和减法,并通过一个实例演示了如何创建复数对象并进行运算。
1569

被折叠的 条评论
为什么被折叠?



