C++中构造函数与析构函数的详解及其作用介绍

来自:网络
时间:2021-10-05
阅读:
目录
构造函数默认构造函数有参构造函数析构函数析构函数例子析构函数执行时机局部对象全局对象

构造函数

构造函数 (constructor) 是一种特殊的成员函数. 它会在每次创建类的新对象时执行. 构造函数的名称与类的名称是完全相同的, 并且不会返回任何类型. 构造函数可用于为某些成员变量设置初始值.

C++中构造函数与析构函数的详解及其作用介绍

格式:

Class::Class(); // 构造函数

默认构造函数

如果用户自己没有定义构造函数, C++ 系统会自动生成一个默认构造函数. 这个构造函数体是空的, 没有参数, 不执行初始化操作.

例如:

Time.h:

class Time {
private:
    int hour;
    int minute;
    int second;
public:
    Time(); // 默认构造函数
    void set_time(int h, int m, int s);
    void show_time();
};

Time.cpp:

#include "Time.h"
#include <iostream>

using namespace std;

Time::Time() {
    hour = 0;
    minute = 0;
    second = 0;
}

void Time::set_time(int h, int m, int s) {
    hour = h;
    minute = m;
    second = s;
}

void Time::show_time() {
    cout << hour << ":" << minute << ":" << second << endl;
}

main:

#include "Time.h"
#include <iostream>

using namespace std;

int main() {

    Time time1;  // 实例化time
    time1.show_time();  // 调用show_time

    return 0;
}

输出结果:

0:0:0

重点:

即使提供了其他构造函数, 提供一个默认构造函数几乎总是对的 通常在默认构造函数中, 给成员提供的初始值应该指出该对象是 “空” 的

有参构造函数

构造函数中参数可以指定默认值. 如果童虎不指定实参指, 编译西永就使形参取默认值.

例如:

Time 类:

#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H

class Time {
private:
    int hour;
    int minute;
    int second;
public:
    Time();  // 默认构造函数
    Time(int h, int m=0, int s=0);  // 有参构造函数
    void show_time();
};

#endif //PROJECT1_TIME_H

Time.cpp:

#include "Time.h"
#include <iostream>
using namespace std;

// 默认构造函数
Time::Time() : hour(0), minute(0), second(0) {}

// 有参构造函数
Time::Time(int h, int m, int s) : hour(h), minute(m), second(s) {}


void Time::show_time() {
    cout << hour << ":" << minute << ":" << second << endl;
}

main:

#include "Time.h"
#include <iostream>
using namespace std;

int main() {

    Time time1;
    time1.show_time();

    Time time2(8);
    time2.show_time();

    Time time3(8, 8);
    time3.show_time();
    
    Time time4(8, 8, 8);
    time4.show_time();

    return 0;
}

输出结果:

0:0:0
8:0:0
8:8:0
8:8:8

析构函数

析构函数 (destructor) 也是一个特殊的成员函数. 当对象的生命期结束时, 会自动执行析构函数. 析构函数的名字是类名前面加一个 “~” 符号.

C++中构造函数与析构函数的详解及其作用介绍

格式:

Class::~Class();  // 析构函数

析构函数的作用在撤销对象占用的内存之前完成一些清理 & 善后的工作.

析构函数例子

Student 类:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    int num;
    string name;
    char gender;
public:
    Student();
    Student(int num, string name, char gender);
    ~Student();
    void display();
};

#endif //PROJECT1_STUDENT_H

Student.cpp:

#include "Student.h"
#include <iostream>
using namespace std;

// 无参构造
Student::Student() : num(-1), name("None"), gender('N') {}

Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
    cout << "执行构造函数: " << "Welcome, " << name << endl;
}

void Student::display() {
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "gender: " << gender << endl;
    cout << "===============" << endl;
}

Student::~Student() {
    cout << "执行析构函数: " << "Bye bye, " << name << endl;
}

main:

#include "Student.h"
#include <iostream>
using namespace std;

int main() {

    Student student1(1, "Little white", 'f');
    Student student2(2, "Big white", 'f');

    student1.display();
    student2.display();

    return 0;
}

输出结果:

执行构造函数: Welcome, Little white
执行构造函数: Welcome, Big white
num: 1
name: Little white
gender: f
===============
num: 2
name: Big white
gender: f
===============
执行析构函数: Bye bye, Big white
执行析构函数: Bye bye, Little white

析构函数执行时机

对于函数中定义的自动局部对象, 当函数被调用结束时, 对象释放. 在对象释放前自自动执行析构函数.

C++中构造函数与析构函数的详解及其作用介绍

局部对象

static 局部对象只在 main 函数结束或调用 exit 函数结束程序时, 调用 static 局部对象的洗后函数.

全局对象

对于全局对象, 在程序的流程离开其作用域时 (如 main 函数结束或调用 exit 函数) 时, 调用该全局对象的析构函数.

返回顶部
顶部