您好,登錄后才能下訂單哦!
有這么一段代碼,
class Person{ private: char* name; char age; public: void SetName ( char* name ){ this->name = name; } int SetAge ( char age ){ this->age = age; return 1; } }; Person person; person.SetName ( "chentong" ); person.SetAge ( 20 );
我想要初始化成員變量,那么我就得每次通過類變量調(diào)用成員函數(shù)來(lái)實(shí)現(xiàn),這樣做當(dāng)然可以,不過比較麻煩,為了解決這個(gè)麻煩,C++引入了構(gòu)造函數(shù)這樣一個(gè)概念。什么是構(gòu)造函數(shù)?與類名相同的函數(shù),叫做構(gòu)造函數(shù)。構(gòu)造函數(shù)可以有多個(gè),如何區(qū)分?通過參數(shù)列表的不同來(lái)區(qū)分。若是我們沒有自己寫構(gòu)造函數(shù),那么,系統(tǒng)會(huì)自動(dòng)調(diào)用默認(rèn)構(gòu)造函數(shù)。但是,如果我們自己實(shí)現(xiàn)了構(gòu)造函數(shù),系統(tǒng)就不會(huì)再調(diào)用默認(rèn)構(gòu)造函數(shù)了。代碼如下,
class Person{ private: char* name; char age; public: void SetName ( char* name ){ this->name = name; } int SetAge ( char age ){ this->age = age; return 1; } void Person () { } void Person ( char* name, char age ){ this->name = name; this->age = age; } };
那么我們想要初始化變量,只要,
Person person ( "chentong", 20 );
這樣,只要調(diào)用一次構(gòu)造函數(shù)就對(duì)所有成員進(jìn)行了設(shè)置。
那么,調(diào)用不帶參數(shù)的構(gòu)造函數(shù),如下:
Person person1;
這樣就是調(diào)用了不帶參數(shù)的構(gòu)造函數(shù)而不能寫成這樣,
Person person1();
因?yàn)?,這樣寫的意義是,聲明一個(gè)函數(shù)。因?yàn)?,這樣寫,就相當(dāng)于,int fac(); //函數(shù)的聲明。順帶說一句,自己重寫構(gòu)造函數(shù)后,一定要寫一個(gè)不帶參數(shù)的構(gòu)造函數(shù),否則編譯無(wú)法通過。
通過指針訪問
Person* person1 = new Person; Person* person2 = new Person(); //Person1和Person2的作用完全一樣,都會(huì)調(diào)用無(wú)參構(gòu)造函數(shù) Person* person3 = new Person [2]; Person* person4 = new Person ( "chentong", 20 );
釋放內(nèi)存
delete person1; delete person2; delete []person3; delete person4;
有如下代碼:
class Person{ private: char* name; char age; char* job; public: void SetName ( char* name ){ this->name = name; } int SetAge ( char age ){ this->age = age; return 1; } void SetJob ( char* job ){ this->job = job; } void Person(){ } void Person( char* name, char age, char* job ){ this->name = new char[strlen(name)+1]; strcpy ( this->name, name ); this->age = age; this->job = new char[stlren(job) + 1]; strcpy ( this->job, job ); } void Person ( char* name, char age, char* job = "none" ){ //this->name = name; this->name = new char[strlen[name + 1]; strcpy ( this->name, name ); this->age = age; //this->job = job; this->job = new char[strlen[name + 1]; strcpy ( this->job, job ); } };
這段代碼通過new動(dòng)態(tài)申請(qǐng)空間,來(lái)存放數(shù)據(jù)。C++中,new用來(lái)動(dòng)態(tài)申請(qǐng)空間,delete用來(lái)釋放動(dòng)態(tài)申請(qǐng)的空間。那么C中的malloc()和C++中的new有什么區(qū)別呢?它們都能用來(lái)動(dòng)態(tài)的申請(qǐng)空間,malloc申請(qǐng)的空間,如果不去主動(dòng)釋放,那么被申請(qǐng)的空間將一直不會(huì)釋放,這樣就會(huì)造成內(nèi)存的浪費(fèi)。而new動(dòng)態(tài)申請(qǐng)的空間,在程序執(zhí)行時(shí)不會(huì)釋放,直到程序執(zhí)行完畢,動(dòng)態(tài)申請(qǐng)的空間才會(huì)被釋放。如果想要在空間使用完畢后就釋放,只要通過delete就可以做到了。
很明顯,我們可以把釋放空間的代碼寫成一個(gè)函數(shù),當(dāng)空間使用完畢后,直接調(diào)用釋放函數(shù),空間就可以被釋放了。如果動(dòng)態(tài)申請(qǐng)的空間比較多,可能會(huì)出現(xiàn)遺漏釋放的情況,所以,為了避免這種情況出現(xiàn),C++引入了析構(gòu)函數(shù)這樣一個(gè)概念。當(dāng)動(dòng)態(tài)空間使用完畢后,析構(gòu)函數(shù)會(huì)自動(dòng)別調(diào)用,這樣以來(lái),空間就被釋放了。析構(gòu)函數(shù)和構(gòu)造函數(shù)類似,都是與類名相同,只不過是,在類名之前加了一個(gè)波浪線~。順帶一提,構(gòu)造函數(shù)和析構(gòu)函數(shù)都沒有返回值類型。代碼如下:
class People { private: char* name; char age; char* job; public: void SetName(char* name) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); } int SetAge(char age) { if (age > 150 || age < 0) { age = 0; return -1; } this->age = age; } void SetJob(char* job) { this->job = new char[strlen(job) + 1]; strcpy(this->job, job); } void PrintInfo() { cout << "name = " << this->name << ", age = " << this->age << ", job = " << this->job << endl; } People() { this->name = NULL; this->job = NULL; } People(char* name, char age, char* job) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; this->job = new char[strlen(job) + 1]; strcpy(this->job, job); } ~People() { if (this->name != NULL) delete this->name; if (this->job != NULL) delete this->job; } };
這里的~People()就是析構(gòu)函數(shù)。當(dāng)程序執(zhí)行完畢后,析構(gòu)函數(shù)會(huì)自動(dòng)被調(diào)用。動(dòng)態(tài)申請(qǐng)的空間就被釋放了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。