一起來(lái)學(xué)習(xí)C++的構(gòu)造和析構(gòu)
1. 構(gòu)造函數(shù)
1.1 構(gòu)造函數(shù)長(zhǎng)什么樣子
(1) 函數(shù)名和類名相同
(2) 沒(méi)有返回值
(3) 如果不寫構(gòu)造函數(shù),任何類中都存在一個(gè)默認(rèn)的構(gòu)造函數(shù)
I 默認(rèn)的構(gòu)造函數(shù)是無(wú)參的
II 當(dāng)我們自己寫了構(gòu)造函數(shù),默認(rèn)的構(gòu)造函數(shù)就不存在
(4) 構(gòu)造函數(shù)在構(gòu)造對(duì)象的時(shí)候調(diào)用
(5) delete可以用來(lái)刪掉默認(rèn)的函數(shù)
(6) 指定使用默認(rèn)的無(wú)參構(gòu)造函數(shù),用default說(shuō)明
(7) 允許構(gòu)造函數(shù)調(diào)用另一個(gè)構(gòu)造函數(shù),只是要用初始化參數(shù)列表的寫法
(8) 初始化參數(shù)列表 : 只有構(gòu)造函數(shù)有
I 構(gòu)造函數(shù)名(參數(shù)1,參數(shù)2,…):成員1(參數(shù)1),成員2(參數(shù)2),…{}
II 避免形參名和數(shù)據(jù)成員名相同的導(dǎo)致問(wèn)題
1.2 構(gòu)造函數(shù)干嘛的
(1) 構(gòu)造函數(shù)用來(lái)構(gòu)造對(duì)象
(2) 構(gòu)造函數(shù)更多是用來(lái)初始化數(shù)據(jù)成員
1.3 思考
(1)為什么不寫構(gòu)造函數(shù)可以構(gòu)造對(duì)象? 是因?yàn)榇嬖谝粋€(gè)默認(rèn)的無(wú)參構(gòu)造函數(shù),所以可以構(gòu)造無(wú)參對(duì)象
(2) 構(gòu)造函數(shù)重載為了什么? 為了構(gòu)造不同長(zhǎng)相的對(duì)象。
#include <iostream> using namespace std; class MM { public: //MM() = delete; 刪掉默認(rèn)的構(gòu)造函數(shù) MM(string mmName, int mmAge) { name = mmName; age = mmAge; cout << "帶參構(gòu)造函數(shù)" << endl; } //MM() //{ // cout << "無(wú)參構(gòu)造函數(shù)" << endl; //} MM() = default; //使用的是默認(rèn)無(wú)參構(gòu)造函數(shù) void print() { cout << name << " " << age << endl; } protected: string name="Lisa"; int age=18; }; //為了能夠構(gòu)造不同長(zhǎng)相的對(duì)象,我們會(huì)給構(gòu)造函數(shù)缺省處理 class Boy { public: //Boy(string mname="", int mage=19) //{ // name = mname; // age = mage; //} //上面函數(shù) 等效可以實(shí)現(xiàn)下面三個(gè)函數(shù)的功能 Boy() {} Boy(string mName) { name = mName; } //出錯(cuò):沒(méi)有與之匹配的構(gòu)造函數(shù) Boy(string mName, int mage) { name = mName; age = mage; } protected: string name; int age; }; //初始化參數(shù)列表的寫法 string girlName = "Baby"; class Student { public: Student(string mname="", int mage=18) :name(mname), age(mage) { cout << "初始化參數(shù)列表" << endl; //繼承和類的組合必須采用初始化參數(shù)列表寫法 } Student(int mage) :name(girlName), age(mage) {} protected: string name; int age; }; //構(gòu)造函數(shù)可以調(diào)用另一個(gè)構(gòu)造函數(shù)初始化數(shù)據(jù) class TT { public: TT(string name, int age) :name(name), age(age) {} //委托構(gòu)造:允許構(gòu)造函數(shù)調(diào)用另一個(gè)構(gòu)造函數(shù) TT():TT("默認(rèn)",18) {} //沒(méi)有給數(shù)據(jù)初始化 void print() { cout << name << "\t" << age << endl; } protected: string name; int age; }; int main() { //MM mm; 構(gòu)造無(wú)參的對(duì)象,需要無(wú)參構(gòu)造函數(shù) MM mm("mm", 28); mm.print(); MM girl; girl.print(); Boy boy1; Boy boy2("流浪之子"); Boy boy3("王子", 18); TT tt; tt.print(); return 0; }
2. 析構(gòu)函數(shù)
2.1 析構(gòu)函數(shù)長(zhǎng)什么樣子?
(1) 無(wú)返回值
(2) 無(wú)參數(shù)
(3) 函數(shù)名: ~類名
(4) 不寫的話會(huì)存在默認(rèn)的析構(gòu)函數(shù)
(5) 析構(gòu)函數(shù)不需要自己 調(diào)用,對(duì)象死亡的之前會(huì)調(diào)用析構(gòu)函數(shù)
2.2 析構(gòu)函數(shù)用來(lái)干嘛?(什么時(shí)候需要自己手動(dòng)寫析構(gòu)函數(shù))
(1) 當(dāng)類中的數(shù)據(jù)成員是指針,并且動(dòng)態(tài)申請(qǐng)內(nèi)存就需要手寫析構(gòu)
(2) 析構(gòu)函數(shù)用來(lái)釋放數(shù)據(jù)成員申請(qǐng)動(dòng)態(tài)內(nèi)存
3. 拷貝構(gòu)造函數(shù)
-> 拷貝構(gòu)造函數(shù)也是構(gòu)造函數(shù),長(zhǎng)相和構(gòu)造函數(shù)一樣的,只是參數(shù)是固定 .拷貝構(gòu)造函數(shù)唯一的參數(shù)是對(duì)對(duì)象引用
-> 不寫拷貝構(gòu)造函數(shù),也存在一個(gè)默認(rèn)的拷貝構(gòu)造函數(shù)
-> 拷貝構(gòu)造函數(shù)作用: 通過(guò)一個(gè)對(duì)象去初始化另一個(gè)對(duì)象
問(wèn)題
I 什么時(shí)候調(diào)用拷貝構(gòu)造?
答:當(dāng)通過(guò)一個(gè)對(duì)象去創(chuàng)建出來(lái)另一個(gè)新的對(duì)象時(shí)候需要調(diào)用拷貝
II 拷貝構(gòu)造什么時(shí)候需要加const修飾參數(shù)?
答:當(dāng)存在匿名對(duì)象賦值操作的時(shí)候,必須要const修飾
#include <iostream> #include <string> using namespace std; class MM { public: MM() = default; MM(string name, int age) :name(name), age(age) {} void print() { cout << name << "\t" << age << endl; } //拷貝構(gòu)造 MM(const MM& mm) //MM girl(mm); { name = mm.name; //girl.name=mm.name age = mm.age; //girl.age=mm.age cout << "拷貝構(gòu)造" << endl; } protected: string name; int age; }; void printData(MM mm) //MM mm=實(shí)參; { mm.print(); } void printData2(MM& mm) //不存在拷貝本 { mm.print(); } int main() { MM mm("mm", 18); mm.print(); //顯示調(diào)用調(diào)用 cout << "顯示調(diào)用調(diào)用" << endl; MM girl(mm); //通過(guò)一個(gè)對(duì)象創(chuàng)建另一個(gè)對(duì)象 girl.print(); //隱式調(diào)用 cout << "隱式調(diào)用" << endl; MM girl2 = mm; //拷貝構(gòu)造 girl2.print(); MM girl3; girl3 = mm; //運(yùn)算符重載 girl3.print(); //函數(shù)傳參 cout << "第一種調(diào)用形態(tài)" << endl; printData(mm); cout << "第二種調(diào)用形態(tài)" << endl; printData2(mm); //無(wú)名對(duì)象 匿名對(duì)象 MM temp; temp = MM("匿名", 18); temp.print(); //匿名對(duì)象創(chuàng)建對(duì)象時(shí)候,拷貝構(gòu)造一定要用const修飾 MM temp2 = MM("匿名", 199); return 0; }
4. 深淺拷貝
(1)淺拷貝: 默認(rèn)的拷貝構(gòu)造叫做淺拷貝
(2)深拷貝: 拷貝構(gòu)造函數(shù)中做了new內(nèi)存操作,并且做拷貝賦值的操作
#include<iostream> #include <cstring> #include <string> using namespace std; class MM { public: MM(const char* mname, int age) :age(age) { name = new char[strlen(mname) + 1]; strcpy_s(name, strlen(mname) + 1, mname); } void print() { cout << name << "\t" << age << endl; } MM(const MM& object) { //name = object.name; name = new char[strlen(object.name) + 1]; strcpy_s(name, strlen(object.name) + 1, object.name); //name = object.name; age = object.age; } ~MM() { delete[] name; } protected: char* name; int age; }; int main() { { MM mm("baby", 19); MM girl(mm); MM gm = mm; mm.print(); girl.print(); gm.print(); } return 0; }
5. 構(gòu)造和析構(gòu)順序問(wèn)題
(1)普通對(duì)象,構(gòu)造順序和析構(gòu)順序是相反
(2)new出來(lái)的對(duì)象,delete會(huì)直接調(diào)用析構(gòu)函數(shù)
(3)static對(duì)象,當(dāng)程序關(guān)閉的時(shí)候,生命周期才結(jié)束,所以是最后釋放
#include <iostream> #include <string> using namespace std; class MM { public: MM(string name="x") :name(name) { cout << name; } ~MM(){ cout << name; } protected: string name; }; int main() { { MM mm1("A"); //A static MM mm2("B"); //B 程序關(guān)閉時(shí)候才死亡,最后析構(gòu) MM* p3 = new MM("C"); //C MM mm4[4]; //xxxx delete p3; //C delete 直接調(diào)用析構(gòu) p3 = nullptr; //xxxxAB } //ABCxxxxCxxxxAB return 0; }
6. C++結(jié)構(gòu)體
#include <iostream> #include <string> using namespace std; struct MM { //默認(rèn)為公有屬性 //類中默認(rèn)屬性是私有屬性 //protected: string name; int age; public: MM(string name) :name(name) { cout << "構(gòu)造函數(shù)" << endl; } MM(const MM& object) { name = object.name; age = object.age; cout << "拷貝構(gòu)造" << endl; } ~MM() { } }; int main() { //采用創(chuàng)建時(shí)候賦值的方式,也是調(diào)用構(gòu)造函數(shù) //MM object = { "lisa",19 }; 錯(cuò)誤,因?yàn)闆](méi)有兩個(gè)參數(shù)的構(gòu)造函數(shù) MM object = { "lisa" }; cout << object.name << "\t" << object.age << endl; //C++結(jié)構(gòu)體一旦寫了構(gòu)造函數(shù),就必須按照C++類的方式的去用 MM mm(object); cout << mm.name << "\t" << mm.age << endl; return 0; }
答疑:
- 為什么要手動(dòng)寫析構(gòu)函數(shù)? 因?yàn)槟J(rèn)的不會(huì)釋放數(shù)據(jù)成員動(dòng)態(tài)申請(qǐng)的內(nèi)存
- 函數(shù)名和類型相同函數(shù)叫做構(gòu)造函數(shù)
- 函數(shù)名字是~類名的無(wú)參函數(shù)叫做析構(gòu)函數(shù)
- 以對(duì)象的引用為參數(shù)的構(gòu)造函數(shù)叫做拷貝構(gòu)造函數(shù)(復(fù)制構(gòu)造函數(shù))
- 怎么寫出來(lái),默認(rèn)的構(gòu)造函數(shù),就是那種在沒(méi)有傳參的時(shí)候的那一串垃圾值
class Boy { public: Boy() {} void print() { cout << a << "\t" << b << "\t" << c << endl; } protected: int a; int b; int c; }; int main() { return 0; }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- C++構(gòu)造析構(gòu)賦值運(yùn)算函數(shù)應(yīng)用詳解
- C++淺析構(gòu)造函數(shù)的特性
- C++類與對(duì)象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實(shí)現(xiàn)
- C++分析類的對(duì)象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點(diǎn)梳理
- C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載詳解
- C++編程析構(gòu)函數(shù)拷貝構(gòu)造函數(shù)使用示例詳解
- C++類的構(gòu)造與析構(gòu)特點(diǎn)及作用詳解
相關(guān)文章
C語(yǔ)言動(dòng)態(tài)內(nèi)存分配的詳解
這篇文章主要介紹了C語(yǔ)言動(dòng)態(tài)內(nèi)存分配的詳解的相關(guān)資料,這里提供了實(shí)現(xiàn)方法整理和出現(xiàn)錯(cuò)誤的解決辦法,需要的朋友可以參考下2017-07-07C++中 STL list詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了C++中 STL list詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04C++ Qt開發(fā)之LineEdit單行輸入組件的用法詳解
Qt 是一個(gè)跨平臺(tái)C++圖形界面開發(fā)庫(kù),利用Qt可以快速開發(fā)跨平臺(tái)窗體應(yīng)用程序,在Qt中我們可以通過(guò)拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點(diǎn)介紹LineEdit單行輸入框組件的常用方法及靈活運(yùn)用2023-12-12opencv3/C++實(shí)現(xiàn)霍夫圓/直線檢測(cè)
今天小編就為大家分享一篇opencv3/C++實(shí)現(xiàn)霍夫圓/直線檢測(cè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12