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

实验五

作者:互联网

//MachinePets.h
#include<iostream>
#include<string>
using namespace std;
#ifndef MACHINEPETS_H
#define MACHINEPETS_H
class MachinePets
{
public:
    MachinePets(const string s);
    virtual string talk() const=0;
    string getNickname();
private:
    string nickname;
};
#endif

//MachinePets.cpp
#include<iostream>
#include"MachinePets.h"
#include<string>
using namespace std;
MachinePets::MachinePets(const string s):nickname(s){}
string MachinePets::getNickname()
{
    return nickname;
}


//PetDogs.h
#include<iostream>
#include<string>
#include"MachinePets.h"
using namespace std;
#ifndef PETDOGS_H
#define PETDOGS_H
class PetDogs:public MachinePets{
public:
    PetDogs(const string s);
    string talk()const;
};
#endif

//PetDogs.cpp
#include<iostream>
#include<string>
#include"PetDogs.h"
using namespace std;
PetDogs::PetDogs(const string s):MachinePets(s){}
string PetDogs::talk()const
{
    return "says wang wang~";
}


//PetCats.h
#include"MachinePets.h"
#include<string>
using namespace std;
#ifndef PETCATS_H
#define PETCATS_H
class PetCats:public MachinePets
{
public:
    PetCats(const string s);
    string talk()const;
};
#endif

//PetCats.cpp
#include<iostream>
#include<string>
#include"PetCats.h"
using namespace std;
PetCats::PetCats(const string s):MachinePets(s){}
string PetCats::talk()const
{    
    return "says miao wu~";
}

//main.cpp
#include<iostream>
#include<string>
#include<cstdlib>
#include"MachinePets.h"
#include"PetCats.h"
#include"PetDogs.h"
using namespace std;
void play(MachinePets *pet)
{
    cout<<pet->getNickname()<<pet-> talk()<<endl;
}
int main()
{
    PetCats cat("miku");
    PetDogs dog("dahuang");
    
    play(&cat);
    system("pause");
    
    play(&dog);
    system("pause");
    
    return 0;
}

 

标签:PetDogs,const,string,实验,MachinePets,PetCats,include
来源: https://www.cnblogs.com/libing-072921/p/10970656.html