利用C++多态实现 “小鸡哔哔”的歌词

#include<iostream>
using namespace std;

int flag=0;
char a[]="小鸡哔哔~bi~哟";
char b[]="小鸡哔哔哔哔~bi~哟";

void fa1() {cout<<a<<endl;}
void fa2() {cout<<b<<endl;}
void fa()  {cout<<a<<endl<<b<<endl;}
void fb()  {cout<<b<<endl<<a<<endl;}
void fc()  {cout<<b<<endl<<a<<endl<<b<<endl;}
void fd()  {cout<<b<<endl<<a<<endl<<b<<endl;}

class xiao_ji
{
public:
   virtual void f1()
   {
       cout<<"你看那里有个小鸡哟"<<endl<<"你看那里它有小鸡哟"<<endl;
   }

   virtual void f2()const
   {
      if(flag==0)
      {fa();fa();fa();}
      else if(flag==1)
      {fb();fb();fa2();}
      else if(flag==2)
      {fa();fa();}
      else if(flag==3)
      {fb();fa2();}
      else if(flag==4)
      {fa();fa();}
      else if(flag==5)
      {fb();fa2();}
      else if(flag==6)
      {fa();fa();}
      else if(flag==7)
      {fb();fa2();}
      else if(flag==8)
      {fa();fa();}
      else if(flag==9)
      {fb();fb();fa2();}
      else if(flag==10)
      {fb();fa2();fa2();}
      flag++;
   }

   virtual void f3()const{}
};

class mu_ji:public xiao_ji
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有母鸡哟"<<endl;
   }

   virtual void f2()const
   {
      this->xiao_ji::f2();
   }

   virtual void f3()const
   {
        cout<<"母鸡咯咯咯咯咯~go~"<<endl;
   }
};

class gong_ji:public mu_ji
{
public:

   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有公鸡哟"<<endl;
   }

   virtual void f2()const
   {
      this->xiao_ji::f2();
   }

   virtual void f3()const
   {
        cout<<"公鸡喔喔喔喔喔~wo~"<<endl;
        this->mu_ji::f3();
   }
};

class huo_ji:public gong_ji
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有火鸡哟"<<endl;
   }

   virtual void f2()const
   {
      this->xiao_ji::f2();
   }

   virtual void f3()const
   {
        cout<<"火鸡嘟嘟嘟嘟嘟~du~"<<endl;
        this->gong_ji::f3();
   }
};

class ge_zi:public huo_ji
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有鸽子哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"鸽子图噜噜噜噜~lu~"<<endl;
       this->huo_ji::f3();
   }
};

class mao_mi:public ge_zi
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有猫咪哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"猫咪喵喵喵喵喵~miao~"<<endl;
       this->ge_zi::f3();
   }
};

class xiao_gou:public mao_mi
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有小狗哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"小狗旺旺旺旺旺~wang~"<<endl;
       this->mao_mi::f3();
   }
};

class shan_yang:public xiao_gou
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有山羊哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"山羊咩~mie~"<<endl;
       this->xiao_gou::f3();
   }
};

class mian_yang:public shan_yang
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有绵羊哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
        cout<<"绵羊咇~bie~"<<endl;
        this->shan_yang::f3();
   }
};

class mu_niu:public mian_yang
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有母牛哟"<<endl;
   }

   virtual void f2()const
   {
       this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"母牛萌~meng~"<<endl;
       this->mian_yang::f3();
   }
};

class gong_niu:public mu_niu
{
public:
   virtual void f1()
   {
       for(int i=0;i<2;i++)
         cout<<"你看那里又有公牛哟"<<endl;
   }

   virtual void f2()const
   {
        this->xiao_ji::f2();
   }

   virtual void f3()const
   {
       cout<<"公牛呣~mu~"<<endl;
       this->mu_niu::f3();
   }
};

class tuo_laji:public xiao_ji
{
public:
  virtual void f1(){}
  void f2()const
  {
      for(int i=0;i<2;++i)
        cout<<"你看那里有拖拉机哟"<<endl;
      for(int i=0;i<3;++i)
        cout<<"拖拉机嗡~weng~"<<endl;
      cout<<"小鸡被撞 喔 啊喔~"<<endl;
  }
};

int main()
{
    xiao_ji* p[12];
    p[0]=new xiao_ji;
    p[1]=new mu_ji;
    p[2]=new gong_ji;
    p[3]=new huo_ji;
    p[4]=new ge_zi;
    p[5]=new mao_mi;
    p[6]=new xiao_gou;
    p[7]=new shan_yang;
    p[8]=new mian_yang;
    p[9]=new mu_niu;
    p[10]=new gong_niu;
    p[11]=new tuo_laji;

    for(int i=0;i<12;++i)
    {
        p[i]->f1();
        p[i]->f3();
        p[i]->f2();
        cout<<endl;
    }
    delete []p;
    return 0;
}


由于歌词过长,直接截取了最后一部分


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值