您的位置:首页 > 编程语言 > C语言/C++

用c++实现加法器

2017-01-23 20:44 375 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: