| | |
Inserting objects into an array
Thread Solved
![]() |
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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)
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:
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):
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!!!
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)
int managercount = 0; manager::manager():staffmember() { salary = 0; managercount++; }
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:
c Syntax (Toggle Plain Text)
string object_temp; 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.. 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):
c Syntax (Toggle Plain Text)
manager::insert(staffID) { int ID_temp; ID_temp = staffID - 1; themanagers[ID_temp] = staffID // being that the staffID is also the identifier of the object }
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*
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:
Where
Vectors are similar in the fact that they too can dynamically add data, but they're really classes that are templates. For example:
And then you can add or remove nodes by using the vector's member functions
Then all you'll need to worry about is the class constructor, and not bother with an
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
c Syntax (Toggle Plain Text)
class LinkedList { public: int mydata; // etc.. LinkedList *next; };
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:
c Syntax (Toggle Plain Text)
std::vector<classtype> classvector_instance;
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."
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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: 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
***
c Syntax (Toggle Plain Text)
//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); };
***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.
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:
And then you can access any node in a vector exactly the same as you would a regular array.
c Syntax (Toggle Plain Text)
class company { // ... protected: std::vector<managers> themanagers; std::vector<casuals> thecasuals; std::vector<salesstaff> thesalesstaff; }; // eg. 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.
c Syntax (Toggle Plain Text)
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."
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
Hey mate.. Thanks for the help and patience!!I have added the correct syntax into my header file.. as you said
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..
PS: My code messed that up in the previous post.. Sorry..
c Syntax (Toggle Plain Text)
protected: std::vector themanagers; std::vector thecasuals; 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..
c Syntax (Toggle Plain Text)
string temptype; string object_name cout << "Please enter a UNIQUE name for this staff member object: "; cin >> object_name; cout << "Please enter the type of staff member you would like to create." << endl; cout << "Either 'casual', 'manager' or 'salesperson'. : "; cin >> temptype; cout << endl; if (temptype == 'casual') { casual object_name(); thecasuals.pushback(object_name); } //... Repeat else if for the manager and salesperson types..
Last edited by WolfPack; Oct 31st, 2006 at 4:57 am. Reason: Edited due to user's request
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.
Missing semicolon.
Use double quotes for a string, not singles.
Vector syntax looks good, though!
c Syntax (Toggle Plain Text)
string object_name
Missing semicolon.
c Syntax (Toggle Plain Text)
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."
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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..
Now that is calling an add_casual function which contains the following
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..
c Syntax (Toggle Plain Text)
//.. client.cpp string temptype; string object_name cout << "Please enter a UNIQUE name for this staff member object: "; cin >> object_name; cout << "Please enter the type of staff member you would like to create." << endl; cout << "Either 'casual', 'manager' or 'salesperson'. : "; cin >> temptype; cout << endl; if (temptype == 'casual') casual object_name(); thecasuals.add_casual(object_name); } //.. rest of the else ifs
Now that is calling an add_casual function which contains the following
c Syntax (Toggle Plain Text)
void company::add_casual(casual casname) { thecasuals.pushback(casname); }
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.
c Syntax (Toggle Plain Text)
if (temptype == 'casual') { // I think you missed this brace here. casual object_name; // Removed the brackets. // causal object_name() is a function called object_name that returns a causal object. // You want a instance of class causal. thecasuals.add_casual(object_name); } //.. rest of the else ifs
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Array of pointers to objects (C++)
- single array objects (Java)
- Array (Java)
- Array Problem (C++)
- Inserting a control break in an array (C++)
- Help with copying an array into a struct (C)
Other Threads in the C++ Forum
- Previous Thread: Overloading the increment operator plz help
- Next Thread: C++ Homework project, need help (simple problem)
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






