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

一个复数类
#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
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
得分:0 
恩,我改了,可在dev c++上就通不过
2006-03-16 23:01
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
快速回复:请教大家,着急,谢谢了
数据加载中...
 
   



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

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