|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #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( )
- {
- [backcolor=Yellow]Line line(10.0);[/backcolor]
-
- // 获取默认设置的长度
- cout << "Length of line : " << line.getLength() <<endl;
- // 再次设置长度
- line.setLength(6.0);
- cout << "Length of line : " << line.getLength() <<endl;
-
- return 0;
- }
复制代码
标黄的地方不太懂(第35行),是声明了一个对象吗?
如果想明确一些的话,这里声明、定义并(直接)初始化了一个对象,初始化过程中调用了构造函数。
|
|