Inserting objects into an array

Thread Solved
Reply

Join Date: Oct 2006
Posts: 7
Reputation: Jonezy is an unknown quantity at this point 
Solved Threads: 0
Jonezy Jonezy is offline Offline
Newbie Poster

Inserting objects into an array

 
0
  #1
Oct 30th, 2006
Hey.. Names Jay..

I'm new around here.. Doin C++ and i'm a beginner programmer.. I have been going OK but i am stuck!!

For this assessment i have to create a payroll system for a company..

The company must contain staffmembers.. There is a class for staffmembers.. Inheriting frmo this staffmember class, are 3 other classes, (Manager, Casual, Salesperson).

In the company class, these 3 types of staffmembers must be stored in an array..

Using the interface we must create, we must be able to insert a users ID, and from their, be able to edit the specific staffmembers details..

My Dillemma:

How to add the members into their correct array after construction:

(The following is my constructor for a manager)


  1.  
  2. int managercount = 0;
  3. manager::manager():staffmember()
  4. {
  5. salary = 0;
  6. managercount++;
  7. }

Now i FIGURE i could somehow insert the manager into the themanagers[] array from my company class.. But i am not sure how..

MY IDEA: considering the interface will be used to create these members of staff.. I could create each object using a number system, such as there staff id.. **I am aware i would need to convert that from into to string.. or maybe char? I'm not sure** Therefore: it would be something SIMILAR to below.. However i don't know the correct syntax:

  1. string object_temp;
  2. object_temp = staffID //.. staffID is a static int counting how many staff members have been created and then giving the next number to the next staffmember as their staff ID..
  3.  
  4. manager object_temp();

If i'm correct, with the correct syntax.. This would make it possible to create an insert function which looked something SIMILAR too (again my syntax is lame because of my lack of knowledge regarding the language so far):

  1.  
  2. manager::insert(staffID)
  3. {
  4. int ID_temp;
  5. ID_temp = staffID - 1;
  6.  
  7. themanagers[ID_temp] = staffID // being that the staffID is also the identifier of the object
  8.  
  9. }

If anyone has an EASIER idea.. Please contact me!! Or if you can help with any of the syntax here i'd be thankful.. Or even a resource already available which may help me?

I apologise for the long post and i also apologise if i have caused anybody discomfort for having to try and understand my jibberish!!
THANK YOU!!!
Last edited by Jonezy; Oct 30th, 2006 at 10:32 pm. Reason: Realised i forgot to add some stuff.. *blushes*
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Inserting objects into an array

 
0
  #2
Oct 30th, 2006
Do you mean add new members to an array that's already been declared? That's not possible. You'll either have to use vectors or a linked list. Linked lists look like this:
  1. class LinkedList {
  2. public:
  3. int mydata;
  4. // etc..
  5.  
  6. LinkedList *next;
  7. };

Where next points to the next node in the structure.

Vectors are similar in the fact that they too can dynamically add data, but they're really classes that are templates. For example:
  1. std::vector<classtype> classvector_instance;
And then you can add or remove nodes by using the vector's member functions push and pop, respectively.

Then all you'll need to worry about is the class constructor, and not bother with an insert member function.

More information on linked lists and vectors:
http://www.fortunecity.com/skyscrape.../linklist.html
http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4027/

Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: Jonezy is an unknown quantity at this point 
Solved Threads: 0
Jonezy Jonezy is offline Offline
Newbie Poster

Re: Inserting objects into an array

 
0
  #3
Oct 31st, 2006
Awesome.. Thank you!!I'm working on the idea of using vectors.. I THINK i have the concept behind it, however, i'm not sure where to put each part of the vectors.. For example, would it go in my header file as follows:
  1. //company.hclass company{ public: //..functions go here protected: vector themanagers(20); // making vectors with room for 20, 50 and 50 in order vector thecasuals(50); vector thesalesstaff(50); };
And then to use these in there respective class, i am sorta lost after that.. Do i need an iterator (if so how/what does this do).. And then where do i put the syntax to put the managers into the vector? I have a feeling i've turned something simple into something confusing.. Please help me fix that.. lol..Thanks..
***Please excuse the code bein all messed up.. I can't seem to get it to go normal ***
Last edited by Jonezy; Oct 31st, 2006 at 12:22 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Inserting objects into an array

 
0
  #4
Oct 31st, 2006
No, the syntax of vectors is different, and you don't need to declare how many nodes are going to be in it (although you can, but in this case it's kind of pointless), you just add them as you ned them:

  1.  
  2. class company {
  3.  
  4. // ...
  5.  
  6.  
  7.  
  8. protected:
  9.  
  10. std::vector<managers> themanagers;
  11.  
  12. std::vector<casuals> thecasuals;
  13.  
  14. std::vector<salesstaff> thesalesstaff;
  15.  
  16. };
  17.  
  18.  
  19.  
  20. // eg.
  21.  
  22. themanagers.push_back(/*initalization data for constructor*/);



