⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list1511.cpp

📁 teach yourself C++ in 21 days 第五版
💻 CPP
字号:
//Listing 15.11 Array of pointers to member functions
#include <iostream>
using std::cout;
using std::endl;

class Dog
{
  public:
    void Speak()const { cout << "Woof!" << endl; }
    void Move() const { cout << "Walking to heel..." << endl; }
    void Eat() const { cout << "Gobbling food..." << endl; }
    void Growl() const { cout << "Grrrrr" << endl; }
    void Whimper() const { cout << "Whining noises..." << endl; }
    void RollOver() const { cout << "Rolling over..." << endl; }
    void PlayDead() const 
{ cout << "The end of Little Caesar?" << endl; }
};

typedef void (Dog::*PDF)()const ;
int main()
{
   const int MaxFuncs = 7;
   PDF DogFunctions[MaxFuncs] =
      {Dog::Speak,
       Dog::Move,
       Dog::Eat,
       Dog::Growl,
       Dog::Whimper,
       Dog::RollOver,
       Dog::PlayDead };

   Dog* pDog =0;
   int Method;
   bool fQuit = false;

   while (!fQuit)
   {
      cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
      cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
      std::cin >> Method;
      if (Method <= 0 || Method >= 8)
      {
         fQuit = true;
      }
      else
      {
         pDog = new Dog;
         (pDog->*DogFunctions[Method-1])();
         delete pDog;
      }
   }
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -