标签:ace album target 程序 str 创建 space targe url
(一)构造函数:用来在创建对象时初始化对象, 即为对象成员变量赋初始值
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 class Person{ 5 private: 6 char *name; 7 int age; 8 public: 9 10 Person(){ /*构造函数*/ 11 this->name = NULL; 12 this->age = NULL; 13 cout<<"Person()"<<endl; 14 } 15 Person(char *name,int age){ /*构造函数*/ 16 cout<<"Person(char *,int)"<<endl; 17 this->name = new char[strlen(name) + 1]; 18 strcpy(this->name,name); 19 this->age = age; 20 21 } 22 ~Person(){ 23 cout<<"~Person()"<<endl; 24 delete [] name; 25 } /*析构函数,当子程序运行结束时候系统自动调用*/ 26 void printInfo(void); 27 }; 28 void Person::printInfo(void) 29 { 30 cout<<"name = "<<name<<"age = "<<age<<endl; 31 } 32 int main(int argc,char ** argv) 33 { 34 Person per1; /*调用构造函数,打印无参时候的Person()*/ 35 Person per2("zhangsan",10);/*调用构造函数,打印Person(char *,int)*/ 36 per2.printInfo(); 37 return 0; 38 }
标签:ace album target 程序 str 创建 space targe url
原文地址:https://www.cnblogs.com/luxiaoguogege/p/9690186.html