title: create objects based on entries from a file
body: <p>How to create objects during runtime based on input from file.</p>
  <p>I have few entries in a file in an organized way separated by a space</p>
  <pre><code>entryname type satus
  entry1 type1  yes
  entry2 type2  no
  ...
  </code></pre>
  <p>I would like to create objects of below class based on number of entries.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  
  class Entry{
      std::string name;
      std::string type;
      std::string availability;
  };
  
  void getEntriesInfo(std::vector&lt;std::string&gt; &amp;info)
  {
      std::string line;
      std::fstream myfile(&quot;entries.txt&quot;,std::ios::in);
      if (myfile.is_open())
      {
          //remove first line from file.
          getline(myfile,line);
          while ( getline(myfile,line) )
             info.push_back(line);
          myfile.close();
      }
      else std::cout &lt;&lt; &quot;Unable to open file&quot;; 
  }
  
  int main()
  {
      std::vector&lt;std::string&gt; entries_info;
      getEntriesInfo(entries_info);
  
      for(auto &amp;e: entries_info)
      std::cout&lt;&lt;e&lt;&lt;std::endl;
      return 0;
  }
  </code></pre>
  <p><strong>EDIT 1:</strong></p>
  <p>I have restructured code a bit using maps and vectors, can it be made more elegant.</p>
  <p>If you look at the code , I have not used constructors &amp; destructors properly, (I could not figure out a good way to use), also the setters and not written all kinds of constructors(deep copy or move) etc..</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  //#include &lt;istream&gt;
  #include &lt;map&gt;
  
  class AItem;
  AItem makeItem(std::string );
  void displayItemInfo(std::vector&lt;std::string&gt; info);
  
  class Item {
      public:
      virtual ~Item() = default;
      virtual std::string name() const = 0 ;
      virtual std::string quota() const = 0 ;
      virtual std::string make() const = 0 ;
  };
  
  class AItem : Item{
  private:
      std:: string _name;
      std:: string _quota;
      std:: string _make;
  public:
      virtual std::string name() const { return _name;} 
      virtual std::string quota() const { return _quota;}
      virtual std::string make() const {return _make;}
      void setName();
      void setQuota();
      void setMake();
      AItem(){}
      AItem(std::string name,std::string quota,std::string make) :_name(name),_quota(quota),_make(make) {}
      friend std::istream&amp; operator&gt;&gt;(std::istream&amp; in,AItem&amp; item);
      friend std::ostream &amp; operator &lt;&lt; (std::ostream &amp;out, const AItem &amp; item);
  };
  
  void displayItemInfo(std::vector&lt;std::string&gt; info)
  {
          for(auto &amp;i_it:info)
              std::cout&lt;&lt;i_it&lt;&lt;std::endl;
  }
  
  std::vector&lt;std::string&gt; readItemInfo(const char *str)
  {
      std::string line;
      std::vector&lt;std::string&gt; itemInfo;
      std::ifstream file(str, std::ios::in);
      if(file.is_open()){
          getline(file,line);
          while(getline(file,line))
              itemInfo.push_back(line);
          file.close();
      }else{
          std::cout&lt;&lt;&quot;Unable to open&quot;&lt;&lt;str&lt;&lt;std::endl;
      }
     return itemInfo;
  }
  
  std::ostream &amp;operator &lt;&lt; (std::ostream &amp;out,const AItem&amp; item)
  {
      //std::cout &lt;&lt;item.name()&lt;&lt;&quot; &quot;&lt;&lt;item.quota()&lt;&lt;&quot; &quot;&lt;&lt;item.make() &lt;&lt;std::endl;
      return out &lt;&lt;item.name()&lt;&lt;&quot; &quot;&lt;&lt;item.quota()&lt;&lt;&quot; &quot;&lt;&lt;item.make() &lt;&lt;std::endl;
  }
  
  std::istream&amp; operator &gt;&gt; (std::istream&amp; in,AItem&amp; item) {
       //in &gt;&gt; item._name &gt;&gt; item._quota &gt;&gt; item._make;
       return in &gt;&gt; item._name &gt;&gt; item._quota &gt;&gt; item._make;
  }
  
  AItem makeItem(std::string aitem)
  {
      std::stringstream ss(aitem);
      std::vector&lt;std::string&gt; arguments;
      std::string entry;
  
      while (ss &gt;&gt; entry) {
              arguments.push_back(entry);
      }
      AItem aItem_obj(arguments.at(0),arguments.at(1),arguments.at(2));
      return aItem_obj;
  }
  
  std::map&lt;std::string,AItem&gt; createItems(std::vector&lt;std::string&gt; item)
  {
     std::map&lt;std::string,AItem&gt; ItemMap;
      for(auto &amp;i_it:item) {
          AItem a_item = makeItem(i_it); 
          ItemMap.insert(std::make_pair(a_item.name(),a_item));
      }
    //  for(auto &amp;m:ItemMap)
    //      std::cout&lt;&lt;m.first&lt;&lt;m.second&lt;&lt;std::endl;
          
      return ItemMap;
  }
  
  int main(int argc, char*argv[])
  {
      char *str = &quot;player_info.txt&quot;;
      std::vector&lt;std::string&gt; info;
      std::map&lt;std::string,AItem&gt; ItemMap;
     // info = readItemInfo(str);
     // displayItemInfo(info);    
     //#if 0
      ItemMap = createItems(readItemInfo(str));
      for(auto &amp;item:ItemMap)
          std::cout&lt;&lt;item.first&lt;&lt;item.second&lt;&lt;std::endl;
     //#endif
          
      return 0;
  }
  </code></pre>
  <p><strong>EDIT 2</strong></p>
  <p>I have done this way</p>
  <p>entry.h</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class Entry{
  private:
      std::string name;
      std::string type;
      std::string availability;
  public:
      std::string getName(void) const;
      std::string getType(void) const;
      std::string getAvailability(void) const;
  
      void setName(const std::string n);
      void setType(const std::string t);
      void setAvailability(const std::string y);
  
      Entry();
      ~Entry();
  
      friend std::ostream &amp;operator &lt;&lt; (std::ostream &amp;out,const Entry &amp;e);
  };
  </code></pre>
  <p>entry.cpp</p>
  <pre><code>#include &quot;entry.h&quot;
  
  std::string Entry ::getName(void) const{
      return name;
  }
  std::string Entry ::getType(void) const{
      return type;
  }
  std::string Entry ::getAvailability(void) const{
      return availability;
  }
  void Entry ::setName(const std::string n){
      name = n;
  }
  void Entry ::setType(const std::string t){
      type = t;
  }
  void Entry ::setAvailability(const std::string y){
      availability = y;
  }
  Entry::Entry(){
  }
  Entry::~Entry(){
  }
  std::ostream &amp;operator &lt;&lt; (std::ostream &amp;out,const Entry &amp;e){
          return out &lt;&lt; e.getName()&lt;&lt;&quot; &quot; &lt;&lt;e.getType()&lt;&lt;&quot; &quot;&lt;&lt;e.getAvailability()&lt;&lt;std::endl;
  }
  </code></pre>
  <p>main.cpp</p>
  <pre><code>#include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &quot;entry.h&quot;
  
  void makeEntry(std::string line,std::vector&lt;Entry&gt; &amp;entries)
  {
      Entry P1;
      P1.setName(line.substr(0, line.find(' ')));
      P1.setType(line.substr(P1.getName().size(), line.find(' ')));
      P1.setAvailability(line.substr(P1.getName().size()+P1.getType().size(), line.find(' ')));
      //std::cout&lt;&lt;&quot;C:&quot;&lt;&lt;P1.getName()&lt;&lt;P1.getType()&lt;&lt;P1.getAvailability()&lt;&lt;std::endl;
      entries.push_back(P1);
  }
  void getEntriesInfo(std::vector&lt;std::string&gt; &amp;info)
  {
      std::string line;
      std::fstream myfile(&quot;entries.txt&quot;,std::ios::in);
      if (myfile.is_open())
      {
          //remove first line from file.
          getline(myfile,line);
          while ( getline(myfile,line) )
             info.push_back(line);
          myfile.close();
      }
      else std::cout &lt;&lt; &quot;Unable to open file&quot;; 
  }
  
  int main()
  {
      std::vector&lt;std::string&gt; entries_info;
      std::vector&lt;Entry&gt; entries;
      getEntriesInfo(entries_info);
  
      for(auto &amp;e: entries_info) {
          std::cout&lt;&lt;e&lt;&lt;std::endl;
          makeEntry(e,entries);
      }
      std::cout&lt;&lt;&quot;OUT&quot;&lt;&lt;std::endl;
  
      for(auto &amp;e: entries) {
          std::cout&lt;&lt;e&lt;&lt;std::endl;
      }
      return 0;
  }
  </code></pre>