And then you can access any node in a vector exactly the same as you would a regular array.

  1. themanagers[index_var].member_data = whatever;
Last edited by John A; Oct 31st, 2006 at 12:32 am. Reason: My post won't show up!
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: Jonezy is an unknown quantity at this point 
Solved Threads: 0
Jonezy Jonezy is offline Offline
Newbie Poster

Re: Inserting objects into an array

 
0
  #5
Oct 31st, 2006
Hey mate.. Thanks for the help and patience!!I have added the correct syntax into my header file.. as you said
  1. protected:
  2. std::vector themanagers;
  3. std::vector thecasuals;
  4. std::vector thesalesstaff;

Now in my interface, i want to make it so that, a staffmember can be created, and then it will be added to the vector.. Below is how i ATTEMPTED to do this..
  1. string temptype;
  2. string object_name
  3. cout << "Please enter a UNIQUE name for this staff member object: ";
  4. cin >> object_name;
  5. cout << "Please enter the type of staff member you would like to create." << endl;
  6. cout << "Either 'casual', 'manager' or 'salesperson'. : ";
  7. cin >> temptype;
  8. cout << endl;
  9. if (temptype == 'casual')
  10. {
  11. casual object_name();
  12. thecasuals.pushback(object_name);
  13. }
  14. //... Repeat else if for the manager and salesperson types..
PS: My code messed that up in the previous post.. Sorry..
Last edited by WolfPack; Oct 31st, 2006 at 4:57 am. Reason: Edited due to user's request
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Inserting objects into an array

 
0
  #6
Oct 31st, 2006
Sorry about the late(ish) reply! I had this post written, and then my computer messed up, so I wasn't able to post it.
  1. string object_name

Missing semicolon.

  1. if (temptype == 'casual')

Use double quotes for a string, not singles.

Vector syntax looks good, though!
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: Jonezy is an unknown quantity at this point 
Solved Threads: 0
Jonezy Jonezy is offline Offline
Newbie Poster

Re: Inserting objects into an array

 
0
  #7
Nov 1st, 2006
Sorry for MY delay on my OWN forum post.. I have been at a job interview all day... Ok i believe i've made progress.. With one compiler error.. I'm not sure why the compiler error is happening.. therefore imma post it here in hopes it is understood..

  1.  
  2. //.. client.cpp
  3.  
  4. string temptype;
  5. string object_name
  6. cout << "Please enter a UNIQUE name for this staff member object: ";
  7. cin >> object_name;
  8. cout << "Please enter the type of staff member you would like to create." << endl;
  9. cout << "Either 'casual', 'manager' or 'salesperson'. : ";
  10. cin >> temptype;
  11. cout << endl;
  12. if (temptype == 'casual')
  13. casual object_name();
  14. thecasuals.add_casual(object_name);
  15. }
  16. //.. rest of the else ifs

Now that is calling an add_casual function which contains the following

  1.  
  2. void company::add_casual(casual casname)
  3. {
  4. thecasuals.pushback(casname);
  5. }


All SEEMS GREAT except 1 error in compiling.. Which says

error C2664: 'add_asual' : annot onvert parameter 1 from 'class casual (void)' to 'class casual'
No constructoer could take the source type, or constructor overload resolution was ambiguous

I don't know why.... I just really need to get this done.. Due friday before 5pm.. It is currently 8:27pm wednesday.. And i've been working on it for weeks lol.. So difficult..

Thanks for any help again..
Last edited by Jonezy; Nov 1st, 2006 at 5:34 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Inserting objects into an array

 
0
  #8
Nov 1st, 2006
  1. if (temptype == 'casual')
  2. { // I think you missed this brace here.
  3. casual object_name; // Removed the brackets.
  4. // causal object_name() is a function called object_name that returns a causal object.
  5. // You want a instance of class causal.
  6. thecasuals.add_casual(object_name);
  7. }
  8. //.. rest of the else ifs
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,754
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 294
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Maven

Re: Inserting objects into an array

 
0
  #9
Nov 1st, 2006
And you should read mr. joe's post again:
if (temptype == 'casual') => if (temptype == "casual")
string object_name => string object_name;
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: Jonezy is an unknown quantity at this point 
Solved Threads: 0
Jonezy Jonezy is offline Offline
Newbie Poster

Re: Inserting objects into an array

 
0
  #10
Nov 1st, 2006
Ahh yes!! Thank you!! lol!! All that for some brackets..

Well i got it compiled.. Now i need to work out how to access all the 'casuals' in this vector.. to print all their details, eg, staff_id and functions.. And the rest of it.. Thank you both for your help!! If i get stuck i will post a new forum post tonight/tomorow some time.. I will be working on it for the next 48 hours straight!!

Oh and thanks niek.. But i had already fixed them but all my code is on my laptop and i simply copy and pasted snippets from my previous posts.. So no need to seem attitudey

Marking as solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC