| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3325 人关注过本帖
标题:C++基础问题求助
只看楼主 加入收藏
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
结帖率:0
收藏
已结贴  问题点数:20 回复次数:18 
C++基础问题求助
#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;
}
当上面的代码被编译和执行时,它会产生下列结果:
Object is being created, length = 10
Length of line : 10
Length of line : 6

请问以上代码中 Line line(10.0)
10.0 是仅仅赋值给 构造函数吗?如果是的话 当把构造函数中的lenth=len 中的lenth随机修改成另一个变量名后,显示结果如下
Object is being created, length = 100000
Length of line : 2.07323e-317
Length of line : 6
请问这里的 第2行的2.07323e-317是如何得到的?
搜索更多相关主题的帖子: private created include Object public 
2017-05-05 07:45
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;
}
当上面的代码被编译和执行时,它会产生下列结果:
Object is being created, length = 10
Length of line : 10
Length of line : 6

请问以上代码中 Line line(10.0)
10.0 是仅仅赋值给 构造函数吗?如果是的话 当把构造函数中的lenth=len 中的lenth随机修改成另一个变量名后,显示结果如下
Object is being created, length = 100000
Length of line : 2.07323e-317
Length of line : 6
请问这里的 第2行的2.07323e-317是如何得到的?

更正以上第二次显示结果是我将 构造函数中的length 修改后并将10.0改成100000.0后形成的结果,但是无论改成什么数字,第二行显示均为
2.07323e-317
请问这里的 第2行的2.07323e-317是如何得到的?
2017-05-05 07:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
10.0 是仅仅赋值给构造函数吗?
------ 10.0是构造函数的实参,“赋值给构造函数”我是听不懂什么意思。

如果是的话 当把构造函数中的lenth=len 中的lenth随机修改成另一个变量名后
------ 不知所云,你还是贴代码吧
2017-05-05 08:22
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
回复 3楼 rjsp
谢谢回复,小白不懂有些该怎么表达。
我是指 line()构造函数中length=len 换成比如 width=len,这样之后得到结果和原先不一样
一开始是
Object is being created, length = 10
Length of line : 10
Length of line : 6
改变之后变成
Object is being created, length = 100000
Length of line : 2.07323e-317
Length of line : 6
我想说这个Length of line : 2.07323e-317
这个数值是怎么得到的
2017-05-05 10:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 4楼 cainiao12332
要么贴代码,要
2017-05-05 10:18
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;
}


第二个代码
#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;
    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;
}
2017-05-05 10:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分: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;
    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;
}

VC9.0编译报错:error C2065: 'width' : undeclared identifier
g++6.3编译报错:error: 'width' was not declared in this scope
2017-05-05 10:40
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 

#include <iostream>
 #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;
}
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;
}
2017-05-05 10:47
cainiao12332
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2017-5-5
收藏
得分:0 
上面那个多复制了个#include<iostream>.
2017-05-05 10:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 8楼 cainiao12332
Line line(10.0);
cout << "Length of line : " << line.getLength() <<endl; // 此时 Line.length 还没有赋值

你的问题可简化为
    #include <iostream>
    int main( void )
    {
        double length;
        std::cout << length << std::endl;
    }
    为什么输出是某某值。
2017-05-05 11:08
快速回复:C++基础问题求助
数据加载中...
 
   



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

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