定义复数类的加法操作,使之能够执行下列运算:

[复制链接]
查看11 | 回复0 | 2007-1-12 13:08:14 | 显示全部楼层 |阅读模式
class complex{ public: complex(int r=0,int i=0);//构造 complex operator+(const complex& other);
//重载加法运算符 complex Complex::operator+(const int &a);void complex::input()protected: int real,image; }; void complex::input(){int real,image;cout >r>>m;real = r;image = m;}complex::complex(int r,int i) { real=r; image=i; return; } complex complex::operator+(const complex& other) { complex temp; temp.real=real+other.real; temp.image=image+other.image; return temp; } complex complex::operator+(const int &a){complex temp;temp.real= real +a;temp.image = image;return temp;}int main(int argc,char* argv[]){complex a,b,c;a.input();b.input();c = a +b;//输出c = 5 +a;//输出c = b +5;//输出}} 可能有错,自己调试一下,加上输出;参考资料:http://bbs.tarena.com.cn/topic.cgi?forum=2&topic=1744本回答被提问者采纳
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行