1. 程式人生 > >C++構造函數和析構函數

C++構造函數和析構函數

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;
}

};
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;
}
};

那麽我們想要初始化變量,只要,

Person person ( "chentong", 20 );

這樣,只要調用一次構造函數就對所有成員進行了設置。

那麽,調用不帶參數的構造函數,如下:

Person person1;

這樣就是調用了不帶參數的構造函數而不能寫成這樣,

Person person1();

因為,這樣寫的意義是,聲明一個函數。因為,這樣寫,就相當於,int fac(); //函數的聲明。順帶說一句,自己重寫構造函數後,一定要寫一個不帶參數的構造函數,否則編譯無法通過。

通過指針訪問

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()就是析構函數。當程序執行完畢後,析構函數會自動被調用。動態申請的空間就被釋放了。

本文出自 “梵高說我腦子有病” 博客,請務必保留此出處http://chen0547.blog.51cto.com/12489941/1978275

C++構造函數和析構函數