#include
using namespace std;
enum Pets_type{dog,cat,bird,fish};
class Pets{
private:
char *name;
Pets_type type;
public:
Pets(const char *n="sonny",int type1 = 0);
Pets& operator=(const Pets &s);
~Pets();
void show()const;
};
Pets::Pets(const char *n,int type1) // 构造函数
{
name = new char[strlen(n) + 1];
// ERROR found
name=n;
type = Pets_type(type1);
}
Pets::~Pets() // 析构函数
{
// ERROR found
name='/0';
}
Pets& Pets::operator =(const Pets &s)
{
// ERROR found
if(&s == null) //确保不要向自身赋值
return *this;
delete []name;
name = new char[strlen(s.name) + 1];
strcpy(name,s.name);
type = s.type;
return *this ;
}
void Pets::show()const
{
cout<<"Name: "<<name<<" Type: ";
switch(type)
{
case dog: cout<<"dog"; break;
case cat: cout<<"cat"; break;
case bird: cout<<"bird"; break;
case fish: cout<<"fish"; break;
}
cout<<endl;
}
int main()
{
Pets mypet1,mypet2("John",dog);
Pets youpet("Danny",cat);
mypet1.show();
mypet2.show();
youpet.show();
youpet = mypet2;
youpet.show();
return 0;
}