Array in Classes

Hello everyone
I have some problems understanding how to declare array in a class:
under private member and public member:

#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
    string name;
    int numClass;
    char* classList;  //<-- here is where I don understand how to declare
    
public:
    void setName(string);
    void setnumClass(int);
    void setclassList(char);  //<-- not sure if I type it correctly
    string getName();
    int getnumClass();
    char getclassList[];  //<-- not sure if I type it correctly
};

void Student::setName(string n)
{
    name=n;
}
void Student::setnumClass(int nc)
{
    numClass=nc;
}
void Student::setclassList(char cl)  //<-- not sure if I type it correctly
{
    for (int x=0; x<50; x++)
    {
        cl=x;
    }
    classList=cl;
}

string Student::getName()
{
    return name;
}
int Student::getnumClass()
{
    return numClass;
}
char Student::*getclassList()   //<-- not sure if I type it correctly
{
    return getclassList();
}

int main()
{
    // insert code here...
    Student infoStudent;
    
    string LName;
    int LnumClass=0;
    char LclassList;
    
    cout<<"What is your name: "<<"\n";
    cin>>LName;
    cout<<"Enter the number of classes you are enrolled: "<<"\n";
    cin>>LnumClass;
    cout<<"Name of the classes you are enrolled: "<<"\n";
    cin>>LclassList;
    
    infoStudent.setName(LName);
    infoStudent.setnumClass(LnumClass);
    infoStudent.setclassList(LclassList);
    
    cout<<"This is your information: \n";
    cout<<infoStudent.getName()<<"\n";
    cout<<infoStudent.getnumClass()<<"\n";
    cout<<infoStudent.getclassList[0]<<"\n";
    
    
    return 0;
}
Last edited on
Firstly, thanks for using tags to format your code. However, you've used the wrong tags. I think you've used the "output" tags, not the code tags. If you use the right tags, then you get line numbers, and context colouring, which makes it easier for us to read and discuss the code.

So, looking at your code:

char* classList; //<-- here is where I don understand how to declare

Here, you've defined a pointer, not an actual array. There is no storage allocated in memory for your array anywhere in your code.

If you want to dynamically allocate memory for your array on the heap, then declaring a pointer is fine, but somewhere in your code you'll need to actually allocate that memory.

On the other hand, if you want to have a fixed-size array, then you'll need to actually declare the array.

1
2
3
4
5
6
7
8
void Student::setclassList(char cl)  //<-- not sure if I type it correctly
{
    for (int x=0; x<50; x++)
    {
        cl=x;
    }
    classList=cl;
}


This is very wrong, and I assume it doesn't actually compile. You pass in a single character to the function, but you do nothing with the value you pass in. You then overwrite the value of the character 50 times; again, you do nothing with each value before overwriting it. Finally, you try and set the value of classList to be the same as the final value of cl, but classList isn't a character, it's a pointer, so the types are incompatible.

1
2
3
4
char Student::*getclassList()   //<-- not sure if I type it correctly
{
    return getclassList();
}

This is infinite recursion. Your function calls itself, and again, and again, and again, until, eventually, the stack will overflow and your program will crash.

It looks to me as though you haven't really understood the fundamentals of arrays. I'd strongly recommend going back to your textbook and re-reading the section on arrays until it makes sense. Alternatively, you might try looking at the tutorial on this site:

http://www.cplusplus.com/doc/tutorial/arrays/

Last edited on
Mike,
Thank you much for replying back to my post.

you are right I haven't understand at all how the array works, I have an idea how they work specially the "int array[]" but what drives me crazy it is how to declare the "char array[] or string array[]" sadly haven't find much information about how this type of arrays work.

e.g.

1
2
3
4
5
6
[output]class Student
{
private:
    string name;
    int numClass;
    char classList[];[/output]


util this point I think I am ok. what I have no idea it how to pass information in between the different point/connection in the rest of the body.
e.g.
1
2
3
[output]char Student::getclassList()   //<-- how do I pass the information here
{
    return getclassL
ist();
}[/output]
Can you tell us what will the classList array will contain? I'm not clear what it represents so I don't know how to code it. Is it the names of all the classes that the student will take?
I have an idea how they work specially the "int array[]" but what drives me crazy it is how to declare the "char array[] or string array[]" sadly haven't find much information about how this type of arrays work.

The bottom line is that all arrays work the same, regardless of which type they are. An array is an indexed collection of objects of the same type. An int array is a collection of ints, a string array is a collection of strings, a char array is a collection of chars, and so on. Once you've learned how to use one type, you've learned how to use them all.

Now, in C, we use char arrays to store strings, so there's some additional behaviour built in to support that, and some additional functions in the standard library for manipulating strings.

But the basic rules on how to work with arrays as arrays (rather than strings) is the same across all types. Once you've understood one type, you've understood them all.

char classList[];[/output]

Is that really how your textbook tells you to declare an array? I doubt it.
Last edited on
Hi dhayden,

I wan the student to enter his:
Name
How many classes he/she is enrolled
Name of the classes he is enrolled <--- Here is where I need the array

Last:
I want a list of the information that we received from the student.
if he/she wants to re-enter his/her information entered previously.
MikeBoy,

Understand what you're saying but,
what about the assessor and mutators? how should I tell them to point to the array if I can't assign arrays[].
e.g.
1
2
3
4
5
6
7
8
[output]void Student::setclassList(char cl)  //<-- not sure if I type it correctly
{
    for (int x=0; x<50; x++)
    {
        cl=x;
    }
    classList=cl;
}[/output]
Since a student can enroll in a variable number of classes, I suggest a vector. Since you want to store the name of the class, the vector should hold a string:
1
2
3
4
5
6
7
8
9
#include <vector>
#include <string>

class Student
{
private:
    string name;
    std::vector<std::string> classes;  // names of the student's classes
...
Last edited on
void Student::setclassList(char cl)

Obviously, that can't be correct. You've defined your parameter to be a single character, not an array.

Also, the other problems I pointed out with that function are still there - you haven't addressed any of the issues I raised.

Seriously, stuff like:

- how to declare arrays
- how to pass them into functions
- how to access array elements

will be covered by even the most rudimentary of tutorials. Asking scatterfire questions on a forum and hoping you somehow accidentally learn all the basic facts, is not a good way of learning, and isn't a great use of our time, either. You need to go to your textbook and learn the basics. Read the section on arrays, and then reread, until you understand what it's saying.
Last edited on
dhayden
MikeBoy

Thank you very much for your help and time guys.
Topic archived. No new replies allowed.