Create a Grade Book class that contains instance variables for the course name,
ID: 3855388 • Letter: C
Question
Create a Grade Book class that contains instance variables for the course name, number of credits, room #, and student capacity. Create mutator/accessor (or commonly known as getter's/setter's) methods for each of the instance variables.
Create a driver program that creates a grade book and presents the user with a menu as follows:
<1> View Grade Book
<2> Set Course Name
<3> Set Number of Credits
<4> Set Room #
<5> Set Capacity
<6> Quit the program
Make all instance variables private.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class GradeBook
{
string cname;
int no_of_credits,room,scapacity;
public:
GradeBook()
{
cname="Civil";
no_of_credits=5;
room=101;
scapacity=100;
}
void setCourseName(string n)
{
cname=n;
}
void noOfCredits(int n)
{
no_of_credits=n;
}
void setRoom(int n)
{
room=n;
}
void setStudentcapacity(int n)
{
scapacity=n;
}
string getCourseName()
{
return cname;
}
int getnoOfCredits()
{
return no_of_credits;
}
int getRoom()
{
return room;
}
int getStudentcapacity()
{
return scapacity;
}
void Print()
{
cout<<"Course name "<<cname<<endl;
cout<<"Number of Credits "<<no_of_credits<<endl;
cout<<"Room "<<room<<endl;
cout<<"Capacity "<<scapacity<<endl;
}
};
int main(int argc, char const *argv[])
{GradeBook o;
while(1)
{
cout<<"1. View Grade Book 2. Set Course Name 3. Set Number of Credits 4. Set Room # 5. Set Capacity 6. Quit the program ";
int ch;
cin>>ch;
switch(ch)
{
case 1:o.Print();break;
case 2:o.setCourseName("Computer");break;
case 3:o.noOfCredits(36);break;
case 4:o.setRoom(121);break;
case 5:o.setStudentcapacity(150);break;
case 6:return 0;
}
}
return 0;
}
========================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ gra.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. View Grade Book
2. Set Course Name
3. Set Number of Credits
4. Set Room #
5. Set Capacity
6. Quit the program
1
Course name Civil
Number of Credits 5
Room 101
Capacity 100
1. View Grade Book
2. Set Course Name
3. Set Number of Credits
4. Set Room #
5. Set Capacity
6. Quit the program
2
1. View Grade Book
2. Set Course Name
3. Set Number of Credits
4. Set Room #
5. Set Capacity
6. Quit the program
1
Course name Computer
Number of Credits 5
Room 101
Capacity 100
1. View Grade Book
2. Set Course Name
3. Set Number of Credits
4. Set Room #
5. Set Capacity
6. Quit the program
6