#include "stdafx.h" #include <iostream> using namespace std; int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int cheng(int a, int b) { return a*b; } int cal(int a, int b, int(*pFunc)(int,int)) { return pFunc(a, b); } // class AbstractCal { public: void setX(int x) { this->x = x; } void setY(int y) { this->y = y; } virtual int GetResult() = 0; protected: int x; int y; }; class AddCal :public AbstractCal { public: AddCal(int a, int b) { x = a; y = b; } int GetResult() { return x + y; } }; class SubCal :public AbstractCal { public: SubCal(int a, int b) { x = a; y = b; } int GetResult() { return x - y; } }; class CheCal :public AbstractCal { public: CheCal(int a, int b) { x = a; y = b; } int GetResult() { return x * y; } }; int Cal(AbstractCal* obj) { return obj->GetResult(); } int main() { cout << cal(10, 20, add) << endl; cout << cal(10, 20, sub) << endl; cout << cal(10, 20, cheng) << endl; cout << "-------------------------------" << endl; AddCal a(10,20); cout << Cal(&a) << endl; SubCal b(10, 20); cout << Cal(&b) << endl; CheCal c(10, 20); cout << Cal(&c) << endl; return 0; }