473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic array of objects in C++

4 New Member
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 2932
Savage
1,764 Recognized Expert Top Contributor
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
Jun 4 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
3046
by: Andrew.Morgan | last post by:
Hi I have three classes TDigest, THash and TSHA1. //--------------------------------------------------------------------------- class TDigest { public: // Constructors. TDigest(int Count);
1
17684
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
8
3691
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
1
4457
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 array of objects. I am skipping initialization and other member functions. class Inventory { Item *itemList; int idx, count, size;
9
17631
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: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> .NET Framework Class Library Control.Init Event
7
1873
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 respondent/poster here... <script type="text/javascript"> var AAA = new Array(4) AAA = 2*0.1*Math.round(10*Math.log(32)/Math.log(2)) AAA = 2*0.1*Math.round(10*Math.log(16)/Math.log(2))
23
7424
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
4
51681
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 controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user controls. Background Please familiarize yourself with the ASP.NET Page Life Cycle. It is crucial to...
2
2126
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 Nothing in a for loop, but what about the array itself? Or what if the array is dynamic but it just consists of integers or strings, not dynamically allocated objects? Does it have to be set to Nothing? Thanks, Dave
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.