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;
};
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); //析构函数
}
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();
}
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];
};
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();
}
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; //正确
}
7
//例 8.15 友元类的使用
#include<iostream>
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);
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;
}
9
void Student::printstu()
{ cout<<“学号,”<<sid<<“姓名,”<<name;
cout<<“成绩,”<<grade<<endl;
}
Teacher::Teacher(int i,char* c)
{ id =i;
name=new char[strlen(c) +1];
if(name !=0)
strcpy(name,c);
}
void Teacher::show()
{ cout<<“工作证号,”<<id<<“姓名,”<<name<<endl;
}
void Teacher::assignGrades(Student& s)
{ if (s.pT->id==id)
s.grade=4.0; //修改学生成绩,直接操作另一个类的保护成员
}
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();
}
11
例 8.17
…
const int A::b=10; //静态常数据成员在类外说明和初始化
A::A(int i):a(i),r(a) //常数据成员只能通过初始化列表来获得初始值
{
// 因为 a和 r是常数据成员,不能把语句 a=i;r=a;写在构造函数内
}
…
void main()
{
A x(100),y(0);
…
}
//例 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;
};
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); //析构函数
}
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();
}
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];
};
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();
}
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; //正确
}
7
//例 8.15 友元类的使用
#include<iostream>
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);
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;
}
9
void Student::printstu()
{ cout<<“学号,”<<sid<<“姓名,”<<name;
cout<<“成绩,”<<grade<<endl;
}
Teacher::Teacher(int i,char* c)
{ id =i;
name=new char[strlen(c) +1];
if(name !=0)
strcpy(name,c);
}
void Teacher::show()
{ cout<<“工作证号,”<<id<<“姓名,”<<name<<endl;
}
void Teacher::assignGrades(Student& s)
{ if (s.pT->id==id)
s.grade=4.0; //修改学生成绩,直接操作另一个类的保护成员
}
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();
}
11
例 8.17
…
const int A::b=10; //静态常数据成员在类外说明和初始化
A::A(int i):a(i),r(a) //常数据成员只能通过初始化列表来获得初始值
{
// 因为 a和 r是常数据成员,不能把语句 a=i;r=a;写在构造函数内
}
…
void main()
{
A x(100),y(0);
…
}