#include <iostream> #include "MachinePets.h" #include "Petcats.h" #include "Petdogs.h" using namespace std; void play(MachinePets *m){ cout<<m->getnickname()<<" say "<<m->talk()<<endl; } int main() { Petcats cat("miku"); Petdogs dog("da huang"); play(&cat); play(&dog); system("pause"); }
#ifndef _MACHINEPETS #define _MACHINEPETS #include <string> using namespace std; class MachinePets{ public: MachinePets(string s); virtual string talk()=0; string getnickname(); private: string nickname; }; #endif MachinePets.h
#include "MachinePets.h" #include <string> using namespace std; MachinePets::MachinePets(string s):nickname(s){ } string MachinePets::getnickname(){ return nickname; }
#ifndef _PETDOGS #define _PETDOGS #include"MachinePets.h" #include<string> using namespace std; class Petdogs:public MachinePets{ public: Petdogs(const string s); string talk(); }; #endif
#include <string> #include"Petdogs.h" using namespace std; Petdogs::Petdogs(const string s):MachinePets(s){} string Petdogs::talk(){ return "wang wang~"; }
#ifndef _PETCATS #define _PETCATS #include"MachinePets.h" #include<string> using namespace std; class Petcats:public MachinePets{ public: Petcats(string s); string talk(); }; #endif
#include "Petcats.h" #include <string> using namespace std; Petcats::Petcats(const string s):MachinePets(s){ } string Petcats::talk(){ return "miao wu~"; }