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

实验五

作者:互联网

1

2

3

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

class MachinePets
{
public:
    MachinePets(const string s);
    virtual string talk() const=0;
    string getNickname();
    virtual ~MachinePets();
    
private:
    string nickname;
};



MachinePets::MachinePets(const string s):nickname(s)
{ }

string MachinePets::getNickname()
{
    return nickname;
}

MachinePets::~MachinePets()
{ }




class PetCats:public MachinePets
{
public:
    PetCats(const string s);
    string talk() const;
    ~PetCats();
};


PetCats::PetCats(const string s):MachinePets(s)
{ }

string PetCats::talk() const
{
    cout<<"words:";
    return "miao wu~ ";
}

PetCats::~PetCats()
{ }



class PetDogs:public MachinePets
{
public:
    PetDogs(const string s);
    string talk() const;
    ~PetDogs();
};


PetDogs::PetDogs(const string s):MachinePets(s)
{ }

string PetDogs::talk() const
{
    cout<<"words:";
    return "wang wang~";
}

PetDogs::~PetDogs()
{ }


void play(MachinePets *pet)
{
    cout<<pet->getNickname()<<" says "<<pet->talk()<<endl;
}

int main()
{
    PetCats cat("miku");
    PetDogs dog("da huang");
    
    play(&cat);
    play(&dog);
    
    return 0;
}
View Code

 

总结:

感觉好像有点会了。

标签:const,string,实验,MachinePets,PetCats,include,talk
来源: https://www.cnblogs.com/yinyinzuinihai/p/10979157.html