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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

我是小白呀   2021-09-07 我要评论
想了解C++中构造函数与析构函数的详解及其作用介绍的相关内容吗,我是小白呀在本文为您仔细讲解C++ 构造函数 析构函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,构造函数,C++,析构函数,下面大家一起来学习吧。

构造函数

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

在这里插入图片描述

格式:

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

在这里插入图片描述

格式:

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

析构函数执行时机

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

在这里插入图片描述

局部对象

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

全局对象

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们