有這麼一段代碼,
ide
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 );
我想要初始化成員變量,那麼我就得每次經過類變量調用成員函數來實現,這樣作固然能夠,不過比較麻煩,爲了解決這個麻煩,C++引入了構造函數這樣一個概念。什麼是構造函數?與類名相同的函數,叫作構造函數。構造函數能夠有多個,如何區分?經過參數列表的不一樣來區分。如果咱們沒有本身寫構造函數,那麼,系統會自動調用默認構造函數。可是,若是咱們本身實現了構造函數,系統就不會再調用默認構造函數了。代碼以下,函數
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; } };
那麼咱們想要初始化變量,只要,
this
Person person ( "chentong", 20 );
這樣,只要調用一次構造函數就對全部成員進行了設置。指針
那麼,調用不帶參數的構造函數,以下:內存
Person person1;
這樣就是調用了不帶參數的構造函數而不能寫成這樣,it
Person person1();
由於,這樣寫的意義是,聲明一個函數。由於,這樣寫,就至關於,int fac(); //函數的聲明。順帶說一句,本身重寫構造函數後,必定要寫一個不帶參數的構造函數,不然編譯沒法經過。編譯
經過指針訪問class
Person* person1 = new Person; Person* person2 = new Person(); //Person1和Person2的做用徹底同樣,都會調用無參構造函數 Person* person3 = new Person [2]; Person* person4 = new Person ( "chentong", 20 );
釋放內存變量
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動態申請空間,來存放數據。C++中,new用來動態申請空間,delete用來釋放動態申請的空間。那麼C中的malloc()和C++中的new有什麼區別呢?它們都能用來動態的申請空間,malloc申請的空間,若是不去主動釋放,那麼被申請的空間將一直不會釋放,這樣就會形成內存的浪費。而new動態申請的空間,在程序執行時不會釋放,直到程序執行完畢,動態申請的空間纔會被釋放。若是想要在空間使用完畢後就釋放,只要經過delete就能夠作到了。
很明顯,咱們能夠把釋放空間的代碼寫成一個函數,當空間使用完畢後,直接調用釋放函數,空間就能夠被釋放了。若是動態申請的空間比較多,可能會出現遺漏釋放的狀況,因此,爲了不這種狀況出現,C++引入了析構函數這樣一個概念。當動態空間使用完畢後,析構函數會自動別調用,這樣以來,空間就被釋放了。析構函數和構造函數相似,都是與類名相同,只不過是,在類名以前加了一個波浪線~。順帶一提,構造函數和析構函數都沒有返回值類型。代碼以下:
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()就是析構函數。當程序執行完畢後,析構函數會自動被調用。動態申請的空間就被釋放了。