其他分享
首页 > 其他分享> > 实验5

实验5

作者:互联网

#include<iostream>
#include<string>
using namespace std;
class MachinePets{
    public:
        MachinePets(){}
        MachinePets(const string s){
            nikname=s;
        }
        string getNikname()const{return nikname;}
        virtual string talk(){return 0;}

        private:
            string nikname;
};

class PetCats:public MachinePets{
    public:
        PetCats(const string s):MachinePets(s){}
        string talk(){return "wu~";
        }
    private:
        string nikname;
};
class PetDogs:public MachinePets{
    public:
        PetDogs(const string s):MachinePets(s){}
        string talk(){return "wang~";
        }
    private:
        string nikname;
};
void play(MachinePets * p){
    cout<<p->getNikname()<<" "<<"says"<<" "<<p->talk()<<endl;
}
int main(){
    PetCats cat("miku");
    PetDogs dog("da huang");
    
    play(&cat);
    play(&dog);
    
    return 0;
}
机器宠物

 

标签:const,string,nikname,实验,MachinePets,return,public
来源: https://www.cnblogs.com/qiuxiuh/p/10970859.html