20-CS-122-001 Computer Science II Spring 2012
Pointer to Function

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Source

#include <iostream>
using namespace std;

int subt (int x, int y) {  return x-y;  }

int add  (int x, int y) {  return x+y;  }

int mult (int x, int y) {  return x*y;  }

void f (int (*g) (int x, int y)) {
   cout << "Answer: " << g(67,34) << "\n";
}

int main () {
   f(mult);
   f(add);
   f(subt);
}

// Sample output:
//  Answer: 2278
//  Answer: 101
//  Answer: 33