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

类的构造函数

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

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

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

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

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

1 Object is being created 

2 Length of line : 6
 

带参数的构造函数

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

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

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

1 Object is being created, length = 10 

2 Length of line : 10

3 Length of line : 6
 

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

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

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)
{
  ....
}

类的析构函数

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

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

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

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

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

1 Object is being created 

2 Length of line : 6

3 Object is being deleted
 
原文地址:https://www.cnblogs.com/luorende/p/6037253.html