注册 登录
编程论坛 C++教室

类型转换问题

最近不在 发布于 2010-03-15 19:43, 688 次点击
程序代码:
#include<iostream>
using namespace std;
class D
{
    public:
    D()
    {d=0;}
    D(double i)
    {d=i;}
    void Print()
    {
        cout<<d<<endl;
    }
    private:
    double d;
};
void main()
{
    D d;
    d=20;
    d.Print();
}

为什么输出是20,而不是浮点数
8 回复
#2
floppyfuck2010-03-15 19:48
摆脱 你看看你的程序不就是直接输出20吗  怎么来要数出浮点数呀?你把问题看走了眼了吧 你提的问题也不对头呀!你根本就没转换!
#3
最近不在2010-03-15 20:12
不是double i吗?20就转换为20.0,然后赋值给d
#4
秀痘魔导士2010-03-16 09:23
cout<<setprecision(2)<<d;

代码问题挺多的,没有重载=操作符就在使用
#5
玩出来的代码2010-03-16 13:39
d=20,实际上就是调用了构造函数。
#6
mghxz2522010-03-16 16:25
看不懂,对类的问题有点迷糊。
#7
pangding2010-03-16 16:42
你直接
double d = 20;
cout << d << endl;
输出的是什么呀?
#8
gtl750088102010-03-16 17:47
实际上d=20是调用了构造函数了。应该是精度设置的问题。
#9
apull2010-03-20 10:04
回复 楼主 最近不在
cout里用setprecision(n) 可以设置小数精度,n是要显示的精度。
cout << setprecision(2) << 20.0;
1