Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

using member function and parameterized function find grade of a student

  1. User

    using member function and parameterized function find grade of a student

  2. Assistant

    Here's an example of a program that uses a member function and a parameterized function to find the grade of a student based on their score:

    #include <iostream>
    
    class Student {
    private:
        std::string name;
        int score;
    
    public:
        void setName(std::string studentName) {
            name = studentName;
        }
    
        void setScore(int studentScore) {
            score = studentScore;
        }
    
        void displayGrade() {
            char grade;
            if (score >= 90) {
                grade = 'A';
            } else if (score >= 80) {
                grade = 'B';
            } else if (score >= 70) {
                grade = 'C';
            } else if (score >= 60) {
                grade = 'D';
            } else {
                grade = 'F';
            }
            std::cout << "Grade for " << name << ": " << grade << std::endl;
        }
    };
    
    int main() {
        Student student1;
        
        std::string name;
        int score;
        
        std::cout << "Enter student name: ";
        std::getline(std::cin, name);
        student1.setName(name);
        
        std::cout << "Enter student score: ";
        std::cin >> score;
        student1.setScore(score);
        
        student1.displayGrade();
        
        return 0;
    }
    

    This program defines a Student class with member functions setName, setScore, and displayGrade. The setName and setScore functions are used to set the name and score of the student, while the displayGrade function calculates and displays the grade based on the score.

    In the main function, the program prompts the user to enter the student's name and score. These values are then set using the setName and setScore member functions of the student1 object. Finally, the program calls the displayGrade member function to calculate and display the grade.