// C++ program to pass function as a// pointer to any function#include<iostream>usingnamespace std;// Function that add two numbersintadd(int x,int y){return x + y;}// Function that multiplies two// numbersintmultiply(int x,int y){return x * y;}// Function that takes a pointer// to a functionintinvoke(int x,int y,int(*func)(int,int)){returnfunc(x, y);}// Driver Codeintmain(){// Pass pointers to add & multiply// function as required
cout <<"Addition of 20 and 10 is ";
cout <<invoke(20,10,&add)<< '
';
cout <<"Multiplication of 20"<<" and 10 is ";
cout <<invoke(20,10,&multiply)<< '
';return0;}
classMyClass{public:MyClass();// Note: No longer marked `static`, and only takes the actual argumentvoidCallback(int x);private:int private_x;};MyClass::MyClass(){usingnamespace std::placeholders;// for `_1`
private_x =5;
handler->addHandler(std::bind(&MyClass::Callback,this, _1));}voidMyClass::Callback(int x){// No longer needs an explicit `instance` argument,// as `this` is set up properly
cout << x + private_x << endl;}
// program to print a text#include<iostream>usingnamespace std;// display a numbervoiddisplayNum(int n1,float n2){
cout <<"The int number is "<< n1;
cout <<"The double number is "<< n2;}intmain(){int num1 =5;double num2 =5.5;// calling the functiondisplayNum(num1, num2);return0;}
// program to print a text#include<iostream>usingnamespace std;// display a numbervoiddisplayNum(int n1,float n2){
cout <<"The int number is "<< n1;
cout <<"The double number is "<< n2;}intmain(){int num1 =5;double num2 =5.5;// calling the functiondisplayNum(num1, num2);return0;}