?? 深拷貝.cpp
字號:
#include<iostream.h>
#include<string.h>
class student{
char *pName;
public:
student();
student(char *pname);
student(student &s);
~student();
student & operator=(student &s);
};
student::student(){
cout<<"Constructor";
cout<<"缺省"<<endl;
}
student::student(char*pname){
cout<<"Constructor";
if(pName=new char[strlen(pname)+1])strcpy(pName,pname);
cout<<pName<<endl;
}
student::student(student &s){
cout<<"Copy Constructor";
if(s.pName){
if(pName=new char[strlen(s.pName)+1])strcpy(pName,s.pName);
}
else pName=NULL;
cout<<pName<<endl;
}
student::~student(){
cout<<"Destuctor"<<pName<<endl;
if(pName)pName[0]='\0';
delete[]pName;
}
student&student::operator=(student&s){
cout<<"Copy Assign operator";
delete []pName;
if(s.pName){
if(pName=new char[strlen(s.pName)+1])strcpy(pName,s.pName);
}
else pName=NULL;
cout<<pName<<endl;
return *this;
}
void main(){
student s1("范英明"),s2("沈俊"),s3;
student s4=s1;
s3=s2;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -