C++ 类构造函数 & 析构函数


类的构造函数

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

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

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

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(); // 这是构造函数
  9. private:
  10. double length;
  11. };
  12. // 成员函数定义,包括构造函数
  13. Line::Line(void)
  14. {
  15. cout << "Object is being created" << endl;
  16. }
  17. void Line::setLength( double len )
  18. {
  19. length = len;
  20. }
  21. double Line::getLength( void )
  22. {
  23. return length;
  24. }
  25. // 程序的主函数
  26. int main( )
  27. {
  28. Line line;
  29. // 设置长度
  30. line.setLength(6.0);
  31. cout << "Length of line : " << line.getLength() <<endl;
  32. return 0;
  33. }

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

  1. Object is being created
  2. Length of line : 6

带参数的构造函数

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

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(double len); // 这是构造函数
  9. private:
  10. double length;
  11. };
  12. // 成员函数定义,包括构造函数
  13. Line::Line( double len)
  14. {
  15. cout << "Object is being created, length = " << len << endl;
  16. length = len;
  17. }
  18. void Line::setLength( double len )
  19. {
  20. length = len;
  21. }
  22. double Line::getLength( void )
  23. {
  24. return length;
  25. }
  26. // 程序的主函数
  27. int main( )
  28. {
  29. Line line(10.0);
  30. // 获取默认设置的长度
  31. cout << "Length of line : " << line.getLength() <<endl;
  32. // 再次设置长度
  33. line.setLength(6.0);
  34. cout << "Length of line : " << line.getLength() <<endl;
  35. return 0;
  36. }

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

  1. Object is being created, length = 10
  2. Length of line : 10
  3. Length of line : 6

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

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

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

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

  1. Line::Line( double len)
  2. {
  3. cout << "Object is being created, length = " << len << endl;
  4. length = len;
  5. }

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

  1. C::C( double a, double b, double c): X(a), Y(b), Z(c)
  2. {
  3. ....
  4. }

类的析构函数

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

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

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

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. void setLength( double len );
  7. double getLength( void );
  8. Line(); // 这是构造函数声明
  9. ~Line(); // 这是析构函数声明
  10. private:
  11. double length;
  12. };
  13. // 成员函数定义,包括构造函数
  14. Line::Line(void)
  15. {
  16. cout << "Object is being created" << endl;
  17. }
  18. Line::~Line(void)
  19. {
  20. cout << "Object is being deleted" << endl;
  21. }
  22. void Line::setLength( double len )
  23. {
  24. length = len;
  25. }
  26. double Line::getLength( void )
  27. {
  28. return length;
  29. }
  30. // 程序的主函数
  31. int main( )
  32. {
  33. Line line;
  34. // 设置长度
  35. line.setLength(6.0);
  36. cout << "Length of line : " << line.getLength() <<endl;
  37. return 0;
  38. }

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

  1. Object is being created
  2. Length of line : 6
  3. Object is being deleted