类的构造函数

类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。

构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值。

下面的实例有助于更好地理解构造函数的概念:

 #include <iostream>

 using namespace std;

 class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数 private:
double length;
}; // 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
} void Line::setLength( double len )
{
length = len;
} double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line; // 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl; return ;
}

当上面的代码被编译和执行时,它会产生下列结果:

 Object is being created 

 Length of line :  

带参数的构造函数

默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值,如下面的例子所示:

 #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 ;
}

当上面的代码被编译和执行时,它会产生下列结果:

 Object is being created, length =  

 Length of line :  

 Length of line :  

使用初始化列表来初始化字段

使用初始化列表来初始化字段:

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

上面的语法等同于如下语法:

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

假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:

C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
....
}

类的析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

下面的实例有助于更好地理解析构函数的概念:

 #include <iostream>

 using namespace std;

 class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数声明
~Line(); // 这是析构函数声明 private:
double length;
}; // 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
} void Line::setLength( double len )
{
length = len;
} double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line; // 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl; return ;
}

当上面的代码被编译和执行时,它会产生下列结果:

 Object is being created 

 Length of line :  

 Object is being deleted 

最新文章

  1. a与a:link、a:visited、a:hover、a:active
  2. sring mvc 返回值至jsp界面的几种方式
  3. Xcode 重新下载项目配置文件
  4. Web API 和 WCF 的比较
  5. MongoDB 权限
  6. mvc4+jquerymobile页面加载时无法绑定事件
  7. Android 编译时出现r cannot be resolved to a variable
  8. cocos2d-x游戏循环和日程安排
  9. 什么时候需要使用Double? double、float、decimal的区别
  10. HDU 1532 最大流入门
  11. [js高手之路]Node.js模板引擎教程-jade速学与实战2-流程控制,转义与非转义
  12. Vray
  13. 细说javascripe事件传播流程
  14. Jenkins部署的时候报错
  15. java 一个实例
  16. ES6中箭头函数与普通函数this的区别
  17. Linux平台 Oracle 18c RAC安装Part2:GI配置
  18. Linux记录-筛选日志sed、find、tail,du,awk命令
  19. POJ1651(KB-E)
  20. Java jdbc 操作数据库详解

热门文章

  1. ACM 水池数目
  2. jQuery自定义滚动条样式插件mCustomScrollbar
  3. asp.net mvc route 中新发现的小技巧
  4. git两种合并方法 比较merge和rebase
  5. URAL 1427. SMS(DP+单调队列)
  6. 【noiOJ】p7939
  7. 【BZOJ3673】&amp;&amp;【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树
  8. mvc2 To 4
  9. PHP面向对象学习一
  10. gcc 编译器参数