服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++中构造函数与析构函数的详解及其作用介绍

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

2021-12-31 14:55我是小白呀 C/C++

这篇文章主要介绍了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 <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 函数) 时, 调用该全局对象的析构函数.

到此这篇关于C++中构造函数与析构函数的详解及其作用介绍的文章就介绍到这了,更多相关C++ 构造函数 析构函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46274168/article/details/116363849

延伸 · 阅读

精彩推荐