Correct way to access a struct in a class?
Dec 13, 2017 at 10:36am UTC
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.
Dec 13, 2017 at 10:45am UTC
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.
Dec 13, 2017 at 11:17am UTC
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 Dec 13, 2017 at 11:42am UTC
Dec 13, 2017 at 1:18pm UTC
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
Dec 13, 2017 at 3:37pm UTC
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 Dec 13, 2017 at 3:38pm UTC
Dec 13, 2017 at 3:44pm UTC
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 ?
Dec 13, 2017 at 3:58pm UTC
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];
};
Dec 14, 2017 at 1:35pm UTC
@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 Dec 14, 2017 at 2:34pm UTC
Dec 14, 2017 at 4:52pm UTC
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);
}
Dec 15, 2017 at 1:28pm UTC
Thank you, nice solution.
What if i want to modify a Student description?
Last edited on Dec 15, 2017 at 1:42pm UTC
Dec 15, 2017 at 4:01pm UTC
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.