Student Class

Student Class

Define a Student struct with private member variables name (string), id (int), and grades(EGrade[ESubject::MAX]).

grade is an array of EGrade values. The enum EGrade contains all letter grades A-F. ESubject is an enum that holds the name of all subjects. Its last element is MAX, which is used to determine the size of the grades array.

Add public getter and setter functions for the grades. Create an instance of Student, set the values, and print them using the getter functions. The class should also have a public interface that lets you get the average grade and set & get the grade for a subject. The grade should be an enum, and the grade member variable should be of a StudentGrades struct that contains the subject grades declared as the enum.

Hint

Use enums for EGrade and ESubject to represent grades and subjects.

Use a loop to calculate the average grade from the grades array.

Answer

#include <iostream>
#include <string>

enum class EGrade { A, B, C, D, E, F };
enum class ESubject { MATH = 0, ENGLISH, HISTORY, SCIENCE, MAX };

struct Student {
private:
    std::string name;
    int id;
    EGrade grades[static_cast<int>(ESubject::MAX)];

public:
    // Getter and setter for name
    std::string get_name() const { return name; }
    void set_name(const std::string& new_name) { name = new_name; }

    // Getter and setter for id
    int get_id() const { return id; }
    void set_id(int new_id) { id = new_id; }

    // Getter and setter for grades
    EGrade get_grade(ESubject subject) const { return grades[static_cast<int>(subject)]; }
    void set_grade(ESubject subject, EGrade grade) { grades[static_cast<int>(subject)] = grade; }
- Define a Car struct with private member variables make, model, year, and mileage. Add public getter and setter functions for these variables. Ensure the setter function for mileage prevents the mileage from being set to a lower value than the current mileage. Demonstrate the use of these functions in a program.

    // Calculate average grade
    float get_average_grade() const {
        int total = 0;
        for (int i = 0; i < static_cast<int>(ESubject::MAX); ++i) {
            total += static_cast<int>(grades.grades[i]);
        }
        return static_cast<float>(total) / static_cast<int>(ESubject::MAX);
    }

    // Print student information
    void print_info() const {
        std::cout << "Name: " << name << std::endl;
        std::cout << "ID: " << id << std::endl;
        std::cout << "Grades:" << std::endl;
        for (int i = 0; i < static_cast<int>(ESubject::MAX); ++i) {
            std::cout << "  Subject " << i << ": " << static_cast<int>(grades.grades[i]) << std::endl;
        }
    }
};

int main() {
    Student student;
    student.set_name("John Doe");
    student.set_id(12345);

    student.set_grade(ESubject::MATH, EGrade::A);
    student.set_grade(ESubject::ENGLISH, EGrade::B);
    student.set_grade(ESubject::HISTORY, EGrade::C);
    student.set_grade(ESubject::SCIENCE, EGrade::D);

    student.print_info();

    std::cout << "Average Grade: " << student.get_average_grade() << std::endl;

    return 0;
}
Questions