473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamic array of objects in C++

If I have these classes:

Expand|Select|Wrap|Line Numbers
  1. class student {
  2. private: 
  3. string * name;
  4. int faculty number;
  5. date datebirth;
  6. public:
  7. ...
  8. };
  9.  
  10. class students : public student {
  11. private:
  12. /*array with objects from class student  -  how to define it ? */
  13. int stream;
  14. string *specialty;
  15. public:
  16. void AddNew(student student1);  /* with dynamic array */
  17. void DeleteStudent(string *name); 
  18. };
  19.  
how can i write a method to add a object in a dynamic array ?
Jun 4 '07 #1
2 2880
Savage
1,764 Expert 1GB
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(string *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,student *array);

and then:

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

Savage
Jun 4 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. student s(...all of the constructor arguments...);
  2.  
  3. vector<student> students;
  4.  
  5. students.push_back(s);
  6.  
  7.  
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.
Jun 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andrew.Morgan | last post by:
Hi I have three classes TDigest, THash and TSHA1. //--------------------------------------------------------------------------- class TDigest { public: // Constructors. TDigest(int Count);
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
9
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out:...
7
by: murrayatuptowngallery | last post by:
After a few searches & inquiries here, I have a method of generating an html table populated with results of equations that works and wasn't too far up a learning curve for me, thanks to a helpful...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
2
by: headware | last post by:
Do dynamic arrays declared using ReDim have to be freed? I assume that if it's an array of dynamically created objects (e.g. Scripting.Dictionary), each one of those objects will have to be set to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.