using member function and parameterized function find grade of a student
- User
using member function and parameterized function find grade of a student
- 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
Studentclass with member functionssetName,setScore, anddisplayGrade. ThesetNameandsetScorefunctions are used to set the name and score of the student, while thedisplayGradefunction calculates and displays the grade based on the score.In the
mainfunction, the program prompts the user to enter the student's name and score. These values are then set using thesetNameandsetScoremember functions of thestudent1object. Finally, the program calls thedisplayGrademember function to calculate and display the grade.