Create a class GradeBook that maintains the course name as a data member so that it can be used
or modified at any time during a program's execution. The class contains member functions
setCourseName, getCourseName and displayMessage. Member function setCourseName stores a
course name in a GradeBook data member, member function getCourseName obtains a
GradeBook's course name from that data member. Member function displayMessage which now
specifies no parameters but displays a welcome message that includes the course name.
The class should also take input for student name (string) and student marks (float). The
number of students for which grades are to be entered should be specified by the user. The
program should also calculate and print the grades for students.
#include <iostream>
#include<string>
using namespace std;
class GradeBook
{
string courName;
public:
GradeBook(){}
void setCourseName()
{
cout<<"Please, enter the course name: ";
getline(cin,courName,'\n');
}
string getCourseName()
{
return courName;
}
void displayMessage()
{
cout<<"\n\t\tYou are welcome!\n";
cout<<"You are on course "<<courName;
}
void CalcGrades()
{
int num;
cout<<"\nEnter the number of students to "
<<"calculate grades: ";
cin>>num;
for(int i=0;i<num;i++)
{
float grade;
float sumGrade=0;
int j=0;
cout<<"\nEnter grades of student "<<i+1<<" (-1 - exit): ";
do
{
cin>>grade;
if(grade==-1)break;
sumGrade+=grade;
j++;
}while(true);
cout<<"Result grade of student "<<i+1<<" is "<<(float)sumGrade/j;
}
}
};
int main()
{
GradeBook gb;
gb.setCourseName();
cout<<"\nYou course: "<<gb.getCourseName();
gb.displayMessage();
gb.CalcGrades();
}
Comments
Leave a comment