1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
using namespace std;
class Calculator {
//members
static int (Calculator::*cp)(int,int);
static int(Calculator::*perform(string function))(int,int);
//member functions
int add(int x, int y) { return x+y; };
int subtract(int x, int y) { return x-y; }
int multiply(int x, int y) { return x*y; }
int devide(int x, int y) {return x/y; }
public:
friend void print(string function, int x, int y);
};
int (Calculator::*Calculator::cp)(int,int) = &Calculator::add;
int(Calculator::*Calculator::perform(string function))(int,int) {
if (function == "add") return &Calculator::add;
if (function == "subtract") return &Calculator::subtract;
if (function == "multiply") return &Calculator::multiply;
if (function == "devide") return &Calculator::devide;
else return &Calculator::add;
}
void print(string function, int x, int y) {
Calculator::cp = Calculator::perform(function);
cout << (Calculator::*cp)(x,y) << endl; //Expected unqualified id
}
int main() {
print("add", 6, 2);
print("subtract", 6, 2);
print("multiply", 6, 2);
print("devide", 6, 2);
}
|