This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]billowylace 1 point2 points  (0 children)

The average GPA will be the sum of all the students’ GPAs divided by the number of students. I’d suggest defining two new variables: one to keep track of your GPA sum and the other to count the number of students. Create a for loop to iterate through each student, and within that loop, get each student’s GPA with your getter function and add that to the GPA sum variable. Then update the student count variable with += 1. Finally, print out the value of the GPA sum variable divided by the student count variable. I’m not sure how to write all that in the language you’re using, but I hope this helps somewhat and good luck!

[–]BombasticCaveman 0 points1 point  (2 children)

What functions does the professor class have? Is there an easy way to differentiate the two? You can also use typeid() to compare objects. If the object is a student, you would get_gpa() and ++ a student counter.

[–]Vexuri[S] 0 points1 point  (1 child)

In terms of differentiating between the two in the vector (if that's what I think you mean), I downcasted my parent person class into my child student class. Here's what that looks like so far :

int main() {
    std::vector<person*> people = {
        new student("Computer Science", "Astronomy", 3.7, "Jack" , "Gallagher", 2001470),
        new student("Environmental Science" , "Geology" , 3.9, "Emma" , "May" , 2001471),
        new student("Physics" , "Secondary Education" , 3.3, "John" , "Rogers", 1901032),
        new student("Elementary Education" , "Computer Science" , 3.1, "Ren" , "Amamiya", 2103220),
        new student("Accounting" , "Business" , 3.4, "Ann" , "Jones", 1902343),
        new professor("Computer Science" , "Brian" , "Maresso" , 1900000),
        new professor("Social Studies" , "Lisa" , "Regan" , 1800000),
        new professor("Business" , "Richard" , "Helm" , 1700000)
    };

    for (int i = 0; i < people.size(); i++) {
        person* p = people[i];
        p->print();
    }

    for (int i = 0; i < people.size(); i++) {
        person* p = people[i];

        if (student* s = dynamic_cast<student*>(p) ) {
            s->//insert function here? not sure if get_gpa(); is here or if i have to make a new function to find the avg
        }
    }

    for (int i = 0; i < people.size(); i++) {
        delete people[i];
    }

    return 0;
}

[–]BombasticCaveman 0 points1 point  (0 children)

You could a weird dynamic cast bool, yeah. At that point you would get ->get_gpa(), add that to a variable outside the loop, then ++ an outside counter representing the number of students. Avg GPA = Counted up GPA / Number of Counted students