fork(6) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Base {virtual int process(int a, int b) = 0;};
  5. static struct : Base {
  6. int process(int a, int b) { return a+b;}
  7. } add;
  8. static struct : Base {
  9. int process(int a, int b) { return a-b;}
  10. } subtract;
  11. static struct : Base {
  12. int process(int a, int b) { return a*b;}
  13. } multiply;
  14. static struct : Base {
  15. int process(int a, int b) { return a/b;}
  16. } divide;
  17.  
  18. void perform(Base& op, int a, int b) {
  19. cout << "input: " << a << ", " << b << "; output: " << op.process(a, b) << endl;
  20. }
  21.  
  22. int main() {
  23. perform(add, 2, 3);
  24. perform(subtract, 6, 1);
  25. perform(multiply, 6, 7);
  26. perform(divide, 72, 8);
  27. return 0;
  28. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
input: 2, 3; output: 5
input: 6, 1; output: 5
input: 6, 7; output: 42
input: 72, 8; output: 9