|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <string>
using namespace std;
class Pet{
public:
Pet(string theName);
void eat();
void sleep();
void play();
protected:
string name;
};
class Cat: public Pet{
public :
Cat(string theName);
void climb();
void play();
};
class Dog : public Pet{
public :
Dog(string theName);
void bark();
void play();
};
Pet::Pet(string theName){
name = theName;
}
void Pet::eat(){
cout<<name<<"正在吃东西"<<endl;
}
void Pet::sleep(){
cout<<name<<"正在睡大觉"<<endl;
}
void Pet::play(){
cout<<name<<"正在玩呢\n"<<endl;
}
Cat::Cat(string theName) : Pet(theName){
}
void Cat::climb(){
cout<<name<<"正在爬树"<<endl;
}
void Cat::play(){
cout<<name<<"玩毛线球"<<endl;
}
Dog::Dog(string theName) : Pet(theName){
}
void Dog::bark(){
Pet::play(); //小甲鱼弄这个函数没看懂,但是我觉得没有必要写这个 所以我注释了
cout<<name<<"旺旺"<<endl;
}
void Dog::play(){
Pet::play(); //小甲鱼弄这个函数没看懂,但是我觉得没有必要写这个 所以我注释了
cout<<name<<"正在追猫"<<endl;
}
int main(){
Pet *cat = new Cat("加菲"); //这里我改成了Cat *cat = new Cat("加菲");
Pet *dog = new Dog("欧迪"); //这里我改成了Dog *cat = new Dog("欧迪");
cat->sleep();
cat-> eat();
cat->play();
dog->sleep();
dog-> eat();
dog->play();
delete cat;
delete dog;
return 0;
}
我的这种写法是否有什么问题?? |
|