fig05_28.cpp

来自「经典vc教程的例子程序」· C++ 代码 · 共 44 行

CPP
44
字号
// Fig. 5.28: fig05_28.cpp
// Demonstrating an array of pointers to functions
#include <iostream.h>
void function1( int );
void function2( int );
void function3( int );

int main()
{
   void (*f[ 3 ])( int ) = { function1, function2, function3 };
   int choice;

   cout << "Enter a number between 0 and 2, 3 to end: ";
   cin >> choice;

   while ( choice >= 0 && choice < 3 ) {
      (*f[ choice ])( choice );
      cout << "Enter a number between 0 and 2, 3 to end: ";
      cin >> choice;
   }

   cout << "Program execution completed." << endl;
   return 0;
}

void function1( int a )
{
   cout << "You entered " << a 
        << " so function1 was called\n\n";
}

void function2( int b )
{
   cout << "You entered " << b 
        << " so function2 was called\n\n";
}

void function3( int c )
{
   cout << "You entered " << c 
        << " so function3 was called\n\n";
}

⌨️ 快捷键说明

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