Hello! Such situation. The program for calculating employee salaries.

Employees 3 species.

There is a base class worker , from it 3 successors turn out: from it, employee , manager , sales . Information about employees is stored in a database.

Here we pull out information about some employee, in the column type of activity - employee . How to create object of class employee ? Those. for manager , an object of class manager , etc.

  • one
    And what is the difficulty? - Petr Abdulin

1 answer 1

#include <iostream> #include <string> #include <map> #include <functional> class Worker { public: virtual std::string name() = 0; }; class Manager : public Worker { public: virtual std::string name() override { return "I am Manager"; } }; class Employee : public Worker { public: virtual std::string name() override { return "I am Employee"; } }; int main(int argc, char *argv[]) { std::map<std::string, std::function<Worker*()>> workerFactory; workerFactory["manager"] = []{return new Manager();}; workerFactory["employee"] = []{return new Employee();}; Worker *worker1 = workerFactory["manager"](); Worker *worker2 = workerFactory["employee"](); std::cout << worker1->name() << std::endl; std::cout << worker2->name() << std::endl; return 0; } 

Conclusion:

 I am Manager I am Employee