∥例8.12使用深拷贝,复制对象不会有运行错误 #include<iostream> using namespace std; class student public student(int, char,k, int, float) student(student & -studento oid printstu 0: private int id char* name int age; float score
1 //例8.12 使用深拷贝,复制对象不会有运行错误 #include<iostream> using namespace std; class student {public: student(int,char*,int,float); student(student&); ~student(); void printstu(); private: int id; char* name; int age; float score; };
student: student (int i, char*c, int a, float s) ∥构造函数 I cout<<"Constructing. "<<endl; age =a, score =s name new char[strlen(c)+1 ∥申请堆空间 if(name =0) strcpy(name, c) student: student (student& s) ∥拷贝构造函数 i cout<< Copy Constructing. "<<endl ∥一般成员简单复制 age=sage score=s score name= new char[strlen(s name)+11 ∥,先申请堆空间 if(name I =O) strcpy(name, sname) ∥析构函数
2 student::student(int i,char* c,int a,float s) //构造函数 { cout<<“Constructing…”<<endl; id =i; age =a; score =s; name = new char[strlen(c) +1]; //申请堆空间 if(name !=0) strcpy(name,c); } student::student(student& s) //拷贝构造函数 { cout<<“Copy Constructing…”<<endl; id=s.id; //一般成员简单复制 age=s.age; score=s.score; name = new char[strlen(s.name) +1]; //先申请堆空间 if(name !=0) strcpy(name,s.name); //析构函数 }
student: - studento i cout<<"Destructing.. <<endl; name0]="10; delete name void student: printstuo cout<“学号:”<<d<<“姓名:"<<name; cout<“年龄<age<<“成绩:"<< score≤<endl void maino i student s1(10, Wang,18, 86); ∥)创建和初始化对象 student s2=s 1 ∥调用深拷贝的拷贝构造 函数 s1. printstuo S2. printstuO
3 student::~ student() { cout<<“Destructing…”<<endl; name[0]=‘\0’; delete name; } void student::printstu() { cout<<“学号:”<<id<<“姓名:”<<name; cout<<“年龄:”<<age<<“成绩:”<<score<<endl; } void main() { student s1(10, “ Wang ”,18,86); //创建和初始化对象 student s2=s1; //调用深拷贝的拷贝构造 函数 s1. printstu(); s2. printstu(); }
∥例813静态数据成员的定义和使用 #include<iostream> #include<string> using namespace std class student ipublic Student(char* pName="no name") -Studento void PrintcO i cout<< The number of student is"<<count<<endl private static int count;∥若写成 count=0;则非法 char name [20]
4 //例8.13 静态数据成员的定义和使用 #include<iostream> #include<string> using namespace std; class Student {public: Student(char* pName=“no name”); ~Student(); void PrintC() { cout<<“The number of student is”<<count<<endl; } private: static int count; //若写成count=0;则非法 char name[20]; };
Student: Student(char* pName)//="no name") i cout<< create one studentin strncpy(name, pName, 20) name[19]=0 count++. 静态成员:每创建一个对象,学生人数 增1 Student: -Studento i cout<< destruct one student\n cout ∥/每析构一个对象,学生人数减1 cout< <The number of student is "<<count<<endl int Student: count=0 ∥静态数据成员在类外分配空间和初始化 void maino Student s 1 s1. Printco Student s2 s2. Printco
5 Student::Student(char* pName)//=“no name”) { cout<<“create one student\n”; strncpy(name,pName,20); name[19]=‘\0’; count++; //静态成员:每创建一个对象,学生人数 增1 } Student::~Student() { cout<<“destruct one student\n”; cout--; //每析构一个对象,学生人数减1 cout<<“The number of student is”<<count<<endl; } int Student::count=0; //静态数据成员在类外分配空间和初始化 void main() { Student s1; s1.PrintC(); Student s2; s2.PrintC(); }