fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Io_obj
  8. {
  9. public:
  10. virtual Io_obj* clone() const =0;
  11. virtual ~Io_obj(){}
  12. };
  13.  
  14.  
  15. class Io_circle : public Io_obj
  16. {
  17. string c;
  18. public:
  19. Io_circle(string& s):c{s}{cout << c << '\n';}
  20. Io_circle* clone() const override{return new Io_circle{*this};}
  21. static Io_obj* new_circle(string& s){return new Io_circle{s};}
  22. };
  23.  
  24.  
  25. class Io_triangle : public Io_obj
  26. {
  27. string t;
  28. public:
  29. Io_triangle(string& s):t{s}{cout << t << '\n';}
  30. Io_triangle* clone() const override{return new Io_triangle{*this};}
  31. static Io_obj* new_triangle(string& s){return new Io_triangle{s};}
  32. };
  33.  
  34.  
  35. using Pf = Io_obj*(*)(std::string&);
  36. std::map<std::string,Pf> io_map{{"circle",&Io_circle::new_circle},{"triangle",&Io_triangle::new_triangle}};
  37. std::vector<std::string> vs{"circle","triangle"};
  38.  
  39. Io_obj* get_obj(int i){
  40. string word = vs[i];
  41.  
  42. if(auto f=io_map[word]){
  43. return f(word);
  44. }else{
  45. throw runtime_error{"shape not found"};
  46. }
  47. }
  48.  
  49.  
  50. int main() {
  51. // your code goes here
  52. return 0;
  53. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty