dynamic array of objects in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • him1979
    New Member
    • Jun 2007
    • 4

    dynamic array of objects in C++

    If I have these classes:

    [code=cpp]class student {
    private:
    string * name;
    int faculty number;
    date datebirth;
    public:
    ...
    };

    class students : public student {
    private:
    /*array with objects from class student - how to define it ? */
    int stream;
    string *specialty;
    public:
    void AddNew(student student1); /* with dynamic array */
    void DeleteStudent(s tring *name);
    };
    [/code]

    how can i write a method to add a object in a dynamic array ?
    Last edited by AdrianH; Jun 4 '07, 04:55 PM. Reason: Please use [code=cpp][/code] tags to improve readablility. Also don't forget to INDENT!!!!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by him1979
    If I have these classes:

    class student {
    private:
    string * name;
    int faculty number;
    date datebirth;
    public:
    ...
    };

    class students : public student {
    private:
    /*array with objects from class student - how to define it ? */
    int stream;
    string *specialty;
    public:
    void AddNew(student student1); /* with dynamic array */
    void DeleteStudent(s tring *name);
    };

    how can i write a method to add a object in a dynamic array ?
    /*array with objects from class student - how to define it ? */,I'm not quite sure that i understand u well,can u rephrase ur question?

    how can i write a method to add a object in a dynamic array ?

    U will need to find current number of elements in ur dynamic array,and increase it for one.

    e.g
    void AddNew(student student1,studen t *array);

    and then:

    array=new array[((sizeof(array)/sizeof(student) )+1]

    Savage

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The problem with using an array is that it is fixed in size. If your students class has a dynamic array, then the array has to be created to a given size.

      Once you do that, you have to keep track of how many student objects are in the array. AND when the array fills up, you have to create a bigger array and copy the old array to the new array and then delete the old array.

      Pant. Pant. Pant.

      C++ implements a container called vector that implements your array and keeps track of all that stuff about how many students are in the array and to expand the array if needed.

      All you have to do is create a student and add it to the vector:

      [code=cpp]
      student s(...all of the constructor arguments...);

      vector<student> students;

      students.push_b ack(s);

      [/code]

      and you are done.

      If you have to use your own array, be prepared for substantial code that essentially duplicates the code for the vector.

      Comment

      Working...