作者在 2014-05-04 21:22:13 发布以下内容
#include <iostream>
#include <string>
using namespace std;
class Internet
{
public:
Internet(char *name,char *url)
{
Internet::name=new char[strlen(name)+1];
Internet::url=new char[strlen(url)+1];
if(name)
{
strcpy(Internet::name,name);
}
if(url)
{
strcpy(Internet::url,url);
}
}
Internet(Internet &temp)
{
Internet::name=new char[strlen(temp.name)+1];
Internet::url=new char[strlen(temp.url)+1];
if(name)
{
strcpy(Internet::name,temp.name);
}
if(url)
{
strcpy(Internet::url,temp.url);
}
}
~Internet()
{
delete[] name;
delete[] url;
}
Internet &operator =(Internet &temp)
{
delete[] this->name;
delete[] this->url;
this->name=new char[strlen(temp.name)+1];
this->url=new char [strlen(temp.url)+1];
if(this->name)
{
strcpy(this->name,temp.name);
}
if(this->url)
{
strcpy(this->url,temp.url);
}
return *this;
}
public:
char *name;
char *url;
};
void main()
{
Internet a("Education","www.edu.cn");
Internet b=a;
cout<<b.name<<endl<<b.url<<endl;
Internet c("Tsinghua","www.tsinghua.edu.cn");
b=c;
cout<<b.name<<endl<<b.url<<endl;
}