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

目录

  • 构造函数
  • 默认构造函数
  • 有参构造函数
  • 析构函数
  • 析构函数例子
  • 析构函数执行时机
    • 局部对象
    • 全局对象

构造函数 构造函数 (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 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 using namespace std; int main() {Time time1; // 实例化timetime1.show_time(); // 调用show_timereturn 0; }

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

有参构造函数 构造函数中参数可以指定默认值. 如果童虎不指定实参指, 编译西永就使形参取默认值.
例如:
Time 类:
#ifndef PROJECT1_TIME_H#define PROJECT1_TIME_Hclass 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 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 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 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 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 using namespace std; int main() {Student student1(1, "Little white", 'f'); Student student2(2, "Big white", 'f'); student1.display(); student2.display(); return 0; }

【C++中构造函数与析构函数的详解及其作用介绍】输出结果:
执行构造函数: 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 函数) 时, 调用该全局对象的析构函数.
到此这篇关于C++中构造函数与析构函数的详解及其作用介绍的文章就介绍到这了,更多相关C++ 构造函数 析构函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读