作者satokuzao (no元)
看板C_and_CPP
标题[问题] >>过载与class里的private之问题!
时间Tue Apr 21 21:07:42 2009
设立一个数学的虚实数物件(object),例如:0+0i(预设)
目的:输入实部和虚部的数字并输出,例如:输入1 +2i则输出1+2i
问题:目前输入1 2後依旧跑出预设(已解决)
红色字为修正部分
#include<iostream>
#include <cstdlib>
using namespace std;
class Complex
{
private:
double real,imag;
public:
Complex::Complex():real(0),imag(0){};
void input(int a,int b);
double getRe() ;
double getIm() ;
friend istream& operator >>(istream& inStr,Complex
& recive);
friend ostream& operator <<(ostream& ouStr,Complex
& recive);
void input(double a,double b);
};
void Complex::input(double a,double b)
{
real=a;
imag=b;
}
istream& operator >>(istream& inStr,Complex
& recive)
{
cout<<"请输入实数及虚数部分\n";
double a,b;
inStr>>a>>b;
recive.input(a,b);
return inStr;
}
int main()
{
Complex test;
cin>>test;
cout<<"现在输出TEST输入结果:"<<test<<endl;
system("pause");
}
ostream& operator <<(ostream& ouStr,Complex
& recive)
{
cout<<"输出结果为";
ouStr<<recive.getRe();
cout<<"+";
ouStr<<recive.getIm();
cout<<endl;
}
double Complex::getIm()
{ return imag; }
double Complex::getRe()
{ return real; }
※ 编辑: satokuzao 来自: 140.117.181.63 (04/21 22:13)
1F:→ james732:Complex recive 改成 Complex &recive 的话? 04/21 22:13
2F:→ satokuzao:糟糕!秒杀~没错加入&参照就OK了 04/21 22:15
※ 编辑: satokuzao 来自: 140.117.181.63 (04/21 22:20)
3F:→ satokuzao:想起来了<<与>>的运算子过载是属於参照类型!哀小细节.. 04/21 22:21