| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 662 人关注过本帖
标题:研究半天,没弄出来,求大神指点,谢谢!
取消只看楼主 加入收藏
jiekevv520
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-3-17
结帖率:66.67%
收藏
已结贴  问题点数:1 回复次数:1 
研究半天,没弄出来,求大神指点,谢谢!
//一个复数类Complex,重载运算符“+”使之能够完成附属的加法运算。
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{public:
//Complex() {real=0;imag=0;}
Complex(double r=0,double i=0):real(r),imag(i) {}
Complex operator+ (Complex &c2);
friend Complex operator+ (Complex &c,int &i);
friend Complex operator+ (int &i,Complex &c2);
void display();
private:
    double real;
    double imag;
};
Complex Complex::operator+ (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);
}
Complex operator+(Complex &c,int &i)
{return Complex(i+c.real,c.imag);
}
Complex operator+ (int &i,Complex &c2)
{return (i+c2.real,c2.imag);
};
void Complex::display()
{cout<<'('<<real<<','<<imag<<'i)'<<endl;
}
int main ()
{Complex r1(1,2),r2(3,4),r3;
r3 = r1 + r2;
r3 = r2 + r1;
r3 = r1 + 4;  
r3 = 4 + r1;
cout<<"r1=";r1.display();
cout<<"r2=";r2.display();
cout<<"r1+r2=";r3.display();
return 0;
}
搜索更多相关主题的帖子: Complex private display include public 
2015-05-17 21:47
jiekevv520
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-3-17
收藏
得分:0 
谢谢各位!

历史如云,我只是抬头望过,历史如雷,我只是掩耳听过!
2015-05-22 12:38
快速回复:研究半天,没弄出来,求大神指点,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013438 second(s), 8 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved