c++ - How to fill a vector in class with objects of different class -


i'm working on project in have create class of workers (with basic info) , put them in vector.

i've found solution works me, i'm curious there better way this? so, i'm looking different implementation of code.

this code:

#include<iostream> #include<vector> #include<string> #include<iterator>  class worker {   private:     std::string _name;     int _age;     double _pay;   public:     worker() : _age{0}, _pay{0.0} {}     worker(std::istream& entry){ input(entry); }      std::istream& input(std::istream& x){ return x >> _name >> _age >> _pay ; }      const std::string& get_name() { return _name; }     const int& get_age(){ return _age; }     const double& get_pay(){ return _pay;} };  class vecworkers {   private:     std::vector<worker> vec_workers;   public:     void input(std::istream& x)     {       worker temp;       while(temp.worker::input(x))       vec_workers.push_back(temp);      }      void output(std::ostream& x)     {       worker temp;       for( std::vector<worker>::const_iterator = vec_workers.begin() ; != vec_workers.end() ; ++it )       {         temp = *it ;         std::cout << temp.get_name() << " "<< temp.get_age() << " " << temp.get_pay() << std::endl;       }     }  };  int main() {   vecworkers w1;   w1.input(std::cin);   w1.output(std::cout);   return 0; } 

i appreciate can provide.

edit 1: title of question bit misleading


answer:

well, since tagged c++11, assuming can use c++11 features here... so:

some advice on code:

  • the getters in worker class returns const reference, should marked const functions, have:

    const std::string& get_name() const { return _name; } const int& get_age() const { return _age; } const double& get_pay() const { return _pay;} 
  • again, theres no point returning const int& , const double&, (though believe optimizers figure out).

    const std::string& get_name() const { return _name; } int get_age() const { return _age; } double get_pay() const { return _pay;} 
  • you may want think overloading insertion , extraction operators << , >> worker. conveys message.

  • other ending input loop, how handle istream failures based on wrong formatting? may want think that. without failures, single wrong formatting will screw inputs of entire vecworkers


  • first alternative (no design change, cleanup... but remember, must make above const corrections work):

class vecworkers {   private:     std::vector<worker> vec_workers;   public:     void input(std::istream& x)     {         for(worker w; w.input(x); vec_workers.push_back(w));     }      void output(std::ostream& x)     {         for(const auto& w : vec_workers)             std::cout << w.get_name() << " " << w.get_age() << " " << w.get_pay() << std::endl;     }  }; 
  • second alternative (moved printing responsibility... or may want have class responsible printing if gonna have different styles)

class worker {   private:     std::string _name;     int _age;     double _pay;   public:     worker() : _age{0}, _pay{0.0} {}     worker(std::istream& entry){ input(entry); }      std::istream& input(std::istream& x){ return x >> _name >> _age >> _pay ; }     std::ostream& output(std::ostream& x) const { return x << _name << _age << _pay; }      const std::string& get_name() const { return _name; }     int get_age() const { return _age; }     double get_pay() const { return _pay;} };  class vecworkers {   private:     std::vector<worker> vec_workers;   public:     void input(std::istream& x)     {         for(worker w; w.input(x); vec_workers.push_back(w));     }      void output(std::ostream& x)     {         for(const auto& w : vec_workers)             w.output(std::cout);    //since didn't overload insertion operator <<     }  }; 

  • third alternative?... quite trivial compound ... so, bit of cleanup solution works.

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -