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

C++基础问题求助

cainiao12332 发布于 2017-05-05 07:45, 3812 次点击
#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是如何得到的?
18 回复
#2
cainiao123322017-05-05 07:48
#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是如何得到的?
#3
rjsp2017-05-05 08:22
10.0 是仅仅赋值给构造函数吗?
------ 10.0是构造函数的实参,“赋值给构造函数”我是听不懂什么意思。

如果是的话 当把构造函数中的lenth=len 中的lenth随机修改成另一个变量名后
------ 不知所云,你还是贴代码吧
#4
cainiao123322017-05-05 10:09
回复 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
这个数值是怎么得到的
#5
rjsp2017-05-05 10:18
回复 4楼 cainiao12332
要么贴代码,要
#6
cainiao123322017-05-05 10:21
第一个代码
#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;
}
#7
rjsp2017-05-05 10:40
程序代码:
#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
#8
cainiao123322017-05-05 10:47

#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;
}
#9
cainiao123322017-05-05 10:49
上面那个多复制了个#include<iostream>.
#10
rjsp2017-05-05 11:08
回复 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;
    }
    为什么输出是某某值。
#11
cainiao123322017-05-05 11:16
回复 10楼 rjsp
对,这就是我想问的,请问输出的为什么是2.07357e-317
#12
cainiao123322017-05-05 11:22
另外问一下,
#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()的实参吗? 这个是怎么判断的
#13
cainiao123322017-05-05 11:32
#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值吗?
#14
rjsp2017-05-05 11:59
这就是我想问的,请问输出的为什么是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;
}
#15
CplusGo2017-05-05 15:32
那个 2.07323e-317是个垃圾数据,你的构造函数赋值给的widelen
length变量为初始化,所以它的值是不确定是啥
构造函数建议完成成员变量的初始化操作
#16
cainiao123322017-05-05 21:52
...

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

#17
cainiao123322017-05-05 21:55
.,,,

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

#18
cainiao123322017-05-05 21:56
回复 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,这个是如何区分是哪个的实参?
#19
cainiao123322017-05-05 21:57
回复 15楼 CplusGo
感谢回复。我还以为是 没初始化也都有默认值,结果试了几次后结果好像有点不太一样,忘记还有垃圾数据了。
1