Guest User

Untitled

a guest
Oct 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. void f_a(void)
  6. {
  7.     std::cout << "a";
  8. }
  9.  
  10. void f_b(void)
  11. {
  12.     std::cout << "b";
  13. }
  14.  
  15. void f_c(void)
  16. {
  17.     std::cout << "c";
  18. }
  19.  
  20. void f_d(void)
  21. {
  22.     std::cout << "d";
  23. }
  24.  
  25. void f_e(void)
  26. {
  27.     std::cout << "e";
  28. }
  29.  
  30. void f_f(void)
  31. {
  32.     std::cout << "f";
  33. }
  34.  
  35. class Printer
  36. {
  37. private:
  38.     void (*pfnFunc)(void);
  39. public:
  40.     void setFunction(void (*pfnFunc)(void))
  41.     {
  42.         this->pfnFunc = pfnFunc;
  43.     }
  44.  
  45.     void exec()
  46.     {
  47.         this->pfnFunc();
  48.     }
  49.  
  50.     void (*getFunction(void))(void)
  51.     {
  52.         return this->pfnFunc;
  53.     }
  54.    
  55.     static void (*getFunctionByID(int i))(void)
  56.     {
  57.         void (*functions[])(void) = { f_a, f_b, f_c, f_d, f_e, f_f };
  58.         return functions[i];
  59.     }
  60. };
  61.  
  62. int main ( void )
  63. {
  64.     srand(time(NULL));
  65.  
  66.     Printer printer;
  67.  
  68.     for(int i = 0; i < 1000; i++) {
  69.         printer.setFunction(Printer::getFunctionByID(rand() % 5));
  70.         printer.exec();
  71.     }
  72.  
  73.     std::cin.get();
  74. }
Add Comment
Please, Sign In to add comment