C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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:
  #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: 11
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,966 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 2 Days 52 m 24 sec
Reputation Power: 189
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: 11
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: 11
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: 11
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 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
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,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
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: 11
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: 12
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: 12
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 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
>> 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: 12
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 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
>> 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: 12
Send a message via ICQ to Gmorphus
Quote:
Originally posted by Scorpions4ever
>> 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 .


I have checked it on VC++ and you cannot get num_students from the user.
if you have succeeded please post the code - prove me wrong.
(the previous code you posted doesn't work)

Reply With Quote
  #15  
Old March 9th, 2003, 04:44 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Scorpions4ever is right, as he/she well knows, and has always been the case in my experience. It's pretty trivial to prove, but here's an example anyway (using int as the type rather than Student) compiled and run in VC++6:

Code:
#include <iostream>
using namespace std;

int main()
{
	int students = 0;
	cout<<"Enter the number of students:\n";
	cin>>students;

	int* scores = new int[students];

	for(int i=0; i<students; i++)
		scores[i] = i;
	for(int ii=0; ii<students; ii++)
		cout<<scores[ii]<<endl;

	delete [] scores;
	return 0;
}


Normally, an array size must be determinable at compile time, so the memory can be set aside, however when you're allocating memory dynamically at run time, the size of an array doesn't need to be known until run time.

Last edited by 7stud : March 9th, 2003 at 05:01 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Dynamic Object Name

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap