| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3326 人关注过本帖
标题:C++基础问题求助
只看楼主 加入收藏
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
回复 10楼 rjsp
对,这就是我想问的,请问输出的为什么是2.07357e-317
2017-05-05 11:16
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
另外问一下,
#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line(double len);
 
   private:
      double length;
    double width;
};
 

Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    width = len;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}

int main( )
{
   Line line(10.0);
 

   cout << "Length of line : " << line.getLength() <<endl;

   line.setLength(6.0);
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

 Line line(10.0);是LINE构造函数的实参,也是setLength()的实参吗? 这个是怎么判断的
2017-05-05 11:22
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line(double len);
 
   private:
      double length;
   
};
 

Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    length = len;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}

int main( )
{
   Line line(10.0);
 

   cout << "Length of line : " << line.getLength() <<endl;

   line.setLength(6.0);
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

 Line line(10.0);
cout << "Length of line : " << line.getLength() <<endl;
输出值为10,这里getLength()是直接从LINE构造函数中取得length值吗?
 line.setLength(6.0);
   cout << "Length of line : " << line.getLength() <<endl;
 这里getLength()是从setLength函数中取length值吗?
2017-05-05 11:32
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
这就是我想问的,请问输出的为什么是2.07357e-317
问题不成立,C/C++没有规定一个未初始化的double值一定是多少
你此时输出2.07357e-317,不等于每次都一定能输出2.07357e-317,更不等于在别人的机器上每次都一定能输出2.07357e-317

Line line(10.0);是LINE构造函数的实参,也是setLength()的实参吗? 这个是怎么判断的
Line line(10.0);是LINE构造函数的实参,也是setLength()的实参
Line line(10.0)中10.0是LINE构造函数的实参,line.setLength(6.0)中6.0是setLength()的实参
“这个是怎么判断的”不明白你想说什么

这里getLength()是直接从LINE构造函数中取得length值吗?
和“LINE构造函数”没有任何关系,你直接看代码嘛
double Line::getLength( void )
{
    return length;
}
可见,getLength 就是返回 line.length 的值

int main( void )
{
    Line line(10.0);
    // 此时 line.length 等于 10
    cout << "Length of line : " << line.getLength() <<endl;
    // 所以上一句输出 line.length 的值 10

    line.setLength(6.0);
    // 此时 line.length 等于 6
    cout << "Length of line : " << line.getLength() <<endl;
    // 所以上一句输出 line.length 的值 6

    return 0;
}
2017-05-05 11:59
CplusGo
Rank: 2
等 级:论坛游民
威 望:1
帖 子:5
专家分:55
注 册:2017-5-4
收藏
得分:10 
那个 2.07323e-317是个垃圾数据,你的构造函数赋值给的widelen
length变量为初始化,所以它的值是不确定是啥
构造函数建议完成成员变量的初始化操作
2017-05-05 15:32
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
...

[此贴子已经被作者于2017-5-5 21:58编辑过]

2017-05-05 21:52
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
.,,,

[此贴子已经被作者于2017-5-5 21:57编辑过]

2017-05-05 21:55
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
回复 14楼 rjsp
谢谢回复。
Line::Line( double len)
{
    cout << "Object is being created, length = " << len << endl;
    length = len;
}
 
void Line::setLength( double len )
{
    length = len;
}
这个是怎么判断是哪个的实参的 我是想问
LINE line(10.0)中10.0是Line构造函数的实参,那也可以是setLength()的实参吗,参数类型都是double len,这个是如何区分是哪个的实参?
2017-05-05 21:56
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
回复 15楼 CplusGo
感谢回复。我还以为是 没初始化也都有默认值,结果试了几次后结果好像有点不太一样,忘记还有垃圾数据了。
2017-05-05 21:57
快速回复:C++基础问题求助
数据加载中...
 
   



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

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