Correct way to access a struct in a class?

Hello everyone,

what is the correct method to access a struct in a class?

Assuming i have a class student that have a struct, as follow:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  Class Student{

    struct studentDescr
        {
           int age;
           string name;
           string grade;

        };

   //Create array student description
    studentDescr studentDescriptions[100];

public:

      //Default Constructor
        Student();


        //Destructor
        ~Student();

	//function
	void init();

	//accessor functions
	string getStudentName(int);
        string getStudentDescription(int);


private:

.... and so on...


In my student.cpp source i have the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Student.h"

//Default Constructor
Student::Student () {


}


//Destructor
Student::~Student()
{
    //dtor
}


//Accessor-Mutator Functions

void init(){

	studentDescriptions[1].name  = "Donald";
	studentDescriptions[1].description  = "A brillant student";
        
}

void initStudentDescriptions(){
        for (int i=0; i<100; i++){
            studentDescriptions[i].name="Empty";
            StudentDescriptions[i].description="Brief Description";
        }
        init();
}
}



How can i initialize and access the struct of the class in the correct way?
Where the struct should be placed?

Thank you in advance.
At the moment each student contains an array of 100 student descriptions. In order to access one such array you need to have an student object available but you can still not access it directly from outside the class because it's private.
Thank you.

OK, i will set the student description to a desidered number

 
studentDescr studentDescriptions[5];


but where the struct (and the struct initialization) should be placed?


I suppose that if i need to search through all the Students, each student objects should be inserted into a vector. Is this right?
Last edited on
Is there a reason for this design?
More natural looks:
1
2
3
4
5
6
7
8
9
class Student
{
public:
  // contructor and getters here
private:
  int age;
  string name;
  string grade;
};

Declare a vector<Student> in main or other place
Yes, you are right.

Since i need more than one description per student, can i do as follow?

1
2
3
4
5
6
7
8
9
10
class Student
{
public:
  // contructor and getters here
private:
  int age;
  string name;
  string grade;
  string description[5];
};


can i create 100 Students object with the same name and put those in a vector?
Last edited on
Instead of an array of string it's normally better to use a vector.
What is description for ?
can i create 100 Students object with the same name and put those in a vector?

Sure you can, but why 100 students with the same name ?
Thank you for the suggestions.

I may need to add an ID too

1
2
3
4
5
6
7
8
9
10
11
class Student
{
public:
  // contructor and getters here
private:
  int ID;
  int age;
  string name;
  string grade;
  string description[5];
};

@Thomas1965


Assuming i'm creating 10 students as follow:

1
2
3
4
5
Student Jon;
Student Peter;
Student Rose;
Student Robin;
...


Now, after created the students, in the main function of main.cpp, i need to populate with some data this Student object i created.

With a setter function i can do something like:
1
2
3
4
5
Jon.setDescription(descNumber, Desc) {

description[descNumber]=Desc;

}


since i can have several Students and each of them can have five descriptions, i'm forced to do this in my main function or main.cpp? Would be really confusing, wouldn't be?

There is a better way to do this?

Thank you very much



Last edited on
I would create a separate function to create all the student objects otherwise you main function becomes long and messy. Maybe like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Student
{
public:
  Student(int id, int age, const string& name, const string& grade)
  {
    this->ID = id;
    this->age = age;
    this->name = name;
    this->grade = grade;
  }
  // getters here
  void addDescription(const string& desc) { descriptions.push_back(desc); }
private:
  int ID;
  int age;
  string name;
  string grade;
  vector<string> descriptions;
};

void createStudents(vector<Student> & students)
{
  // create students and assign values
  Student Jon(1, 22, "Jon", "A");
  Jon.addDescription("Some desc");
  students.push_back(Jon);

  // and so on
}

int main()
{
  vector<Student> students;
  
  createStudents(students);
}
Thank you, nice solution.



What if i want to modify a Student description?
Last edited on
You could add a function changeDescription(const string& oldVal, const string& newVal) to the Student class. Inside this function you search for the old value in the vector and if you find it replace it with the new value, otherwise show error.
Topic archived. No new replies allowed.