您好,登录后才能下订单哦!
本篇内容介绍了“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_HTime.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_HStudent.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 函数) 时, 调用该全局对象的析构函数.
“C++中构造函数与析构函数的实例介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。