11. Modify class GradeBook, Include a string data member for course instructor’s name, Provide a set function and a get function to change and retrieve it, Modify display Message to output the welcome message and course name.

C++ How to Program (8th Edition)

Chapter 3

Introduction to Classes, Objects and Strings

Exercise

Problem # 11:

Modify class GradeBook as follows:
a) Include a second string data member that represents the course instructor’s name.
b) Provide a set function to change the instructor’s name and a get function to retrieve it.
c) Modify the constructor to specify course name and instructor name parameters.
d) Modify function display Message to output the welcome message and course name,
then the string "This course is presented by: " followed by the instructor’s name.
Use your modified class in a test program that demonstrates the class’s new capabilities.

#include<iostream>
#include <string>
using namespace std;

class GradeBook
{
 public:
  GradeBook( string, string );
  void setCourseName( string );
  string getCourseName();
  void setInstructorName(string);
  string getInstructorName();
  void displayMessage();
 private:
  string courseName;
  string instructorName;
};
GradeBook::GradeBook( string course, string instructor )
{
 courseName=course;
 instructorName=instructor;
}
void GradeBook::setCourseName( string course)
{
 if ( course.length() <= 25 )
  courseName = course;
 if ( course.length() > 25 )
 {
  courseName = course.substr( 0, 25 );
  cout << "Name \"" << course <<"\" exceeds maximum length (25).\n"<< "Limiting courseName to first 25 characters.\n" << endl;
 }
}
void GradeBook::setInstructorName(string instructor )
{
 if ( instructor.length() <= 25 )
  instructorName = instructor;
 if ( instructor.length() > 25 )
 {
  instructorName = instructor.substr( 0, 25 );
  cout << "Name \"" << instructor <<"\" exceeds maximum length (25).\n"<< "Limiting instructorName to first 25 characters.\n" << endl;
 }
}
string GradeBook::getCourseName()
{
 return courseName;
}
string GradeBook::getInstructorName()
{
 return instructorName;
}
void GradeBook::displayMessage()
{
 cout << "Welcome to the grade book for\n" << getCourseName()<< "!" << endl;
 cout << "This course is presented by: \n" << getInstructorName()<< "." << endl;
}
int main()
{
 GradeBook gradeBook1( "CS101 Introduction to Programming in C++" ,"Sam");
 GradeBook gradeBook2( "CS102 C++ Data Structures", "Jim" );
 cout << "gradeBook1's initial course name is: "<< gradeBook1.getCourseName()<< "\ngradeBook2's initial course name is: "<< gradeBook2.getCourseName() << endl;
 cout << "gradeBook1's initial instructor name is: "<< gradeBook1.getInstructorName()<< "\ngradeBook2's initial instructor name is: "<< gradeBook2.getInstructorName() << endl;
 gradeBook1.setCourseName( "CS101 C++ Programming" );
 cout << "\ngradeBook1's course name is: "<< gradeBook1.getCourseName()<< "\ngradeBook2's course name is: "<< gradeBook2.getCourseName() << endl;
 gradeBook2.setInstructorName("David");
 gradeBook1.displayMessage();
 gradeBook2.displayMessage();
}

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 3

  1. Create an Account class that a bank might use to represent customers bank accounts. Include a data member to represent the account balance. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account. Member function get Balance should return the current balance.
  2. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members a part number, a part description, a quantity of the item being purchased and a price per item. 
  3. Create a class called Employee that includes three pieces of information as data members a first name, a last name and a monthly salary. Provide a set and a get function for each data member. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again. 
  4. Create a class called Date that includes three pieces of information as data members a month, a day and a year. Provide a set and a get function for each data member. Provide a member function display Date that displays the month, day and year separated by forward slashes (/). 

 Chapter 2


Comments