c++ - Class array with multiple types? How to access one type in the array? -
here's small portion of code:
class student { private: string id, fname, lname, level; double gpa; }
so let's make array a[] of type student, there way access double 'gpa' of each array element? not sure how this. appreciated. need learn how google better because feel shouldn't hard still couldn't find looking for.
if have array
std::array<student, 10> students;
you say
for (auto const& student : students) { std::cout << student.gpa << " "; }
you can arbitrary element well
students[i].gpa = 4.0;
of course have make sure have public
methods access these members if specified private
class student { public student() = default; student(double _gpa) : gpa(_gpa) {} double get_gpa() const { return gpa; } void set_gpa(double _gpa) { gpa = _gpa; } private: double gpa = 0.0; }
then
students[i].set_gpa(4.0);
Comments
Post a Comment