tdudzik

Untitled

Oct 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "func1.cpp"
  4. #include "func2.cpp"
  5.  
  6. using namespace std;
  7.  
  8. class Function {
  9. public:
  10.     void (*function_pointer)();
  11.  
  12.     string description;
  13.  
  14.     Function(void (*function_pointer)(), string description) {
  15.         this->function_pointer = function_pointer;
  16.         this->description = description;
  17.     }
  18. };
  19.  
  20. int main() {
  21.     Function *functions[] = {
  22.             new Function(&function1, "The goal of this task is to print hello"),
  23.             new Function(&function2, "The goal of this task is to print hello 2")};
  24.     int number_of_functions = sizeof(functions) / sizeof(*functions);
  25.  
  26.     while (true) {
  27.         cout << "Functions: " << endl;
  28.         cout << "0. Finish" << endl;
  29.         for (int i = 0; i < number_of_functions; i++) {
  30.             cout << i + 1 << ". " << functions[i]->description << endl;
  31.         }
  32.         cout << endl;
  33.  
  34.         int function_number;
  35.         cout << "Function number: ";
  36.         cin >> function_number;
  37.         cout << endl;
  38.  
  39.         if (function_number == 0) {
  40.             break;
  41.         } else if (function_number < 1 || function_number > number_of_functions) {
  42.             cout << "--" << endl;
  43.             cout << "Invalid function number" << endl;
  44.             cout << "--" << endl << endl;
  45.         } else {
  46.             cout << "--" << endl;
  47.             // call function
  48.             (*functions[function_number - 1]).function_pointer();
  49.             cout << "--" << endl << endl;
  50.         }
  51.     }
  52.  
  53.     // Clean
  54.     for (int i = 0; i < number_of_functions; i++) {
  55.         delete functions[i];
  56.     }
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment