liugongbi 2021-06-23 16:04 采纳率: 0%
浏览 19

C++模板:为什么答案是0 0 1 9 8而不是0.0 0.0 1 9 8呢?

阅读下面程序,写出输出结果。

#include <iostream>

using namespace std;

 

template <class Type>

class Test

{

public:

       Test(Type a = 0, Type b = 0, Type c = 0):z(c) {     x = a; y = b; }

       void Print()

       {

              cout << x << endl;

              cout << y << endl;

       }

       void Print() const

       {

              cout << z << endl;

       }

 

private:

       Type x, y;

       const Type z;

};

 

int main()

{

       Test<float> t1;

       t1.Print();

 

       Test<int> t2(1, 9, 6);

       t2.Print();

 

       const Test<double> t3(0, 6, 1.8);

       t3.Print();

 

       return 0;

}

上面程序的输出结果为:

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-06-23 16:07
    关注

    0即使作为浮点数输出cout也是输出0,除非你强行要求带1位小数才会0.0

    评论

报告相同问题?

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器