1 #include<string>
 2 using namespace std;
 3 #ifndef PETS_H
 4 #define PETS_H
 5 class MachinePets{
 6 public:
 7     MachinePets(const string s):Nickname(s){}
 8     const string getNickname();
 9     virtual string talk()=0;
10 private:
11     string Nickname;
12 };
13 #endif
machinepets.h
 1 #include<string>
 2 using namespace std;
 3 #ifndef PETCATS_H
 4 #define PETCATS_H
 5 #include"pets.h"
 6 
 7 class PetCats : public MachinePets{
 8 public:
 9     PetCats(const string s):MachinePets(s){}
10     string talk();
11 private:
12     string voice;
13 };
14 #endif
petcats
 1 #include<string>
 2 using namespace std;
 3 #ifndef PETDOGS_H
 4 #define PETDOGS_H
 5 #include"pets.h"
 6 
 7 class PetDogs : public MachinePets{
 8 public:
 9     PetDogs(const string s):MachinePets(s){}
10     string talk();
11 private:
12     string voice;
13 };
14 #endif
petdogs
1 #include"pets.h"
2 #include<iostream>
3 #include<string>
4 using namespace std;
5 
6 const string MachinePets::getNickname(){
7     return Nickname;
8 }
machinepets.cpp
 1 #include<iostream>
 2 #include<string>
 3 #include"petcats.h"
 4 #include"pets.h"
 5 using namespace std;
 6 
 7 string PetCats::talk(){
 8     voice="miao wu~";
 9     return voice;
10 }
petcats.cpp
 1 #include<iostream>
 2 #include<string>
 3 #include"petdogs.h"
 4 #include"pets.h"
 5 using namespace std;
 6 
 7 string PetDogs::talk(){
 8     voice="wang wang~";
 9     return voice;
10 }
petdogs.cpp
 1 #include"petcats.h"
 2 #include"petdogs.h"
 3 #include"pets.h"
 4 #include<iostream>
 5 #include<string>
 6 using namespace std;
 7 
 8 void play(MachinePets *p){
 9     cout<<p -> getNickname()<<"   say  "<<p -> talk()<<endl;
10 }
11 
12 
13 int main(){
14     PetCats cat("miku");
15     PetDogs dog("da huang");
16 
17     play(&cat);
18     play(&dog);
19     system("pause");
20     return 0;
21 }
main

posted on 2019-06-02 13:14  domestic徐婷楠  阅读(87)  评论(0编辑  收藏  举报