#include<iostream>
using namespace std;
class student
{
public:
student(char *pName="no name",int ssID=0)
{
id=ssID;
name=new char[strlen(pName)+1];
strcpy(name,pName);
cout<<"construct new student "<<pName<<endl;
};
void copy(student &s)
{
if(this==&s)
{
cout<<"错误:不能将一个对象复制到自己!"<<endl;
return;
}
else
{
name = new char[strlen(s.name)+1];
strcpy(name,s.name);
id=s.id;
cout<<"资源复制函数被调用! "<<endl;
}
}
void disp()
{
cout<<" name:"<<name<<" ID:"<<id<<endl;
}
~student()
{
cout<<"Destruct "<<name<<endl;
//delete name;
}
private:
int id;
char *name;
};
void main()
{
student a("Kevin",12),b("Tom",23);
a.disp();
b.disp();
a.copy(a);
b.copy(a);
a.disp();
b.disp();
}
using namespace std;
class student
{
public:
student(char *pName="no name",int ssID=0)
{
id=ssID;
name=new char[strlen(pName)+1];
strcpy(name,pName);
cout<<"construct new student "<<pName<<endl;
};
void copy(student &s)
{
if(this==&s)
{
cout<<"错误:不能将一个对象复制到自己!"<<endl;
return;
}
else
{
name = new char[strlen(s.name)+1];
strcpy(name,s.name);
id=s.id;
cout<<"资源复制函数被调用! "<<endl;
}
}
void disp()
{
cout<<" name:"<<name<<" ID:"<<id<<endl;
}
~student()
{
cout<<"Destruct "<<name<<endl;
//delete name;
}
private:
int id;
char *name;
};
void main()
{
student a("Kevin",12),b("Tom",23);
a.disp();
b.disp();
a.copy(a);
b.copy(a);
a.disp();
b.disp();
}