| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1073 人关注过本帖
标题:请教大家,着急,谢谢了
只看楼主 加入收藏
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
 问题点数:0 回复次数:13 
请教大家,着急,谢谢了

一个复数类
#include<iostream>
using namespace std;

class Complex
{
private:
double real; //实数部分
double imag; //虚数部分
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator +(const Complex &c2 );
Complex operator -(const Complex &c2 );
Complex operator *(const Complex &c2 );
Complex operator /(const Complex &c2 );
void display();
};

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

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

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

Complex Complex::operator/(const 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 main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=:";
c3.display();
c3=c1-c2;
cout<<"c3=:";
c3.display();
c3=c1*c2;
cout<<"c3=:";
c3.display();
return 0;
}

可以编译过去


如果这里改成
Complex& operator +(const Complex &c2 );
Complex& operator -(const Complex &c2 );
Complex& operator *(const Complex &c2 );
Complex& operator /(const Complex &c2 );
就不可以编译了,为什么呀?

什么时候用引用,什么时候不用,谢谢了,急!!!!!!!!

搜索更多相关主题的帖子: double Complex private display 
2006-03-16 22:28
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 

return *this;这种时候一般用引用

但是我觉得你这样改了好象也能通过编译的
改了记住在实现那个类的时候也要改哦


天行健,君子以自强不息!!QQ:68660681
2006-03-16 22:40
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
得分:0 
恩,我改了,可在dev c++上就通不过
2006-03-16 23:01
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
那就不要改成那样嘛,我在vc上能通过编译

天行健,君子以自强不息!!QQ:68660681
2006-03-16 23:09
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
得分:0 

看看这个程序
#include<iostream>
using namespace std;

class Complex{
double real; //实数部分
double imag; //虚数部分
public:
Complex(double real=0,double imag=0){}
Complex operator+(Complex &s);
Complex operator+(int &s);
friend Complex operator+(int &s,Complex &s);
void disp();
};

Complex Complex::operator+(Complex &s)
/*{
Complex a;
a.real=real+s.real;
a.imag=imag+s.imag;
return a;
}*/
{
return Complex(real+s.real,imag+s.imag);
}
Complex Complex::operator+(int &s)
{
return Complex(real+s,imag);
}

Complex Complex::operator+(int &s , Complex &s1 )
{
return Complex(s+s1.real,s1.imag);
}

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

int main()
{
Complex c1(3,3),c2(-1,0),c3,c4;
c3=c1+c2;
cout<<"c3=";
c3.disp();
int i=7;
c4=c1+i;
c3=i+c2;
c4.disp();
cout<<endl;
c3.disp();
return 0;
}



2006-03-16 23:13
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
得分:0 
也通不过
2006-03-16 23:13
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
得分:0 

能给我举个一定用引用的例子吗?谢谢你了,我这点特别搞不懂!!!!!!!!!

2006-03-16 23:15
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
上面的程序都是错的!!!

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-17 09:39
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
复数的运算法则......

2006-03-17 10:10
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
以下是引用wumingsx在2006-3-16 23:15:00的发言:

能给我举个一定用引用的例子吗?谢谢你了,我这点特别搞不懂!!!!!!!!!

你看看这个程序,看看运行结果!

#include<iostream>
using namespace std;

class Complex{
double real; //实数部分
double imag; //虚数部分
public:
Complex(double real=0,double imag=0){}
Complex& operator+(Complex &s);
void disp();
};

Complexq& Complex::operator+(Complex &s)

{
real=real+s.real;
imag=imag+s.mag;
return *this;
}

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

int main()
{
complex c1(1,2),c2(3,4);
c1.disp;

complex c3;
c3=c1+c2;
c3.disp;
c1.disp;
return 0;
}




天行健,君子以自强不息!!QQ:68660681
2006-03-17 12:21
快速回复:请教大家,着急,谢谢了
数据加载中...
 
   



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

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