• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HBUwyf
博客园    首页    新随笔    联系   管理    订阅  订阅
C++ 用运算符重载 实现复数相加
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{public:
    Complex(){real = 0;imag = 0;}
    Complex (double r, double i){real = r ;imag = i;}
    Complex operator+(Complex &c2);
    void display();
private:
    double real;
    double imag;
};

Complex Complex :: operator+(Complex &c2)
{
    Complex c;
    c.real = real + c2.real;
    c.imag = imag + c2.imag;
    return c;
}

void Complex::display()
{
    cout<<"}"<<real<<","<<imag<<"i)"<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
    Complex c1(3,4),c2(5,-10),c3;
    c3 = c1 + c2;
    cout<<"c1="; c1.display();
    cout<<"c2="; c2.display();
    cout<<"c1+c2="; c3.display();
    return 0;
}

 

posted on 2018-04-18 20:13  hbuwyf  阅读(571)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3