- 论坛徽章:
- 0
|
#include <iostream>
#include<cstring>
using namespace std;
enum Petstype {dog,cat,bird,fish};
class Pets
{
private:
char *name;
Petstype type;
public:
Pets(const char *name ="sonny",Petstype type=dog);
Pets& operator =(const Pets &s);
~Pets();
void show() const;
};
Pets: ets(const char *name,Petstype)
{
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->type=type;
}
Pets::~Pets()
{
delete[]name;
}
Pets& Pets: perator =(const Pets &s)
{
if(&s==this) 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;
} |
|