有这么一段代码,
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()就是析构函数。当程序执行完毕后,析构函数会自动被调用。动态申请的空间就被释放了。