C++基础知识_class是对struct的扩展
使用结构体:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 struct person 4 { 5 char *name; 6 int age; 7 char *work; 8 void (*printInfo)(struct person *per);//函数指针 9 }; 10 11 void printInfo(struct person *per) 12 { 13 printf("%s,%d,%s\n",per ->name,per ->age,per ->work); 14 } 15 16 17 int main(int argv,char **argc) 18 { 19 struct person persons[] = {{"zhangsan",10,"teacher",printInfo},{"lisi",20,"doctor",printInfo}}; 20 persons[0].printInfo(&persons[0]); 21 persons[1].printInfo(&persons[1]); 22 23 }
将结构体改为class类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class person 4 { 5 public: 6 char *name; 7 int age; 8 char *work; 9 void printInfo (void) 10 { 11 printf("%s,%d,%s\n",name,age,work); 12 } 13 }; 14 15 int main(int argv,char **argc) 16 { 17 struct person persons[] = {{"zhangsan",10,"teacher"},{"lisi",20,"doctor"}}; 18 persons[0].printInfo(); 19 persons[1].printInfo(); 20 21 }
访问控制:private, protected, public
private:用此保护的数据,只可以类内部的成员函数使用。
如果类外使用就会出现如下情况:
public:类外部的成员可以使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class person 4 { 5 private: 6 char *name; 7 int age; 8 char *work; 9 public: 10 void setName (char *n) 11 { 12 name = n; 13 } 14 int setAge(int a) 15 { 16 if (a<0||a>150) 17 { 18 age = 0; 19 return -1; 20 } 21 age = a; 22 return 0; 23 } 24 void printInfo(void) 25 { 26 printf("name = %s,age = %d,work = %s\n",name,age,work); 27 } 28 }; 29 30 int main(int argv,char **argc) 31 { 32 person per; 33 //per.name = "zhansan";//模拟类外使用 34 per.setName((char*)"zhangsan");//加上类型强制转换就不会报警告 35 per.setAge(10); 36 per.printInfo(); 37 return 0; 38 39 }
this指针:在类的成员函数中使用,表示当前对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class person 4 { 5 private: 6 char *name; 7 int age; 8 char *work; 9 public: 10 void setName (char *name) 11 { 12 this->name = name; 13 } 14 int setAge(int age) 15 { 16 if (age<0||age>150) 17 { 18 this ->age = 0; 19 return -1; 20 } 21 this->age = age; 22 return 0; 23 } 24 void printInfo(void) 25 { 26 printf("name = %s,age = %d,work = %s\n",name,age,work); 27 } 28 }; 29 30 int main(int argv,char **argc) 31 { 32 person per; 33 //per.name = "zhansan";//模拟类外使用 34 per.setName((char*)"zhangsan");//加上类型强制转换就不会报警告 35 per.setAge(10); 36 per.printInfo(); 37 return 0; 38 39 }
类和对象:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int a;//int 为类型,a为变量 2 person per;//person 为类,per为对象
类内声明函数类外实现:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class person 4 { 5 private: 6 char *name; 7 int age; 8 char *work; 9 public: 10 void setName(char *name); 11 int setAge(int age); 12 void printInfo(void); 13 14 };//类内定义函数类外声明 15 16 void person::setName (char *name) 17 { 18 this->name = name; 19 } 20 int person::setAge(int age) 21 { 22 if (age<0||age>150) 23 { 24 this ->age = 0; 25 return -1; 26 } 27 this->age = age; 28 return 0; 29 } 30 void person::printInfo(void) 31 { 32 printf("name = %s,age = %d,work = %s\n",name,age,work); 33 } 34 int main(int argv,char **argc) 35 { 36 person per; 37 //per.name = "zhansan";//模拟类外使用error 38 per.setName((char*)"zhangsan");//加上类型强制转换就不会报警告 39 per.setAge(10); 40 per.printInfo(); 41 return 0; 42 43 }
程序化结构:.h中声明函数等,.cpp中实现
.h中:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 class person 4 { 5 private: 6 char *name; 7 int age; 8 char *work; 9 public: 10 void setName(char *name); 11 int setAge(int age); 12 void printInfo(void); 13 14 };
.cpp中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include "C++day1_06.h" 3 void person::setName (char *name) 4 { 5 this->name = name; 6 } 7 int person::setAge(int age) 8 { 9 if (age<0||age>150) 10 { 11 this ->age = 0; 12 return -1; 13 } 14 this->age = age; 15 return 0; 16 } 17 void person::printInfo(void) 18 { 19 printf("name = %s,age = %d,work = %s\n",name,age,work); 20 }
main.cpp中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include "C++day1_06.h" 3 int main(int argv,char **argc) 4 { 5 person per; 6 //per.name = "zhansan";//模拟类外使用error 7 per.setName((char*)"zhangsan");//加上类型强制转换就不会报警告 8 per.setAge(10); 9 per.printInfo(); 10 return 0; 11 12 }
Makefile
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 day_06:main.o C++day1_06.o 2 g++ -o $@ $^ 3 %.o:%.cpp 4 g++ -c -o $@ $<注意一定要有-o 5 clean: 6 rm -f *.o day_06
使用命名空间:1.多个.h中有多个相同名字的函数的处理 2.程序结构
1.直接使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 .h中 2 #include <stdio.h> 3 namespace D{ 4 class Dog { 5 private: 6 char *name; 7 int age; 8 public: 9 void setName(char *name); 10 int setAge(int age); 11 void printInfo(void); 12 13 }; 14 void printVersion(void); 15 } 16 .cpp中 17 #include "dog.h" 18 //此为空间D 19 namespace D{ 20 void Dog::setName(char *name) 21 { 22 this ->name = name; 23 } 24 int Dog::setAge(int age) 25 { 26 this->age = age; 27 } 28 void Dog::printInfo(void) 29 { 30 printf("name = %s,age = %d\n",name,age); 31 } 32 void printVersion(void)//与person中的函数名字一样 33 { 34 printf("Dog v1,by hello\n"); 35 } 36 } 37 .h中 38 #include <stdio.h> 39 //此为空间A 40 namespace A { 41 42 class Person { 43 private: 44 char *name; 45 int age; 46 char *work; 47 48 public: 49 void setName(char *name); 50 int setAge(int age); 51 void printInfo(void); 52 }; 53 54 void printVersion(void); 55 } 56 .cpp中 57 #include "person.h" 58 59 namespace A { 60 61 void Person::setName(char *name) 62 { 63 this->name = name; 64 } 65 66 int Person::setAge(int age) 67 { 68 if (age < 0 || age > 150) 69 { 70 this->age = 0; 71 return -1; 72 } 73 this->age = age; 74 return 0; 75 } 76 77 void Person::printInfo(void) 78 { 79 printf("name = %s, age = %d, work = %s\n", name, age, work); 80 } 81 82 void printVersion(void) 83 { 84 printf("Person v1, by weidongshan\n"); 85 } 86 87 } 88 main中 89 #include "dog.h" 90 #include "person.h" 91 92 int main(int argc,char **argv) 93 { 94 A::Person per; 95 per.setName((char *)"zhangsan"); 96 per.setAge(16); 97 per.printInfo(); 98 99 D::Dog dog; 100 dog.setName((char *)"wangcai"); 101 dog.setAge(1); 102 dog.printInfo(); 103 104 A::printVersion(); 105 D::printVersion(); 106 return 0; 107 108 } 109 Makefile中 110 day_08:main.o person.o dog.o 111 g++ -o $@ $^ 112 %.o:%.cpp 113 g++ -c -o $@ $< 114 clean: 115 rm -f *.o day_08
2.using声明
与第一种只是main中的代码有所不同
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "dog.h" 2 #include "person.h" 3 4 /*global namespace*/ 5 6 /*把A::Person放入global namespace, 以后可以使用Person来表示A::Person */ 7 using A::Person; 8 9 /* 把C::Dog放入global namespace, 以后可以使用Dog来表示C::Dog */ 10 using D::Dog; 11 12 using A::printVersion; 13 using D::printVersion; 14 15 int main(int argc,char **argv) 16 { 17 Person per; 18 per.setName((char *)"zhangsan"); 19 per.setAge(16); 20 per.printInfo(); 21 22 Dog dog; 23 dog.setName((char *)"wangcai"); 24 dog.setAge(1); 25 dog.printInfo(); 26 27 A::printVersion(); 28 D::printVersion(); 29 return 0; 30 31 }
3.using编译
与第一种只是main中的代码有所不同
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "dog.h" 2 #include "person.h" 3 4 using namespace A;//可以直接使用空间A中的内容 5 6 using namespace D; 7 8 int main(int argc,char **argv) 9 { 10 Person per; 11 per.setName((char *)"zhangsan"); 12 per.setAge(16); 13 per.printInfo(); 14 15 Dog dog; 16 dog.setName((char *)"wangcai"); 17 dog.setAge(1); 18 dog.printInfo(); 19 20 A::printVersion();//空间A中有与其他空间相同的内容则需要使用空间名区分开 21 D::printVersion(); 22 return 0; 23 24 }
改变调用的库:使用cout ......endl替换printf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 .h 2 #include <iostream>//以此替换stdio.h库 3 namespace D{ 4 class Dog { 5 private: 6 char *name; 7 int age; 8 public: 9 void setName(char *name); 10 int setAge(int age); 11 void printInfo(void); 12 13 }; 14 void printVersion(void); 15 } 16 .cpp 17 #include "dog.h" 18 //此为空间D 19 namespace D{ 20 21 using namespace std;//使用第三种方法 22 void Dog::setName(char *name) 23 { 24 this ->name = name; 25 } 26 int Dog::setAge(int age) 27 { 28 this->age = age; 29 } 30 void Dog::printInfo(void) 31 { 32 cout<<"name = "<<name<<"age = "<<age<<endl; 33 } 34 void printVersion(void)//与person中的函数名字一样 35 { 36 cout<<"Dog v1,by hello"<<endl; 37 } 38 } 39 .h 40 #include <iostream> 41 42 namespace A { 43 44 class Person { 45 private: 46 char *name; 47 int age; 48 char *work; 49 50 public: 51 void setName(char *name); 52 int setAge(int age); 53 void printInfo(void); 54 }; 55 56 void printVersion(void); 57 } 58 .cpp 59 #include "person.h" 60 61 namespace A { 62 63 void Person::setName(char *name) 64 { 65 this->name = name; 66 } 67 68 int Person::setAge(int age) 69 { 70 if (age < 0 || age > 150) 71 { 72 this->age = 0; 73 return -1; 74 } 75 this->age = age; 76 return 0; 77 } 78 79 void Person::printInfo(void) 80 { 81 std::cout<<"name = "<<name<<" age = "<<age<<" work = "<<work<<std::endl; 82 } 83 84 void printVersion(void) 85 { 86 std::cout<<"Person v1, by weidongshan"<<std::endl; 87 } 88 89 } 90 main 91 #include "person.h" 92 #include "dog.h" 93 94 using namespace A; 95 using namespace C; 96 97 int main(int argc, char **argv) 98 { 99 /* local namespace */ 100 //using A::Person; 101 //using C::Dog; 102 103 Person per; 104 per.setName("zhangsan"); 105 per.setAge(16); 106 per.printInfo(); 107 108 Dog dog; 109 dog.setName("wangcai"); 110 dog.setAge(1); 111 dog.printInfo(); 112 113 A::printVersion(); 114 C::printVersion(); 115 return 0; 116 } 117 Makefile 118 person: main.o person.o dog.o 119 g++ -o $@ $^ 120 121 %.o : %.cpp 122 g++ -c -o $@ $< 123 124 clean: 125 rm -f *.o person
重载:函数名相同,参数不同(类型、数量、顺序不同)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 using namespace std; 3 4 int add (int a,int b) 5 { 6 cout<<"add int + int"<<endl; 7 return a+b; 8 } 9 10 int add(int a,int b,int c) 11 { 12 cout<<"add double + double"<<endl; 13 return a + b + c; 14 } 15 double add(double a,double b) 16 { 17 cout<<"add double + double"<<endl; 18 return a + b; 19 } 20 double add(int a, double b) 21 { 22 cout << "add int + double"<<endl; 23 return (double)a + b; 24 } 25 double add (double a,int b) 26 { 27 cout<<"add double + int"<<endl; 28 return (double)a + b; 29 } 30 int main(int argc,char **argv) 31 { 32 add(1,2); 33 add(1,2,3); 34 add(1.0,2.0); 35 add(1,2.0); 36 add(1.0,2); 37 return 0; 38 }
指针和引用:引用一定要初始化(引用相当是给变量起了一个别名,故变量改变引用的值也就改变了)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream>//指针与引用是相同的 2 3 using namespace std; 4 5 int add_one(int a) 6 { 7 a = a+1; 8 return a; 9 } 10 11 int add_one(int *a) 12 { 13 *a = *a +1; 14 return *a; 15 } 16 int add_one_ref(int &b) 17 { 18 b = b + 1; 19 return b; 20 } 21 int main(int argc,char **argv) 22 { 23 int a = 99; 24 int &c =a; 25 cout<<add_one(a)<<endl; 26 cout<<"a = "<<a<<endl; 27 28 cout <<add_one(&a)<<endl; 29 cout<<"a ="<<a<<endl; 30 31 c++; 32 cout<<"a = "<<a<<endl; 33 cout<<"c = "<<c<<endl; 34 }
构造函数:
无返回值,函数名与类名相同,一旦自己实现了带参的构造函数,一定要自己写出不带参的构造函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 3 using namespace std; 4 5 class person { 6 private: 7 char *name; 8 int age; 9 char *work; 10 public: 11 person()//构造函数 12 { 13 cout<<"person()"<<endl; 14 } 15 person(char *name) 16 { 17 cout<<"person(char *)"<<endl; 18 this->name = name; 19 } 20 person(char *name,int age) 21 { 22 cout<<"person(char*,int)"<<endl; 23 this->name = name; 24 this->age = age; 25 } 26 void setName(char *n) 27 { 28 name = n; 29 } 30 int setAge(int a) 31 { 32 if (a < 0||a > 150) 33 { 34 age = a; 35 return -1; 36 } 37 age =a; 38 return 0; 39 } 40 void printInfo(void) 41 { 42 cout<<"name = "<<name<<",age = "<<age<<",work = "<<work<<endl; 43 } 44 45 }; 46 int main (int atgc,char **argv) 47 { 48 person per((char *)"zhangsan",16); 49 person per2; 50 51 per.printInfo(); 52 return 0; 53 }
无参对象定义: Person p; 不能写为 Person p(); //后者是一个函数的定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 3 using namespace std; 4 5 class person { 6 private: 7 char *name; 8 int age; 9 char *work; 10 public: 11 person()//构造函数 12 { 13 cout<<"person()"<<endl; 14 } 15 person(char *name) 16 { 17 cout<<"person(char *)"<<endl; 18 this->name = name; 19 } 20 person(char *name,int age) 21 { 22 cout<<"person(char*,int)"<<endl; 23 this->name = name; 24 this->age = age; 25 } 26 void setName(char *n) 27 { 28 name = n; 29 } 30 int setAge(int a) 31 { 32 if (a < 0||a > 150) 33 { 34 age = a; 35 return -1; 36 } 37 age =a; 38 return 0; 39 } 40 void printInfo(void) 41 { 42 cout<<"name = "<<name<<",age = "<<age<<",work = "<<work<<endl; 43 } 44 45 }; 46 int main (int atgc,char **argv) 47 { 48 person per((char *)"zhangsan",16); 49 person per2;//调用无参构造函数 50 person per3();//相当于是定义了一个函数,不是调用无参构造函数 51 52 per.printInfo(); 53 return 0; 54 }
这样代码的运行结果与上图一样
拷贝构造函数:例如:有class A {function f();}对象p1,p2
http://blog.csdn.net/lwbeyond/article/details/6202256详细的讲述了拷贝构造函数
注意:拷贝构造函数容易出现对一块空间的多次释放
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 //cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 //cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 //cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 } 38 ~person() 39 { 40 cout<<"~person()"<<endl; 41 if(this->name) 42 { 43 cout<<"name ="<<name<<endl; 44 delete this->name; 45 } 46 if(this->work) 47 { 48 cout<<"work ="<<work<<endl; 49 delete this->work; 50 } 51 } 52 void setName(char *n) 53 { 54 name = n; 55 } 56 int setAge(int a) 57 { 58 if (a < 0 || a > 150) 59 { 60 age = 0; 61 return -1; 62 } 63 age = a; 64 return 0; 65 } 66 void printInfo(void) 67 { 68 //printf("name = %s, age = %d, work = %s\n", name, age, work); 69 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 70 } 71 72 }; 73 74 int main(int argc,char **argv) 75 { 76 person per("zhangsan",18); 77 person per2(per);//拷贝构造函数 78 79 per2.printInfo(); 80 return 0; 81 }
自己实现的拷贝构造函数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 //cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 //cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 //cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 } 38 person(person &per)//自己实现的拷贝构造函数 39 { 40 cout<<"person(person &)"<<endl; 41 this->age = per.age; 42 43 this->name = new char(strlen(per.name)+1); 44 strcpy(this->name,per.name); 45 46 this->work = new char[strlen(per.work) + 1]; 47 strcpy(this->work, per.work); 48 } 49 ~person() 50 { 51 cout<<"~person()"<<endl; 52 if(this->name) 53 { 54 cout<<"name ="<<name<<endl; 55 delete this->name; 56 } 57 if(this->work) 58 { 59 cout<<"work ="<<work<<endl; 60 delete this->work; 61 } 62 } 63 void setName(char *n) 64 { 65 name = n; 66 } 67 int setAge(int a) 68 { 69 if (a < 0 || a > 150) 70 { 71 age = 0; 72 return -1; 73 } 74 age = a; 75 return 0; 76 } 77 void printInfo(void) 78 { 79 //printf("name = %s, age = %d, work = %s\n", name, age, work); 80 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 81 } 82 83 }; 84 85 int main(int argc,char **argv) 86 { 87 person per("zhangsan",18); 88 person per2(per); 89 90 per2.printInfo(); 91 return 0; 92 }
构造顺序:1.按运行中定义对象的顺序调用构造函数,静态对象只调用一次构造函数; 全局对象在main函数执行前被构造
2.类中含有对象成员时,先调用对象成员的默认构造函数,再调用类的构造函数,对象成员:按定义时的顺序构造
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 cout<<"name ="<<name<<endl; 38 cout<<"work ="<<work<<endl; 39 40 } 41 person(person &per) 42 { 43 cout<<"person(person &)"<<endl; 44 this->age = per.age; 45 46 this->name = new char(strlen(per.name)+1); 47 strcpy(this->name,per.name); 48 49 this->work = new char[strlen(per.work) + 1]; 50 strcpy(this->work, per.work); 51 } 52 ~person() 53 { 54 //cout<<"~person()"<<endl; 55 if(this->name) 56 { 57 //cout<<"name ="<<name<<endl; 58 delete this->name; 59 } 60 if(this->work) 61 { 62 //cout<<"work ="<<work<<endl; 63 delete this->work; 64 } 65 } 66 void setName(char *n) 67 { 68 name = n; 69 } 70 int setAge(int a) 71 { 72 if (a < 0 || a > 150) 73 { 74 age = 0; 75 return -1; 76 } 77 age = a; 78 return 0; 79 } 80 void printInfo(void) 81 { 82 //printf("name = %s, age = %d, work = %s\n", name, age, work); 83 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 84 } 85 86 }; 87 person per_g("per_g",10); 88 89 void func() 90 { 91 person per_func("per_func",11); 92 static person per_func_s("per_func_s",10); 93 } 94 95 int main(int argc,char **argv) 96 { 97 person per_main("per_main",11); 98 static person per_main_s("per_main_s",11); 99 100 for(int i = 0;i<2;i++) 101 { 102 func(); 103 person per_for("per_for",1); 104 } 105 return 0; 106 }
2.对应的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 cout<<"name ="<<name<<endl; 38 cout<<"work ="<<work<<endl; 39 40 } 41 person(person &per) 42 { 43 cout<<"person(person &)"<<endl; 44 this->age = per.age; 45 46 this->name = new char(strlen(per.name)+1); 47 strcpy(this->name,per.name); 48 49 this->work = new char[strlen(per.work) + 1]; 50 strcpy(this->work, per.work); 51 } 52 ~person() 53 { 54 //cout<<"~person()"<<endl; 55 if(this->name) 56 { 57 //cout<<"name ="<<name<<endl; 58 delete this->name; 59 } 60 if(this->work) 61 { 62 //cout<<"work ="<<work<<endl; 63 delete this->work; 64 } 65 } 66 void setName(char *n) 67 { 68 name = n; 69 } 70 int setAge(int a) 71 { 72 if (a < 0 || a > 150) 73 { 74 age = 0; 75 return -1; 76 } 77 age = a; 78 return 0; 79 } 80 void printInfo(void) 81 { 82 //printf("name = %s, age = %d, work = %s\n", name, age, work); 83 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 84 } 85 86 }; 87 class Student{ 88 private: 89 person father; 90 person mother; 91 int student_id; 92 public: 93 Student() 94 { 95 cout<<"Student()"<<endl; 96 } 97 98 Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39) : mother(mother, mother_age), father(father, father_age)//调用father和mother中的构造函数,要调用对象成员的其他构造函数,可以这样写: Student(int sID) : id(sID) {} 99 构造函数的"{}"前加":",加上成员的初始化代码 100 { 101 cout<<"Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)"<<endl; 102 } 103 104 ~Student() 105 { 106 cout<<"~Student()"<<endl; 107 } 108 109 110 }; 111 112 int main(int argc,char **argv) 113 { 114 Student s(100,"bill","lily"); 115 return 0; 116 }
new和delete:有new就会有delete,而且new出来的对象只有用delete删除时才会调用相应的析构函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 3 using namespace std; 4 5 class person { 6 private: 7 char *name; 8 int age; 9 char *work; 10 public: 11 person()//构造函数 12 { 13 cout<<"person()"<<endl; 14 } 15 person(char *name) 16 { 17 cout<<"person(char *)"<<endl; 18 this->name = name; 19 } 20 person(char *name,int age,char *work =(char *)"none") 21 { 22 cout<<"person(char*,int)"<<endl; 23 this->name = name; 24 this->age = age; 25 this->work = work; 26 } 27 void setName(char *n) 28 { 29 name = n; 30 } 31 int setAge(int a) 32 { 33 if (a < 0||a > 150) 34 { 35 age = 0; 36 return -1; 37 } 38 age =a; 39 return 0; 40 } 41 void printInfo(void) 42 { 43 cout<<"name = "<<name<<",age = "<<age<<",work = "<<work<<endl; 44 } 45 46 }; 47 int main (int atgc,char **argv) 48 { 49 person per((char*)"zhangsan",16); 50 person per2;//调用无参构造函数 51 //person per3();//相当于是定义了一个函数,不是调用无参构造函数 52 53 //另一种实例化对象的方法 54 person *per4 = new person;//调用无参的构造函数 55 person *per5 = new person();//调用无参的构造函数 56 57 person *per6 = new person[2];//调用两次无参构造函数 58 59 person *per7 = new person("lisi",18,"student"); 60 person *per8 = new person("wangwu",18); 61 62 per.printInfo(); 63 per7->printInfo(); 64 per8->printInfo(); 65 66 delete per4; 67 delete per5; 68 delete []per6;//数组的delete方法 69 delete per7; 70 delete per8; 71 return 0; 72 }
析构函数:无返回值,无参数,函数形式为"~类名()",对象销毁前瞬间,由系统自动调用析构函数
最好自己有一个析构函数,不要等系统自己去调用默认的析构函数
以下为无自己构造的析构函数:在函数运行期间会一直占用内存
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 //cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 //cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 //cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 } 38 void setName(char *n) 39 { 40 name = n; 41 } 42 int setAge(int a) 43 { 44 if (a < 0 || a > 150) 45 { 46 age = 0; 47 return -1; 48 } 49 age = a; 50 return 0; 51 } 52 void printInfo(void) 53 { 54 //printf("name = %s, age = %d, work = %s\n", name, age, work); 55 //cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 56 } 57 58 }; 59 void test_fun() 60 { 61 person per("zhangsan", 16); 62 person per2; /* 调用无参构造函数 */ 63 person per3(); /* int fun(); */ 64 65 person *per4 = new person; 66 person *per5 = new person(); 67 68 person *per6 = new person[2]; 69 70 person *per7 = new person("lisi", 18, "student"); 71 person *per8 = new person("wangwu", 18); 72 73 per.printInfo(); 74 per7->printInfo(); 75 per8->printInfo(); 76 77 delete per4; 78 delete per5; 79 delete []per6; 80 delete per7; 81 delete per8; 82 83 } 84 85 int main(int argc,char **argv) 86 { 87 for(int i = 0;i < 1000000;i++) 88 { 89 test_fun(); 90 91 } 92 cout<<"run test_fun end"<<endl; 93 sleep(10); 94 return 0; 95 }
有自己构造的析构函数:在创建的对象销毁的时候就会自动释放空间,而不是等程序运行完
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 char *name; 10 int age; 11 char *work; 12 public: 13 person() 14 { 15 //cout <<"Pserson()"<<endl; 16 this ->name = NULL; 17 this ->work = NULL; 18 } 19 20 person(char *name) 21 { 22 //cout<<"person(char *)"<<endl; 23 this ->name = new char(strlen(name)+1); 24 strcpy(this->name,name); 25 this->work = NULL; 26 } 27 person(char *name, int age, char *work = "none") 28 { 29 //cout <<"Pserson(char*, int)"<<endl; 30 this->age = age; 31 32 this->name = new char[strlen(name) + 1]; 33 strcpy(this->name, name); 34 35 this->work = new char[strlen(work) + 1]; 36 strcpy(this->work, work); 37 } 38 ~person()//增加了之一个函数 39 { 40 if(this->name) 41 { 42 delete this->name; 43 } 44 if(this->work) 45 { 46 delete this->work; 47 } 48 } 49 void setName(char *n) 50 { 51 name = n; 52 } 53 int setAge(int a) 54 { 55 if (a < 0 || a > 150) 56 { 57 age = 0; 58 return -1; 59 } 60 age = a; 61 return 0; 62 } 63 void printInfo(void) 64 { 65 //printf("name = %s, age = %d, work = %s\n", name, age, work); 66 //cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 67 } 68 69 }; 70 void test_fun() 71 { 72 person per("zhangsan", 16); 73 person per2; /* 调用无参构造函数 */ 74 person per3(); /* int fun(); */ 75 76 person *per4 = new person; 77 person *per5 = new person(); 78 79 person *per6 = new person[2]; 80 81 person *per7 = new person("lisi", 18, "student"); 82 person *per8 = new person("wangwu", 18); 83 84 per.printInfo(); 85 per7->printInfo(); 86 per8->printInfo(); 87 88 delete per4; 89 delete per5; 90 delete []per6; 91 delete per7; 92 delete per8; 93 94 } 95 96 int main(int argc,char **argv) 97 { 98 for(int i = 0;i < 1000000;i++) 99 { 100 test_fun(); 101 102 } 103 cout<<"run test_fun end"<<endl; 104 sleep(10); 105 return 0; 106 }
静态成员:1.类中定义,类外分配空间和初始化,初始化时不加static,
2.函数可以在类内实现或类内声明类外实现
3.static 成员不可以访问非static成员
4.用static修饰的变量(函数)属于整个类,不管这个类实例化出多少个对象,这几个对象共用static修饰的变量(函数)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class person { 8 private: 9 static int cnt; 10 char *name; 11 int age; 12 char *work; 13 public: 14 15 static int getCount(void); 16 person() 17 { 18 cout <<"Pserson()"<<endl; 19 this ->name = NULL; 20 this ->work = NULL; 21 cnt++; 22 } 23 24 person(char *name) 25 { 26 cout<<"person(char *)"<<endl; 27 this ->name = new char(strlen(name)+1); 28 strcpy(this->name,name); 29 this->work = NULL; 30 cnt++; 31 } 32 person(char *name, int age, char *work = "none") 33 { 34 cout <<"Pserson(char*, int)"<<endl; 35 this->age = age; 36 37 this->name = new char[strlen(name) + 1]; 38 strcpy(this->name, name); 39 40 this->work = new char[strlen(work) + 1]; 41 strcpy(this->work, work); 42 cout<<"name ="<<name<<endl; 43 cout<<"work ="<<work<<endl; 44 cnt++; 45 46 } 47 person(person &per) 48 { 49 cout<<"person(person &)"<<endl; 50 this->age = per.age; 51 52 this->name = new char(strlen(per.name)+1); 53 strcpy(this->name,per.name); 54 55 this->work = new char[strlen(per.work) + 1]; 56 strcpy(this->work, per.work); 57 cnt++; 58 } 59 ~person() 60 { 61 //cout<<"~person()"<<endl; 62 if(this->name) 63 { 64 //cout<<"name ="<<name<<endl; 65 delete this->name; 66 } 67 if(this->work) 68 { 69 //cout<<"work ="<<work<<endl; 70 delete this->work; 71 } 72 } 73 void setName(char *n) 74 { 75 name = n; 76 } 77 int setAge(int a) 78 { 79 if (a < 0 || a > 150) 80 { 81 age = 0; 82 return -1; 83 } 84 age = a; 85 return 0; 86 } 87 void printInfo(void) 88 { 89 //printf("name = %s, age = %d, work = %s\n", name, age, work); 90 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 91 } 92 93 }; 94 95 96 int person::cnt = 0; 97 int person::getCount(void) 98 { 99 return cnt; 100 } 101 102 int main(int argc,char **argv) 103 { 104 person p[100]; 105 cout<<"person number = "<<person::getCount()<<endl; 106 cout<<"person number = "<<p[0].getCount()<<endl; 107 cout<<"person number = "<<p[1].getCount()<<endl; 108 return 0; 109 }
友员:1.在类中声明非成员函数时加上friend,它即可访问类的私有数据成员
2.一个类的成员函数也可以是另一个类的友元
class A {
private :
int a;
public:
friend void fun1(); // fun1不是A的成员函数也可以访问a
frined void B::fun2(); // B类的fun2也可以访问a
}
class{
private:
int x;
int y;
public:
person(int x,int y):x(x),y(y) 意思是x 等于参数x,y等于参数y
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class point{ 8 private: 9 int x; 10 int y; 11 public: 12 point(){} 13 point(int x,int y):x(x),y(y){} 14 15 int getx(){return x;} 16 int gety(){return y;} 17 void setx(int x){this->x = x;} 18 void sety(int y){this ->y = y;} 19 void printInfo() 20 { 21 cout<<"("<<x<<","<<y<<")"<<endl; 22 } 23 friend point add(point &p1,point &p2);//声明友员 24 25 }; 26 point add(point &p1,point &p2) 27 { 28 point n; 29 //n.setx(p1.getx() + p2.getx());//声明友员后就可以直接使用x和y 30 //n.sety(p1.gety()+p2.gety()); 31 32 n.x = p1.x + p2.x; 33 n.y = p1.y + p2.y; 34 return n; 35 } 36 37 int main(int argc,char **argv) 38 { 39 point p1(1,2); 40 point p2(2,3); 41 42 point sum = add(p1,p2); 43 sum.printInfo(); 44 45 return 0; 46 }
运算符的重载:
在类外面实现
+的重载:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class point{ 8 private: 9 int x; 10 int y; 11 public: 12 point(){} 13 point(int x,int y):x(x),y(y){} 14 15 int getX(){ return x; } 16 int getY(){ return y; } 17 void setX(int x){ this->x = x; } 18 void setY(int y){ this->y = y; } 19 void printInfo() 20 { 21 cout<<"("<<x<<", "<<y<<")"<<endl; 22 } 23 friend point add(point &p1, point &p2); 24 friend point operator+(point &p1, point &p2);//声明为友员,才可以直接使用类的私有成员 25 }; 26 27 point add(point &p1,point &p2) 28 { 29 point n; 30 n.x = p1.x + p2.x; 31 n.y = p1.y + p2.y; 32 return n; 33 } 34 point operator+(point &p1, point &p2)//相当于是把p1和p2自身传递过来 35 { 36 cout<<"point operator+(point &p1, point &p2)"<<endl; 37 point n; 38 n.x = p1.x+p2.x; 39 n.y = p1.y+p2.y; 40 return n; 41 } 42 int main(int argc,char**argv) 43 { 44 point p1(1,2); 45 point p2(2,3); 46 47 point sum = p1 + p2; 48 sum.printInfo(); 49 50 return 0; 51 }
返回结果:1.值返回:
返回函数内部定义的局部变量;
该变量在函数执行时被创建,执行完毕时被销毁;
只返回值;
效率低
2.引用返回:
3.选用原则:
不影响运算结果
效率优先
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class point{ 8 private: 9 int x; 10 int y; 11 public: 12 point() 13 { 14 cout<<"Point()"<<endl; 15 } 16 point(int x,int y):x(x),y(y) 17 { 18 cout<<"Point(int x, int y)"<<endl; 19 } 20 21 point (const point& p) 22 { 23 cout<<"Point(const Point& p)"<<endl; 24 x = p.x; 25 y = p.y; 26 } 27 28 ~point() 29 { 30 cout<<"~Point()"<<endl; 31 } 32 33 int getX(){ return x; } 34 int getY(){ return y; } 35 void setX(int x){ this->x = x; } 36 void setY(int y){ this->y = y; } 37 void printInfo() 38 { 39 cout<<"("<<x<<", "<<y<<")"<<endl; 40 } 41 42 friend point add(point &p1, point &p2); 43 friend point operator+(point &p1, point &p2);//声明为友员,才可以直接使用类的私有成员 44 friend point operator++(point &p); 45 friend point operator++(point &p, int a); 46 }; 47 48 point add(point &p1,point &p2) 49 { 50 point n; 51 n.x = p1.x + p2.x; 52 n.y = p1.y + p2.y; 53 return n; 54 } 55 point operator+(point &p1, point &p2)//相当于是把p1和p2自身传递过来 56 { 57 cout<<"point operator+(point &p1, point &p2)"<<endl; 58 point n; 59 n.x = p1.x+p2.x; 60 n.y = p1.y+p2.y; 61 return n; 62 } 63 64 65 point operator++(point &p) 66 { 67 cout<<"++p"<<endl; 68 p.x += 1; 69 p.y += 1; 70 return p;//返回的是一个point型的临时对象,所以会调用point (const point& p)这个析构函数 71 } 72 73 /* point p(1,2); p++; */ 74 point operator++(point &p, int a) 75 { 76 cout<<"p++"<<endl; 77 point n; 78 n = p; 79 p.x += 1; 80 p.y += 1; 81 return n; 82 } 83 84 int main(int argc,char**argv) 85 { 86 point p1(2,3); 87 88 cout<<"begin"<<endl; 89 ++p1; 90 cout<<"****************"<<endl; 91 92 p1++; 93 94 cout<<"end"<<endl; 95 96 return 0; 97 }
运行结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class point{ 8 private: 9 int x; 10 int y; 11 public: 12 point() 13 { 14 cout<<"point()"<<endl; 15 } 16 point(int x,int y):x(x),y(y) 17 { 18 cout<<"point(int x, int y)"<<endl; 19 } 20 21 point (const point& p) 22 { 23 cout<<"point(const point& p)"<<endl; 24 x = p.x; 25 y = p.y; 26 } 27 28 ~point() 29 { 30 cout<<"~point()"<<endl; 31 } 32 33 int getX(){ return x; } 34 int getY(){ return y; } 35 void setX(int x){ this->x = x; } 36 void setY(int y){ this->y = y; } 37 void printInfo() 38 { 39 cout<<"("<<x<<", "<<y<<")"<<endl; 40 } 41 42 friend point add(point &p1, point &p2); 43 friend point operator+(point &p1, point &p2);//声明为友员,才可以直接使用类的私有成员 44 friend point&operator++(point &p); 45 friend point operator++(point &p, int a); 46 }; 47 48 point add(point &p1,point &p2) 49 { 50 point n; 51 n.x = p1.x + p2.x; 52 n.y = p1.y + p2.y; 53 return n; 54 } 55 point operator+(point &p1, point &p2)//相当于是把p1和p2自身传递过来 56 { 57 cout<<"point operator+(point &p1, point &p2)"<<endl; 58 point n; 59 n.x = p1.x+p2.x; 60 n.y = p1.y+p2.y; 61 return n; 62 } 63 64 65 point& operator++(point &p) 66 { 67 cout<<"++p"<<endl; 68 p.x += 1; 69 p.y += 1; 70 return p;//返回的是引用,即传进来的对象的引用,故不会创建临时对象,所以也就不会调用相应的析构函数 71 } 72 /* point p(1,2); p++; */ 73 point operator++(point &p, int a) 74 { 75 cout<<"p++"<<endl; 76 point n; 77 n = p; 78 p.x += 1; 79 p.y += 1; 80 return n; 81 } 82 83 int main(int argc, char **argv) 84 { 85 point p1(1, 2); 86 87 cout<<"begin"<<endl; 88 ++p1; 89 cout << "******************"<<endl; 90 91 p1++; 92 cout<<"end"<<endl; 93 94 95 return 0; 96 }
在类的内部实现:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class point{ 8 private: 9 int x; 10 int y; 11 public: 12 point() 13 { 14 cout<<"point()"<<endl; 15 } 16 point(int x,int y):x(x),y(y)//等同于this->x = x,this -> y = y 17 { 18 cout<<"point(int x, int y)"<<endl; 19 } 20 21 point (const point& p) 22 { 23 cout<<"point(const point& p)"<<endl; 24 x = p.x; 25 y = p.y; 26 } 27 28 ~point() 29 { 30 cout<<"~point()"<<endl; 31 } 32 33 int getX(){ return x; } 34 int getY(){ return y; } 35 void setX(int x){ this->x = x; } 36 void setY(int y){ this->y = y; } 37 void printInfo() 38 { 39 cout<<"("<<x<<", "<<y<<")"<<endl; 40 } 41 42 //point operator+(point &p1, point &p2) 43 point operator+(point &p)//与在外部的情况相比少了一个参数 44 { 45 cout<<"point operator+"<<endl; 46 point n; 47 n.x = this->x+p.x; 48 n.y = this->y+p.y; 49 return n; 50 } 51 52 53 point& operator++(void) 54 { 55 cout<<"++p"<<endl; 56 this->x += 1; 57 this->y += 1; 58 return *this; 59 } 60 /* point p(1,2); p++; */ 61 point operator++(int a) 62 { 63 cout<<"p++"<<endl; 64 point n; 65 n = *this; 66 this->x += 1; 67 this->y += 1; 68 return n; 69 } 70 71 friend point add(point &p1, point &p2); 72 friend ostream& operator<<(ostream &o, point p); 73 }; 74 75 point add(point &p1,point &p2) 76 { 77 point n; 78 n.x = p1.x + p2.x; 79 n.y = p1.y + p2.y; 80 return n; 81 } 82 83 ostream& operator<<(ostream &o, point p) 84 { 85 cout<<"("<<p.x<<", "<<p.y<<")"; 86 return o; 87 } 88 89 90 int main(int argc, char **argv) 91 { 92 point p1(1, 2); 93 point p2(2, 3); 94 point m,n; 95 m = p1 + p2; /* m = p1.operator+(p2); */ 96 cout<<"add p1,p2 = "<<m<<endl; 97 98 cout<<"begin"<<endl; 99 m = ++p1; /* m = p1.operator++(); */ 100 cout<<"m = "<<m<<" "<<"p1 = "<<p1<<endl; 101 cout << "******************"<<endl; 102 103 n = p1++; /* m = p1.operator++(0); */ 104 cout<<"n = "<<n<<" "<<"p1 = "<<p1<<endl; 105 cout<<"end"<<endl; 106 107 108 return 0; 109 }
const成员函数:
1.成员函数声明后面,加上const
void printInfo(void) const;
表示此函数没有修改操作(即为const成员函数)
2.const对象只能调用const成员函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <unistd.h> 4 5 using namespace std; 6 7 class Person { 8 private: 9 static int cnt; 10 char *name; 11 int age; 12 char *work; 13 14 public: 15 16 static int getCount(void); 17 18 Person() {//cout <<"Pserson()"<<endl; 19 name = NULL; 20 work = NULL; 21 cnt++; 22 } 23 Person(char *name) 24 { 25 //cout <<"Pserson(char *)"<<endl; 26 this->name = new char[strlen(name) + 1]; 27 strcpy(this->name, name); 28 this->work = NULL; 29 cnt++; 30 } 31 32 Person(char *name, int age, char *work = "none") 33 { 34 cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl; 35 this->age = age; 36 37 this->name = new char[strlen(name) + 1]; 38 strcpy(this->name, name); 39 40 this->work = new char[strlen(work) + 1]; 41 strcpy(this->work, work); 42 cnt++; 43 } 44 45 Person(const Person &per) 46 { 47 cout <<"Pserson(Person &)"<<endl; 48 this->age = per.age; 49 50 this->name = new char[strlen(per.name) + 1]; 51 strcpy(this->name, per.name); 52 53 this->work = new char[strlen(per.work) + 1]; 54 strcpy(this->work, per.work); 55 cnt++; 56 } 57 58 ~Person() 59 { 60 cout << "~Person()"<<endl; 61 if (this->name) { 62 cout << "name = "<<name<<endl; 63 delete this->name; 64 } 65 if (this->work) { 66 cout << "work = "<<work<<endl; 67 delete this->work; 68 } 69 } 70 71 void setName(char *n) 72 { 73 name = n; 74 } 75 int setAge(int a) 76 { 77 if (a < 0 || a > 150) 78 { 79 age = 0; 80 return -1; 81 } 82 age = a; 83 return 0; 84 } 85 void printInfo(void) const 86 { 87 //printf("name = %s, age = %d, work = %s\n", name, age, work); 88 cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl; 89 } 90 91 Person& operator=(const Person& p) 92 { 93 cout << "operator=(const Person& p)"<<endl; 94 95 if (this == &p) 96 return *this; 97 this->age = p.age; 98 99 if (this->name) { 100 delete this->name; 101 } 102 if (this->work) { 103 delete this->work; 104 } 105 106 this->name = new char[strlen(p.name) + 1]; 107 strcpy(this->name, p.name); 108 109 this->work = new char[strlen(p.work) + 1]; 110 strcpy(this->work, p.work); 111 112 return *this; 113 114 } 115 116 }; 117 118 int Person::cnt = 0; /* 定义和初始化 */ 119 120 int Person::getCount(void) 121 { 122 return cnt; 123 } 124 125 126 int main(int argc, char **argv) 127 { 128 const Person p1("zhangsan", 10);//const对象只能调用const成员函数(析构函数除外) 129 130 cout<<"Person p2 = p1" <<endl; 131 Person p2 = p1;//此为拷贝构造函数 132 133 Person p3; 134 135 cout<<"p3=p1"<<endl; 136 p3 = p1;//这种情况才会调用本工程中的=号的重载 137 cout<<"end"<<endl; 138 p1.printInfo();//const对象只能调用const成员函数 139 p2.printInfo(); 140 p3.printInfo(); 141 142 143 144 return 0; 145 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
work