c++实现加法器

转自:http://blog.csdn.net/qq_36691454/article/details/54695712?locationNum=2&fps=1

[cpp]  view plain  copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. class complex{  
  5. private:  
  6.     double real;  //实部  
  7.     double imag;  //虚部  
  8. public:  
  9.     complex(): real(0.0), imag(0.0){ }  
  10.     complex(double a, double b): real(a), imag(b){ }  
  11.     complex operator+(const complex & A)const;  
  12.     void display()const;  
  13. };  
  14.   
  15. //运算符重载  
  16. complex complex::operator+(const complex & A)const{  
  17.     complex B;  
  18.     B.real = real + A.real;  
  19.     B.imag = imag + A.imag;  
  20.     return B;  
  21. }  
  22.   
  23. void complex::display()const{  
  24.     cout<<real<<" + "<<imag<<"i"<<endl;  
  25. }  
  26.   
  27. int main(){  
  28.     complex c1(4.3, 5.8);  
  29.     complex c2(2.4, 3.7);  
  30.     complex c3;  
  31.     c3 = c1 + c2;  
  32.     c3.display();  
  33.     
  34.     return 0;  
  35. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值