c++ - Building a dynamically allocated array of class Objects -


first off, if problem seems incredibly easy you, want in advance apologize beginner.

i have been stuck week problem , getting ridiculous since shouldn't hard, complete beginner me.

i writing program reads bunch of information regarding receipts text file, name, sum, date etc. , prints out screen. simple enough, right? started using static arrays in 2 classes transaction , transactionslist , working fine, printing contents of file screen fine 1 line after other. need using dynamic arrays.

each line in text file contains date, type, name, sum, number of friends , name of friends should read stored transaction class object inside dynamic array trans. having trouble understanding no matter how theory , googling on subject. should use overloaded assigment operator, copy constructor , how call them properly? have read on these concepts can't use them in program still. these questions flying around in head right now.

i have changed arrays friends , trans declared pointers understand correct. want allocate memory arrays "new", here starting unsure allocate new, inside contructors of classes or inside functions needed? realize vectors answer alot of these problems should tell have not gotten vectors yet, trying solve problem without vectors. realize may be bit backwards, should able build dynamically allocated array of objects , print out without vectors think. have heard more practical have understand assignment without concept of vectors. have read on difference between shallow copies , deep copies , theory, can't implement somehow. (i retarded know). have got far:

#include <iostream> #include <string> #include <fstream> #include <cstdlib> #include <iomanip>  using namespace std;  class transaction  {   private:    string date;    string type;    string name;    double sum;    int nr_friends;    string *friends;    public:    transaction();    ~transaction();    transaction &operator = ( const transaction &t );    string get_name();    int get_no_friends();    double get_sum();    bool readonetrans( istream &is );    void writeonetrans( ostream &os );  };   class transactionslist  {  private:    transaction *trans;    int no_trans;   public:    transactionslist();    ~transactionslist();    void read( istream & );    void print( ostream & os );    void add( transaction & t );   };   int main() {     ifstream infile("test.txt");     transaction t;     transactionslist tl;     // t.readonetrans(infile);  // reading 1 line works fine (when uncommented)    // t.writeonetrans(cout);  // printing works fine       //tl.read(infile); // here want read contents of file      //tl.print(cout); // , here print out them screen   return 0;  }   transaction::transaction()     {         date = "000000";         type = "transp";         name = "default";         sum = 0.0;         nr_friends = 0;         friends = null;     }  transaction::~transaction() {     delete [] friends; }  transaction &transaction::operator = ( const transaction &t ) {          if ( != &t )         {             delete[] friends;             date = t.date;             type = t.type;             name = t.name;             sum = t.sum;             nr_friends = t.nr_friends;             friends = new string[nr_friends];              ( int = 0; < nr_friends; i++ )             {                 friends[i] = t.friends[i];         }     }       return *this; }  string transaction::get_name()     {     return name; }  double transaction::get_sum()     {     return sum; }  int transaction::get_no_friends()     {         return nr_friends;     }  bool transaction::readonetrans( istream &is )     {         >> date >> type >> name >> sum >> nr_friends;          friends = new string[nr_friends];          (int = 0; < nr_friends; i++)             {                 >> friends[i];             }          return is;         return !is.eof();     }  void transaction::writeonetrans( ostream &os )     {         os << left << setw(10) << date <<         setw(10) << type << setw(10) << name         << setw(10) << sum << setw(10)         << nr_friends;          (int = 0; < nr_friends; i++)             {                 os << left << setw(8) << friends[i];             }          os << endl;     }    transactionslist::transactionslist() {         no_trans = 1;         trans = new transaction[no_trans];  }  transactionslist::~transactionslist() {     delete [] trans; }  void transactionslist::read( istream & ) {             transaction t;              while ( t.readonetrans( ))                 {                      add( t );                 }  }  void transactionslist::print( ostream & os ) {     transaction t;      (int = 0; < no_trans; i++)         {             t = trans[i];             t.writeonetrans( os );         }      if (os == cout)         {             os << "\nnumber of transactions: " << no_trans << endl;         }  }  void transactionslist::add( transaction & t ) {    // each time read line file passed in object t here     // here want add object t dynamic array trans somehow    // , keep building array new class object every time      // overloading assignment operator somehow how?          trans[no_trans] = t;         no_trans++;   // have no idea put here make work...  } 

so can see, want continually build dynamic array trans different objects of class transaction, each instance representing different line in text file reading can print out lines in file screen in end. output lines should this:

  011216    food      john       300       2        nathan   julia 

to dynamically, realize must copy contents of object t passed in in method "add" , add array trans , somehow without losing data of earlier t:s representing previous text lines. easy me while arrays static ones, assigned next element in array trans equal current object t (inside add function). how add function looked static arrays:

 void transactionslist::add( transaction & t ) {         trans[no_trans] = t;         no_trans++; } 

obviously doesn't work when working dynamically allocated memory. read theory on , understand 1 cannot change size of array while running array has deleted , allocated larger array , copy on old contents using deep copy, doesn't copy memory address dynamic array makes new array olds content.

as can see, have read alot of theory don't understand it... can help? immensely thankful have not learned in week , killing me right now. need make progress now!

some hints container:

  1. don't use using namespace std; (why?)

  2. an unsigned integral size in c++ represented std::size_t <cstddef>.

  3. get familiar rule of three / rule of three/four/five.

  4. a quite useful idiom applied such classes is: 'resource acquisition initialization (raii)'.

bottom line:

  1. when managing resources need have

    • a destructor
    • a copy constructor
    • a move constructor
    • a copy assignment operator
    • a move assignment operator
  2. resource aquisition should happen in constructor.

    functions such add should not perform seperate resource acquisition create temporary of appropriate size , swap/move contents.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -