943,970 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4421
  • C++ RSS
Oct 30th, 2006
0

Inserting objects into an array

Expand Post »
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)


c++ Syntax (Toggle Plain Text)
  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*
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jonezy is offline Offline
7 posts
since Oct 2006
Oct 30th, 2006
0

Re: Inserting objects into an array

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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Oct 31st, 2006
0

Re: Inserting objects into an array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jonezy is offline Offline
7 posts
since Oct 2006
Oct 31st, 2006
0

Re: Inserting objects into an array

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!
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Oct 31st, 2006
0

Re: Inserting objects into an array

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jonezy is offline Offline
7 posts
since Oct 2006
Oct 31st, 2006
0

Re: Inserting objects into an array

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!
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 1st, 2006
0

Re: Inserting objects into an array

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jonezy is offline Offline
7 posts
since Oct 2006
Nov 1st, 2006
0

Re: Inserting objects into an array

  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
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 1st, 2006
0

Re: Inserting objects into an array

And you should read mr. joe's post again:
Quote ...
if (temptype == 'casual') => if (temptype == "casual")
string object_name => string object_name;
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 1st, 2006
0

Re: Inserting objects into an array

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jonezy is offline Offline
7 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Overloading the increment operator plz help
Next Thread in C++ Forum Timeline: C++ Homework project, need help (simple problem)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC