In: Computer Science
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class.
This program has some functionality that you can use with the Gradebook class.
So, please revise Gradebook class so that the class can use sort() and display() functions of WEEK4 program .
week-4 program :
#include
#include
using namespace std;
void sort(char nm[][10]);
void display(char name[][10]);
int main() {
char names[10][10];
int i;
for (i=0; i<10; i++)
{
cout << "Enter name of the student : ";
cin >> names[i];
}
sort(names);
display(names);
return 0;
}
void sort(char nm[][10]){
char temp[10];
cout <<"Sorting names in Ascending Order " << endl;
for (int i = 0; i <10; ++i)
for (int j =i+1; j<10; j++)
{
if (strcmp(nm[i],nm[j])>0)
{
strcpy(temp,nm[i]);
strcpy(nm[i],nm[j]);
strcpy(nm[j],temp);
}
}
};
void display(char nm[][10]) {
cout <<"Displaying names in Ascending Order " <<
endl;
for (int i = 0; i <10; ++i)
{
cout<< nm[i] << " " <
}
}
--------------------------------------------------------------------------------
Gradebook class:
#include
#include // program uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
// function that sets the course name
void setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName
// function that gets the course name
string getCourseName() const
{
return courseName; // return the object's courseName
} // end function getCourseName
// function that displays a welcome message
void displayMessage() const
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
cout << "Welcome to the grade book for\n" <<
getCourseName() << "!"
<< endl;
} // end function displayMessage
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook
// function main begins program execution
int main()
{
string nameOfCourse; // string of characters to store the course
name
GradeBook myGradeBook; // create a GradeBook object named
myGradeBook
// display initial value of courseName
cout << "Initial course name is: " <<
myGradeBook.getCourseName()
<< endl;
// prompt for, input and set course name
cout << "\nPlease enter the course name:" <<
endl;
getline( cin, nameOfCourse ); // read a course name with
blanks
myGradeBook.setCourseName( nameOfCourse ); // set the course
name
cout << endl; // outputs a blank line
myGradeBook.displayMessage(); // display message with new course
name
} // end main
Thanks for the question.
Here is the completed code for this problem. Comments are
included so that you understand whats going on. Let me know if you
have any doubts or if you need anything to change.
Thank You !!
========================================================================================
#include <iostream>
#include <string>
using namespace std;
class GradeBook{
public:
void setCourseName( string name ){
courseName = name;
}
string getCourseName() const{
return courseName;
}
void displayMessage() const{
cout << "Welcome to the grade book for\n" <<
getCourseName() << "!"
<< endl;
}
// displays names of all the students in ascending
order
void displayNames(){
sortNames();
cout <<"Displaying names in Ascending Order "
<< endl;
for (int i = 0; i <10; ++i){
cout<<"Student
"<<i+1<<" is: "<< names[i] <<endl;
}
}
// ask user for sstudent name and store the names in the
array
void readNames(){
for(int i=0; i<10;i++){
cout<<"Enter student name:
";
cin>>names[i];
}
}
private:
string courseName;
string names[10]; // string array of size 10
// private sort function that sorts the names in the string
array
void sortNames(){
string temp;
for (int i = 0; i <10;
++i)
for (int j =i+1;
j<10; j++)
{
if
(names[i].compare(names[j])>0)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
}
};
int main(){
string nameOfCourse;
GradeBook myGradeBook;
cout << "Initial course name is: " << myGradeBook.getCourseName()<< endl;
cout << "\nPlease enter the course name:" <<
endl;
getline( cin, nameOfCourse );
myGradeBook.setCourseName( nameOfCourse );
cout << endl;
myGradeBook.displayMessage();
myGradeBook.readNames();
myGradeBook.displayNames();
}
===========================================================================================