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

实验五

作者:互联网

#include <iostream>
#include<string>
#include"MachinePets.h"
#include "PetCats.h"
#include "PetDogs.h"
using namespace std;


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


int main() {
PetCats cat("miku");
PetDogs dog("da huang");
play(&cat); // 按照play()形参,传递参数
play(&dog); // 按照play()形参,传递参数
return 0;
}
main.cpp
#include <iostream>
#include"MachinePets.h"
using namespace std;


MachinePets::MachinePets{const string s):nickname(s){
}
string MachinePets::getNickname()const{
    return nickname;
}
MachinePets.cpp
#ifndef MachinePets_H
#define MachinePets_H

#include <string>
using namespace std;

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

#endif
MachinePets.h
#include<string>
#include"PetDogs.h"

using namespace std;

PetCats::PetCats(const string s):MachinePets(s){
}
string PetCats::talk(){
    return "nia~~";
}
PetCats.cpp
#ifndef PetCats_H
#define PetCats_H
#include"MachinePets.h"
#include <string>
using namespace std;

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

};


#endif
PetCats.h
#include<string>
#include"PetDogs.h"

using namespace std;

PetDogs::PetDogs(const string s):MachinePets(s){
}
string PetDogs::talk(){
    return "woof~~";
}
PetDogs.cpp
#ifndef PetDogs_H
#define PetDogs_H
#include"MachinePets.h"
#include <string>
using namespace std;

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


#endif
PetDogs.h

 

标签:PetDogs,const,string,实验,MachinePets,PetCats,include
来源: https://www.cnblogs.com/changtingzao/p/10972860.html