Using typedef with Pointers to Functions - C++ Function

C++ examples for Function:Function Pointer

Description

Using typedef with Pointers to Functions

Demo Code

#include <iostream> 
 
enum BOOL { FALSE, TRUE }; 
 
class Pet /* w  w  w . j a  v a  2s.  c o m*/
{ 
public: 
    Pet():age(1) {  } 
    virtual ~Pet() { } 
    virtual void speak() const = 0; 
    virtual void move() const = 0; 
protected: 
    int age; 
}; 
 
class Dog : public Pet 
{ 
public: 
    void speak() const { std::cout << "Woof!\n"; } 
    void move() const { std::cout << "Walking to heel ...\n"; } 
}; 
 
class Cat : public Pet 
{ 
public: 
    void speak() const { std::cout << "Meow!\n"; } 
    void move() const { std::cout << "Slinking ...\n"; } 
}; 
 
class Horse : public Pet 
{ 
public: 
    void speak() const { std::cout << "Winnie!\n"; } 
    void move() const { std::cout << "Galloping ...\n"; } 
}; 
 
int main() 
{ 
    void (Pet::*pFunc)() const = 0; 
    Pet* ptr = 0; 
    int animal; 
    int method; 
    bool fQuit = false; 
 
    while (fQuit == false) 
    { 
         std::cout << "(0) Quit (1) Dog (2) Cat (3) Horse: "; 
         std::cin >> animal; 
         switch (animal) 
         { 
         case 1: 
             ptr = new Dog; 
             break; 
         case 2: 
             ptr = new Cat; 
             break; 
         case 3: 
             ptr = new Horse; 
             break; 
         default: 
              fQuit = true; 
             break; 
         } 
         if (fQuit) 
             break; 
 
           std::cout << "(1) Speak (2) Move: "; 
           std::cin >> method; 
           switch (method) 
           { 
           case 1: 
               pFunc = &Pet::speak; 
               break; 
          default: 
               pFunc = &Pet::move; 
               break; 
           } 
 
           (ptr->*pFunc)(); 
          delete ptr; 
    } 
    return 0; 
}

Result


Related Tutorials