A QUICK C++ Question

Kenji4861

Banned
Jan 28, 2001
2,821
0
0
I have a class say, it's called student
and the student has

int grades[20];
int GPA;
char grade;

and say I have it declared by
studentA = *new student();

If I want to change the array size of the grades array? How can I do that?
Thanks!
 

bUnMaNGo

Senior member
Feb 9, 2000
964
0
0
well I guess you can have something like int *grades; and int arraysize; and before you create a new student, prompt for the grade array size and set it dynamically? like in the constructor set grades = new int[arraysize] or something like that? I forget the exact syntax. I don't know if it's what you want though... sounds like you want to be able to change the size and insert more grades as time goes on.
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
I'd definetly use a vector, it is a lot smarter, and can be updated as your data size grows, rather than having to recompile.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
make this line:
int grades[20];
into this line:
int grades[200];


only way, otherwise you've got a lot more work than just a simple array.
Either that, or use a language with dynamic arrays :)
 

Kenji4861

Banned
Jan 28, 2001
2,821
0
0
Someone clued me in class that I'm suppose to "delete" the array portion and just declare the array portion. I'm not quite sure if that's possible.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
just declare the grades array as a pointer and malloc the size of memory you need for the array like this:




class student{

public:

int * grades
double GPA
char GRADE


}



student * studentA = new Student();


studentA->grades = new int[SIZE_YOU_WANT];




you could also overload the constructer to make it nice and let the constructor take the number of grades the student got.



a vector is not the answer, learn your pointers and dynamic allocation.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0


<< Someone clued me in class that I'm suppose to "delete" the array portion and just declare the array portion. I'm not quite sure if that's possible. >>



you cannot deleted a staticlly allocated array only a dyanmically allocated one.
 

crypticlogin

Diamond Member
Feb 6, 2001
4,047
0
0


<< a vector is not the answer, learn your pointers and dynamic allocation. >>


I think that would depend on how often, if at all, the array would need to be adjusted, and how the grades are entered (do I know how many grades there are beforehand?). Since Kenji4861 hasn't made that clear, I think either way is acceptable until more of the program is revealed.

edit: and I'd consider making some of those variables private, but hey that's just me.