天才吃鸡少女

导航

实验5

宠物:

#include <iostream>
#include <string>
using namespace std;
  class MachinePets{
	public:
		 MachinePets(const string s):nickname(s){}
		 string getNickname(void ) const {return nickname;}
		 virtual string talk( void )=0;
    private:
    	 string nickname;
};
class PetCats:public MachinePets{
      public:
	      PetCats(const string s):MachinePets(s){}
          string talk( void ){return string("miao wu");}
};

class PetDogs:public MachinePets{
     public:
	     PetDogs(const string s):MachinePets(s){}
         string talk( void ){return string("wang wang");}
};
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;
   }

  

总结:课上做的,避免建项目就没有划分开来。

posted on 2019-05-28 20:54  天才吃鸡少女  阅读(136)  评论(0编辑  收藏  举报