∥例8.12使用深拷贝,复制对象不会有运行错误 #include 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 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 #include 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 #include 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(); }
Class A public A(int m=o)X=m; 1 static void f(a a) Private int x void A f(aa) Cout<<x;∥付X的引用是错误的 cout<<ax;∥l确
6 Class A {public: A(int m=0) {x=m;} static void f(A a); Private: int x; }; void A::f(A a) { Cout <<x; //对X的引用是错误的 cout<<a.x; //正确 }
刀例8.15友元类的便用 #include using namespace std class student ∥前向引用声明,只声明类名,类定义在后面 class Teacher public eacher(int i, char*c); void showo void assign Grades( Student&s);∥使用未定义的类,需要前 向引用声明 private: char* name int id Class student ipublic Student(int i, char, float s, Teacher* t
7 //例8.15 友元类的使用 #include using namespace std; class Student ; //前向引用声明,只声明类名,类定义在后面 class Teacher {public: Teacher(int i,char* c); void show(); void assignGrades(Student& s); //使用未定义的类,需要前 向引用声明 private: char* name; int id; }; Class Student {public: Student(int i,char* c,float s,Teacher* t);
void printstuo friend class Teacher prⅳvate: Teacher*pT int sid char* name float grade Student: Student(int i, char*c, float S, Teacher* t) I sid=i name= new char[strlen(c)+1 if(name I=O) strcpy(name,c grade=s; T=t
8 void printstu(); friend class Teacher; private: Teacher* pT; int sid; char* name; float grade; }; Student::Student(int i,char* c,float s,Teacher* t) { sid=i; name= new char[strlen(c) +1]; if (name !=0) strcpy(name,c); grade=s; pT=t; }
void Student: printstuo coutid==id) s grade=4.0 ∥修改学生成绩,直接操作另一个类的保护成 贝
9 void Student::printstu() { coutid==id) s.grade=4.0; //修改学生成绩,直接操作另一个类的保护成 员 }
void maino { Teacher wang(62,“Wang”); wang showO Student s1(10, zhang san, 87.6, &wang) s1. printstuO wang assign Grade(s1); s1. printstuo Student s2(12, li si, 80, &wang S2. printstuo wang assign Grade(s2) S2. printstuo 10
10 void main() { Teacher wang(62, “ wang”); wang.show(); Student s1(10, “ zhang san”,87.6,&wang); s1.printstu(); wang.assignGrade(s1); s1.printstu(); Student s2(12, “ li si”,80,&wang); s2.printstu(); wang.assignGrade(s2); s2.printstu(); }