To access an array within a class in C++

Asked 2 years ago, Updated 2 years ago, 24 views

Below is part of the code. You want to add a code that creates an average from the values in the class below. First of all, what I'm curious about is how to approach the arrangement in the class, like the title. For example, if you want to access the second item in the first array containing 'Tom': '41', do you mean 'student0? Or how should I approach it?

    Student student[10] = {
        Student("Tom", 41, 85, 72), Student("Ami", 38, 80, 69),
        Student("Cathy", 65, 68, 96), Student("Mike", 22, 49, 67),
        Student("John", 51, 61, 63), Student("Alex", 87, 66, 24),
        Student("Alice", 80, 83, 71), Student("Bob", 60, 64, 52),
        Student("Colin", 90, 60, 49), Student("Dave", 31, 23, 99)};

    for(int i=0; i<10; i++) {
        int average[i] = ((student[i][1] + student[i][2] + student[i][3] / 3));
    }

Below is the code you have written so far.

class Student {
private:
    char name[15];
    int scnce, eng, math;
    int average;
public:
    Student(char * _name, int _scnce, int _eng, int _math);
    Student();
    void GetAverage();
    void ShowInfo();
   Student::Student(char * _name, int _scnce, int _eng,int _math) 
    : : scnce(_scnce), eng(_eng), math(_math) {strcpy_s(name, _name);
}


void Student::GetAverage() {
    for (int i = 0; i<10; i+ +) {
        int average[i] = stu[i]._scnce + stu[i]._eng + stu._math / 3;
    }
}
void Student::ShowInfo() {

    cout << name << "\t" << average << "\t" << scnce << "\t" << eng << "\t" << math << endl;
}

int main() {
    Student stu[] = {
        Student("1.Tom", 41, 85, 72), Student("2.Ami", 38, 80, 69),
        Student("3.Cathy", 65, 68, 96), Student("4.Mike", 22, 49, 67),
        Student("5.John", 51, 61, 63), Student("6.Alex", 87, 66, 24),
        Student("7.Alice", 80, 83, 71), Student("8.Bob", 60, 64, 52),
        Student("9.Colin", 90, 60, 49), Student("10.Dave", 31, 23, 99) };
    cout << "name" << "\t" << "average" << "\t" << "science" << "\t" << "english" << "\t" << "math" << endl;
    for (int i = 0; i<10; i++)
        stu[i].ShowInfo();
    return 0;
}


};

Additional questions. How do I solve the problem of adding the ":" colon(?) character after the name and the output doesn't fit the column like the image and the column is pushed to the right?

c++

2022-09-22 21:11

4 Answers

The array currently contains objects. The object should have a getter to get these values. You can't make the field public. In other words, the classifier must have a function such as getValue(){returnvalue;}.

Accessing elements in an array means referring to objects stored in the array. student[1].I'll get the value back through getValue(), right?

Or if you have a medium that returns the mean, it's more efficient.

If it's a data-only class, you can switch to a structure...


2022-09-22 21:11

I think we need to understand more about objects.

Each object has a different value. I mean, the grades are different for each student class.

That's what they're saying. If a student class wants to share a value, it needs to be given a static attribute

statc string schoolName = "A"

Anyway

For every method that returns the mean, the return value is int (float if necessary) It will be return (scnce+eng+math)/3.

From the main,

for (int i = 0; i<10; i++) stu[i].getavg();

It's going to be like this.


2022-09-22 21:11

for (int i = 0; i < 10; i++) average = (scnce + math + eng) / 3; Why did you do this?


2022-09-22 21:11

We're stuck here right now. It's not the average value <

Student::Student(char * _name, int _scnce, int _eng,int _math) 
    : : scnce(_scnce), eng(_eng), math(_math) {strcpy_s(name, _name);
}

int Student::GetAverage() {
    for (int i = 0; i < 10; i++)
        average = (scnce + math + eng) / 3;
    return average;
}

void Student::ShowInfo(int num) {

    cout << num << "."<< name << "\t" << average << "\t" << scnce << "\t" << eng << "\t" << math << endl;
}

int main() {
    static Student stu[] = {
        Student("Tom", 41, 85, 72), Student("Ami", 38, 80, 69),
        Student("Cathy", 65, 68, 96), Student("Mike", 22, 49, 67),
        Student("John", 51, 61, 63), Student("Alex", 87, 66, 24),
        Student("Alice", 80, 83, 71), Student("Bob", 60, 64, 52),
        Student("Colin", 90, 60, 49), Student("Dave", 31, 23, 99) };
    cout << "name" << "\t" << "average" << "\t" << "science" << "\t" << "english" << "\t" << "math" << endl;
    for (int i = 0; i < 10; i++) {
        stu[i].ShowInfo(i + 1);
        stu[i].GetAverage();
    }
    return 0;
}


2022-09-22 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.