SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 9th, 2003, 10:48 AM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Dynamic Object Name

Hi all, looking for a little help with dynamic object names.

Eg, i have a class for students. When the system is run, the user will be asked how many students they require, the program can then create X instances of the student class,

student1, student2... studentX etc.

Im just wondering how todo this. Is Student student[i](); correct for a constructor? If not, how do I do it ?

Cheers
Taz

Reply With Quote
  #2  
Old March 9th, 2003, 10:56 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
the constructor should remain "standard":
Student();

to create the objects:
Code:
Student student[];
...
for (i=0;i<students_number;i++) {
  student[i]=new Student();
}
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old March 9th, 2003, 11:02 AM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
thank you very much young sir, will report back when i balls it up

Cheers
Taz

Reply With Quote
  #4  
Old March 9th, 2003, 11:07 AM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
one thing, I need to pass 2 arguments to my constructor.

So does...
PHP Code:
 Student student[];
...
for (
i=0;i<students_number;i++) {
  
student[i]=new Student();



become
PHP Code:
 Student student[];
...
for (
i=0;i<students_number;i++) {
  
student[i]=new Student([b]MY ARGUMENTS?[/b]);


Reply With Quote
  #5  
Old March 9th, 2003, 11:16 AM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
right, on compilng the following code I get the following errors..

Quote:
int num_students = 2;
Students students[];

// Generate required number of monkey objects
int i;
for(i = 0; i < num_students; i++)
{
students[i]=new Students(5,3);
}


Errors:
Quote:
stage3.cc: In function `int main()':
stage3.cc:24: array size missing in `students
stage3.cc:24: no matching function for call to `Students::Students()'
monkey.h:14: candidates are: Students::Students(const Students&)
students.h:25: Students::Students(int, int)
stage3.cc:30: no match for `Students& = Students*&' operator
students.h:14: candidates are: Students& Students::operator=(const Students&)


Side note. The class works when calling normally, eg Students students(5,3);

Reply With Quote
  #6  
Old March 9th, 2003, 01:34 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
Try declaring your array like this:
Code:
int num_students = 2;
Student *students = new Student[num_students](5,3);
... 
... do your stuff
...
delete [] students;

Reply With Quote
  #7  
Old March 9th, 2003, 01:36 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
From your error message it looks like you don't have a constructor defined that takes two arguments, but that is the way you would call the constructor. A constructor is like any other function except it doesn't and can't have a return type, and when you call any function, the parameters must match up with a function definition.

student[i]=new Student(MY ARGUMENTS?);
---------------

Student student[];******what size array did you create here?

for (i=0;i<students_number;i++) {
student[i]=new Student();
}

Last edited by 7stud : March 9th, 2003 at 02:06 PM.

Reply With Quote
  #8  
Old March 9th, 2003, 02:25 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Hi guys, the following fixed it, required pointers. Note, student is now monkey

Regards

Quote:
// Create pointer array
int num_monkeys = 2;
Monkey *monkey[num_monkeys];

// Generate required number of monkey objects
for(int i = 0; i < num_monkeys; i++)
{
monkey[i] = new Monkey(5,3);
}

Reply With Quote
  #9  
Old March 9th, 2003, 03:05 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
7stud was right. some clarifications.
What you are trying to do, as you probably know, is to dynamically allocate an array of

Student.
You have to use pointers since you don't know the size of the array.

before that you have some problems with the constructor.
let's take the following class:
PHP Code:
class Student
{
public:
    
Student(int iint j)
    {
        
nClassID i;
        
nStudentID j;
    }
private:
    
int nClassID;
    
int nStudentID;



let's say you want to retrieve the number of the students, as well as the IDs, from the user

and than construct these instances.
This is the wrong way:
PHP Code:
 int nNumberOfStudents;
Student *pStudentsArray;
// get the number of students from the user and put it in nNumberOfStudents
// than dynamically allocate the array
pStudentsArray = new Student[nNumberOfStudents]; 


why? because when you try to allocare the "new Student[..]" the program tries to construct

these instances.

you have to possibilities as far as I know:
1. Use pointer to pointer (I will explain)
2. Add a function "Create(int i, int j)" and empty the constructor.

Explaination:
1. Instead of allocating an array of students we allocate an array of pointers to Student
PHP Code:
 typedef Student *PStudent;
// get number of students from the user
PStudent *ppStudentsArray = new PStudent[nNumberOfStudents];
for (
int i=0i<nNumberOfStudentsi++)
{
    
int nClsIDnStudentID;
    
//get values from the user to nClsID and nStudentID
    
ppStudentsArray[i] = new Student(nClsIDnStudentID);



2. Add Create(int, int) that does the same thing as the constructor. make the constructor do

NOTHING and do the following:
PHP Code:
// get number of students from the user
Student *pStudentsArray = new Student[nNumberOfStudents];
for (
int i=0i<nNumberOfStudentsi++)
{
    
int nClsIDnStudentID;
    
//get values from the user to nClsID and nStudentID
    
pStudentsArray[i].Create(nClsIDnStudentID);

__________________
"Gravitation can NOT be responsible for people falling in Love"
(one of the most significant characters in the history, can you guess?)

Gmorph.

Reply With Quote
  #10  
Old March 9th, 2003, 03:06 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
Your solution is good only if the number of monkeys is constant - but if you want to get this and the other values from the user it wont work i suspect.

Reply With Quote
  #11  
Old March 9th, 2003, 03:09 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
>> 2. Add Create(int, int) that does the same thing as the constructor. make the constructor do nothing
Or, you could use my example posted above and use the constructor just fine. BTW I tested my example with g++ before posting it here .

Reply With Quote
  #12  
Old March 9th, 2003, 03:18 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
It would work - but i'm not sure it will satisfy all the requirements:
1. The number of Elements is CONSTANT and not dynamic
2. What if you want to construct the each element with different values?!

Reply With Quote
  #13  
Old March 9th, 2003, 03:27 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
>> 1. The number of Elements is CONSTANT and not dynamic
Nope, you can get num_students from user input.

>> 2. What if you want to construct the each element with different values?!
Then you'd have to have a constructor with no arguments and set the values inside a for loop, like your example did .

Reply With Quote
  #14  
Old March 9th, 2003, 03:47 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7