| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 424 人关注过本帖
标题:课后习题求大神指点。
只看楼主 加入收藏
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
结帖率:96.23%
收藏
 问题点数:0 回复次数:2 
课后习题求大神指点。
定义一个负数类 complex,并重载“+”,实现复数的相加,但是不能把“+”重载为友元函数和成员函数,下面是我写的两个代码,第一个可以运行但是第二个不能运行求指点!!
#include<iostream>
using namespace std;
class complex
{
public:
    complex(){real=0;image=0;}
    complex(int r,int i):real(r),image(i){}
    int disreal();
    int disimage();
    void display();
private:
    int real;
    int image;
};
int complex::disreal()
{
    return(real);
}
int complex::disimage()
{
    return(image);
}
void complex::display()
{
    cout<<real<<"+"<<image<<endl;
}
complex operator +(complex &c1,complex &c2)
{
    return complex(c1.disreal()+c2.disreal(),c1.disimage()+c2.disimage());
}

int main()
{
    complex c1(4,3),c2(3,5),c3;
    c3=c1+c2;
    c3.display();
    return(0);
}
这是第一个可以运行。
#include<iostream>
using namespace std;
class complex
{
public:
    complex(){real=0;image=0;}
    complex(int r,int i):real(r),image(i){}
    friend ostream& operator <<(ostream&,complex&);
    int disreal();
    int disimage();
private:
    int real;
    int image;
};
int complex::disreal()
{
    return(real);
}
int complex::disimage()
{
    return(image);
}
ostream& operator <<(ostream &output,complex &c3)
{
   output<<c3.real<<c3.image<<endl;
 return output;
}
complex operator +(complex &c1,complex &c2)
{
    return complex(c1.disreal()+c2.disreal(),c1.disimage()+c2.disimage());
}

int main()
{
    complex c1(4,3),c2(3,5),c3;
    c3=c1+c2;
    cout<<c3;
    return(0);
}
第二个我重载了<<使它能输出complex类,但是不知道为啥不能运行。
搜索更多相关主题的帖子: void private complex display 
2013-02-01 19:34
mfkblue
Rank: 5Rank: 5
等 级:职业侠客
帖 子:472
专家分:343
注 册:2008-12-21
收藏
得分:0 
运行正常 ,输出 78
2013-02-02 11:39
liqingqinger
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:69
专家分:163
注 册:2013-1-2
收藏
得分:0 
百度文库

求布喷。。。
我是菜鸟。。。。。
2013-02-16 11:29
快速回复:课后习题求大神指点。
数据加载中...
 
   



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

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