#include <iostream>
using namespace std;
class complex{
private:
double real; //实部
double imag; //虚部
public:
complex(): real(0.0), imag(0.0){ }
complex(double a, double b): real(a), imag(b){ }
complex operator+(const complex & A)const;
void display()const;
};
//运算符重载
complex complex::operator+(const complex & A)const{
complex B;
B.real = real + A.real;
B.imag = imag + A.imag;
return B;
}
void complex::display()const{
cout<<real<<" + "<<imag<<"i"<<endl;
}
int main(){
complex c1(4.3, 5.8);
complex c2(2.4, 3.7);
complex c3;
c3 = c1 + c2;
c3.display();
return 0;
}用c++实现加法器
最新推荐文章于 2022-06-10 21:43:05 发布
本文介绍了一个简单的C++程序,该程序定义了一个复数类,并实现了加法运算符的重载。通过这个例子,读者可以了解如何使用C++进行类的设计及运算符重载的基本方法。
1073

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



