Nested Student Grades

Nested Student-Grades

Define a Student class with a nested enum Grade representing letter grades (A, B, C, D, F). Add member variables to the Student class for name, ID, and a nested Grade enum for storing grades in subjects like math, science, and history. Write methods to set and get the grades. Demonstrate the use of the nested Grade enum by creating instances of Student and setting/getting grades. You should also include a static grade_to_string(Grade grade) function.

Answer

#include <iostream>
#include <string>

class Student {
public:
    // Nested enum Grade representing letter grades
    enum Grade { A, B, C, D, F };

private:
    std::string name;
    int id;
    Grade math_grade;
    Grade science_grade;
    Grade history_grade;

public:
    // Constructor to initialize name and id
    Student(const std::string& student_name, int student_id)
        : name(student_name), id(student_id), math_grade(F), science_grade(F), history_grade(F) {}

    // Setter for math grade
    void set_math_grade(Grade grade) {
        math_grade = grade;
    }

    // Getter for math grade
    Grade get_math_grade() const {
        return math_grade;
    }

    // Setter for science grade
    void set_science_grade(Grade grade) {
        science_grade = grade;
    }

    // Getter for science grade
    Grade get_science_grade() const {
        return science_grade;
    }

    // Setter for history grade
    void set_history_grade(Grade grade) {
        history_grade = grade;
    }

    // Getter for history grade
    Grade get_history_grade() const {
        return history_grade;
    }

    // Static method to convert Grade enum to string
    static std::string grade_to_string(Grade grade) {
        switch (grade) {
        case A: return "A";
        case B: return "B";
        case C: return "C";
        case D: return "D";
        case F: return "F";
        default: return "Unknown";
        }
    }

    // Method to print student details
    void print_details() const {
        std::cout << "Name: " << name << ", ID: " << id << std::endl;
        std::cout << "Math Grade: " << grade_to_string(math_grade) << std::endl;
        std::cout << "Science Grade: " << grade_to_string(science_grade) << std::endl;
        std::cout << "History Grade: " << grade_to_string(history_grade) << std::endl;
    }
};

int main() {
    // Create instances of Student
    Student student1("Alice", 101);
    Student student2("Bob", 102);

    // Set grades for student1
    student1.set_math_grade(Student::A);
    student1.set_science_grade(Student::B);
    student1.set_history_grade(Student::C);

    // Set grades for student2
    student2.set_math_grade(Student::B);
    student2.set_science_grade(Student::C);
    student2.set_history_grade(Student::A);

    // Print student details
    std::cout << "Student 1 Details:" << std::endl;
    student1.print_details();
    std::cout << std::endl;

    std::cout << "Student 2 Details:" << std::endl;
    student2.print_details();

    return 0;
}
Questions