C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
構(gòu)造函數(shù)
構(gòu)造函數(shù) (constructor) 是一種特殊的成員函數(shù). 它會在每次創(chuàng)建類的新對象時執(zhí)行. 構(gòu)造函數(shù)的名稱與類的名稱是完全相同的, 并且不會返回任何類型. 構(gòu)造函數(shù)可用于為某些成員變量設(shè)置初始值.

格式:
Class::Class(); // 構(gòu)造函數(shù)
默認(rèn)構(gòu)造函數(shù)
如果用戶自己沒有定義構(gòu)造函數(shù), C++ 系統(tǒng)會自動生成一個默認(rèn)構(gòu)造函數(shù). 這個構(gòu)造函數(shù)體是空的, 沒有參數(shù), 不執(zhí)行初始化操作.
例如:
Time.h:
class Time {
private:
int hour;
int minute;
int second;
public:
Time(); // 默認(rèn)構(gòu)造函數(shù)
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(); // 調(diào)用show_time
return 0;
}
輸出結(jié)果:
0:0:0
重點:
- 即使提供了其他構(gòu)造函數(shù), 提供一個默認(rèn)構(gòu)造函數(shù)幾乎總是對的
- 通常在默認(rèn)構(gòu)造函數(shù)中, 給成員提供的初始值應(yīng)該指出該對象是 “空” 的
有參構(gòu)造函數(shù)
構(gòu)造函數(shù)中參數(shù)可以指定默認(rèn)值. 如果童虎不指定實參指, 編譯西永就使形參取默認(rèn)值.
例如:
Time 類:
#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H
class Time {
private:
int hour;
int minute;
int second;
public:
Time(); // 默認(rèn)構(gòu)造函數(shù)
Time(int h, int m=0, int s=0); // 有參構(gòu)造函數(shù)
void show_time();
};
#endif //PROJECT1_TIME_H
Time.cpp:
#include "Time.h"
#include <iostream>
using namespace std;
// 默認(rèn)構(gòu)造函數(shù)
Time::Time() : hour(0), minute(0), second(0) {}
// 有參構(gòu)造函數(shù)
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;
}
輸出結(jié)果:
0:0:0
8:0:0
8:8:0
8:8:8
析構(gòu)函數(shù)
析構(gòu)函數(shù) (destructor) 也是一個特殊的成員函數(shù). 當(dāng)對象的生命期結(jié)束時, 會自動執(zhí)行析構(gòu)函數(shù). 析構(gòu)函數(shù)的名字是類名前面加一個 “~” 符號.

格式:
Class::~Class(); // 析構(gòu)函數(shù)
析構(gòu)函數(shù)的作用在撤銷對象占用的內(nèi)存之前完成一些清理 & 善后的工作.
析構(gòu)函數(shù)例子
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;
// 無參構(gòu)造
Student::Student() : num(-1), name("None"), gender('N') {}
Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
cout << "執(zhí)行構(gòu)造函數(shù): " << "Welcome, " << name << endl;
}
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
cout << "===============" << endl;
}
Student::~Student() {
cout << "執(zhí)行析構(gòu)函數(shù): " << "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;
}
輸出結(jié)果:
執(zhí)行構(gòu)造函數(shù): Welcome, Little white
執(zhí)行構(gòu)造函數(shù): Welcome, Big white
num: 1
name: Little white
gender: f
===============
num: 2
name: Big white
gender: f
===============
執(zhí)行析構(gòu)函數(shù): Bye bye, Big white
執(zhí)行析構(gòu)函數(shù): Bye bye, Little white
析構(gòu)函數(shù)執(zhí)行時機(jī)
對于函數(shù)中定義的自動局部對象, 當(dāng)函數(shù)被調(diào)用結(jié)束時, 對象釋放. 在對象釋放前自自動執(zhí)行析構(gòu)函數(shù).

局部對象
static 局部對象只在 main 函數(shù)結(jié)束或調(diào)用 exit 函數(shù)結(jié)束程序時, 調(diào)用 static 局部對象的洗后函數(shù).
全局對象
對于全局對象, 在程序的流程離開其作用域時 (如 main 函數(shù)結(jié)束或調(diào)用 exit 函數(shù)) 時, 調(diào)用該全局對象的析構(gòu)函數(shù).
到此這篇關(guān)于C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++ 構(gòu)造函數(shù) 析構(gòu)函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實現(xiàn)
- C++深入講解對象的銷毀之析構(gòu)函數(shù)
- 詳解C++中虛析構(gòu)函數(shù)的作用及其原理分析
- 詳解C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)
- C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序詳解
- 淺談C++基類的析構(gòu)函數(shù)為虛函數(shù)
- C++類成員構(gòu)造函數(shù)和析構(gòu)函數(shù)順序示例詳細(xì)講解
- C++ 私有析構(gòu)函數(shù)的作用示例詳解
相關(guān)文章
從匯編看c++中默認(rèn)構(gòu)造函數(shù)的使用分析
c++中,如果為一個類沒有明確定義一個構(gòu)造函數(shù),那么,編譯器就會自動合成一個默認(rèn)的構(gòu)造函數(shù)。下面,通過匯編程序,來看一下其真實情況2013-05-05

