关于编程当中的复数问题
来源:百度知道 编辑:UC知道 时间:2024/06/03 01:31:45
#include "iostream"
using namespace std;
class fushu
{
public:
fushu()
{
shi=0;
xu=0;
}
fushu(float a,float b)
{
shi=a;
xu=b;
}
fushu operator +(fushu c);
void display();
private:
float shi,xu;
};
fushu fushu::operator +(fushu c)
{
return fushu(shi+c.shi,xu+c.xu);
}
void fushu::display()
{
cout<<shi<<" "<<xu<<"i"<<endl;
}
void main()
{
fushu fs1(6.1,12.3),fs2(1,1.2),fs3;
fs3=fs1+fs2;
cout<<"fs1="<<fs1.display;
cout<<"fs2="<<fs2.display;
cout<<"fs3="<<fs3.display;
}
编译完的结果确是fs1=1fs2=1fs3=1
求原因及解决办法,谢谢各位:)
呵呵,我以按照一楼的想法改过了,结果是fs1=fs2=fs3=
还有为什么display没有返回值就不能cout啊?
万分感谢.
using namespace std;
class fushu
{
public:
fushu()
{
shi=0;
xu=0;
}
fushu(float a,float b)
{
shi=a;
xu=b;
}
fushu operator +(fushu c);
void display();
private:
float shi,xu;
};
fushu fushu::operator +(fushu c)
{
return fushu(shi+c.shi,xu+c.xu);
}
void fushu::display()
{
cout<<shi<<" "<<xu<<"i"<<endl;
}
void main()
{
fushu fs1(6.1,12.3),fs2(1,1.2),fs3;
fs3=fs1+fs2;
cout<<"fs1="<<fs1.display;
cout<<"fs2="<<fs2.display;
cout<<"fs3="<<fs3.display;
}
编译完的结果确是fs1=1fs2=1fs3=1
求原因及解决办法,谢谢各位:)
呵呵,我以按照一楼的想法改过了,结果是fs1=fs2=fs3=
还有为什么display没有返回值就不能cout啊?
万分感谢.
您的写法很有问题,在main函数中应该像是调用一个函数一样调用display函数啊,更何况display函数没有返回值,不能输出的,改成这样
cout<<"fs1=";
fs1.display();
cout<<"fs2=";
fs2.display();
cout<<"fs3=";
fs3.display();