一起來學(xué)習(xí)C++的構(gòu)造和析構(gòu)
1. 構(gòu)造函數(shù)
1.1 構(gòu)造函數(shù)長什么樣子
(1) 函數(shù)名和類名相同
(2) 沒有返回值
(3) 如果不寫構(gòu)造函數(shù),任何類中都存在一個默認(rèn)的構(gòu)造函數(shù)
I 默認(rèn)的構(gòu)造函數(shù)是無參的
II 當(dāng)我們自己寫了構(gòu)造函數(shù),默認(rèn)的構(gòu)造函數(shù)就不存在
(4) 構(gòu)造函數(shù)在構(gòu)造對象的時候調(diào)用
(5) delete可以用來刪掉默認(rèn)的函數(shù)
(6) 指定使用默認(rèn)的無參構(gòu)造函數(shù),用default說明
(7) 允許構(gòu)造函數(shù)調(diào)用另一個構(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)致問題
1.2 構(gòu)造函數(shù)干嘛的
(1) 構(gòu)造函數(shù)用來構(gòu)造對象
(2) 構(gòu)造函數(shù)更多是用來初始化數(shù)據(jù)成員
1.3 思考
(1)為什么不寫構(gòu)造函數(shù)可以構(gòu)造對象? 是因為存在一個默認(rèn)的無參構(gòu)造函數(shù),所以可以構(gòu)造無參對象
(2) 構(gòu)造函數(shù)重載為了什么? 為了構(gòu)造不同長相的對象。
#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 << "無參構(gòu)造函數(shù)" << endl;
//}
MM() = default; //使用的是默認(rèn)無參構(gòu)造函數(shù)
void print()
{
cout << name << " " << age << endl;
}
protected:
string name="Lisa";
int age=18;
};
//為了能夠構(gòu)造不同長相的對象,我們會給構(gòu)造函數(shù)缺省處理
class Boy
{
public:
//Boy(string mname="", int mage=19)
//{
// name = mname;
// age = mage;
//}
//上面函數(shù) 等效可以實現(xiàn)下面三個函數(shù)的功能
Boy() {}
Boy(string mName) { name = mName; }
//出錯:沒有與之匹配的構(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òu)造函數(shù)初始化數(shù)據(jù)
class TT
{
public:
TT(string name, int age) :name(name), age(age) {}
//委托構(gòu)造:允許構(gòu)造函數(shù)調(diào)用另一個構(gòu)造函數(shù)
TT():TT("默認(rèn)",18) {} //沒有給數(shù)據(jù)初始化
void print()
{
cout << name << "\t" << age << endl;
}
protected:
string name;
int age;
};
int main()
{
//MM mm; 構(gòu)造無參的對象,需要無參構(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ù)長什么樣子?
(1) 無返回值
(2) 無參數(shù)
(3) 函數(shù)名: ~類名
(4) 不寫的話會存在默認(rèn)的析構(gòu)函數(shù)
(5) 析構(gòu)函數(shù)不需要自己 調(diào)用,對象死亡的之前會調(diào)用析構(gòu)函數(shù)
2.2 析構(gòu)函數(shù)用來干嘛?(什么時候需要自己手動寫析構(gòu)函數(shù))
(1) 當(dāng)類中的數(shù)據(jù)成員是指針,并且動態(tài)申請內(nèi)存就需要手寫析構(gòu)
(2) 析構(gòu)函數(shù)用來釋放數(shù)據(jù)成員申請動態(tài)內(nèi)存
3. 拷貝構(gòu)造函數(shù)
-> 拷貝構(gòu)造函數(shù)也是構(gòu)造函數(shù),長相和構(gòu)造函數(shù)一樣的,只是參數(shù)是固定 .拷貝構(gòu)造函數(shù)唯一的參數(shù)是對對象引用
-> 不寫拷貝構(gòu)造函數(shù),也存在一個默認(rèn)的拷貝構(gòu)造函數(shù)
-> 拷貝構(gòu)造函數(shù)作用: 通過一個對象去初始化另一個對象
問題
I 什么時候調(diào)用拷貝構(gòu)造?
答:當(dāng)通過一個對象去創(chuàng)建出來另一個新的對象時候需要調(diào)用拷貝
II 拷貝構(gòu)造什么時候需要加const修飾參數(shù)?
答:當(dāng)存在匿名對象賦值操作的時候,必須要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=實參;
{
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); //通過一個對象創(chuàng)建另一個對象
girl.print();
//隱式調(diào)用
cout << "隱式調(diào)用" << endl;
MM girl2 = mm; //拷貝構(gòu)造
girl2.print();
MM girl3;
girl3 = mm; //運算符重載
girl3.print();
//函數(shù)傳參
cout << "第一種調(diào)用形態(tài)" << endl;
printData(mm);
cout << "第二種調(diào)用形態(tài)" << endl;
printData2(mm);
//無名對象 匿名對象
MM temp;
temp = MM("匿名", 18);
temp.print();
//匿名對象創(chuàng)建對象時候,拷貝構(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)順序問題
(1)普通對象,構(gòu)造順序和析構(gòu)順序是相反
(2)new出來的對象,delete會直接調(diào)用析構(gòu)函數(shù)
(3)static對象,當(dāng)程序關(guān)閉的時候,生命周期才結(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)閉時候才死亡,最后析構(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)建時候賦值的方式,也是調(diào)用構(gòu)造函數(shù)
//MM object = { "lisa",19 }; 錯誤,因為沒有兩個參數(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;
}
答疑:
- 為什么要手動寫析構(gòu)函數(shù)? 因為默認(rèn)的不會釋放數(shù)據(jù)成員動態(tài)申請的內(nèi)存
- 函數(shù)名和類型相同函數(shù)叫做構(gòu)造函數(shù)
- 函數(shù)名字是~類名的無參函數(shù)叫做析構(gòu)函數(shù)
- 以對象的引用為參數(shù)的構(gòu)造函數(shù)叫做拷貝構(gòu)造函數(shù)(復(fù)制構(gòu)造函數(shù))
- 怎么寫出來,默認(rèn)的構(gòu)造函數(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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- C++構(gòu)造析構(gòu)賦值運算函數(shù)應(yīng)用詳解
- C++淺析構(gòu)造函數(shù)的特性
- C++類與對象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實現(xiàn)
- C++分析類的對象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點梳理
- C++繼承中的對象構(gòu)造與析構(gòu)和賦值重載詳解
- C++編程析構(gòu)函數(shù)拷貝構(gòu)造函數(shù)使用示例詳解
- C++類的構(gòu)造與析構(gòu)特點及作用詳解
相關(guān)文章
C++ Qt開發(fā)之LineEdit單行輸入組件的用法詳解
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點介紹LineEdit單行輸入框組件的常用方法及靈活運用2023-12-12

