// Fig 3.5 from D & D // Scott Emrich // More bells and whistles for our in-class example #include using std::cout; // no change using std::cin; using std::endl; #include using std::string; using std::getline; class GradeBook { public: // modify the class variable courseName. // This type of function is also called a "mutator" void setCourseName (string name) { courseName = name; } // return the class variable courseName. // This type of function is also called a "accessor" string getCourseName () { return (courseName); } // output the message as before with this member function void displayMessage() { // NOTE: although this can "see" the variable, it is good practice to // use the accessor function internally for proper encapsulation cout << "Welcome to the grade book for:\n" << getCourseName() << endl; } private: // define private variables just for this class. // although this is the default, it is good practice to set these // and public functions in two sections string courseName; // "hide" this from the main function and other classes }; int main () { string CourseName; GradeBook myGradeBook; // read in course name from the user cout << "Please enter the course name: " << endl; getline (cin, CourseName); cout << endl; // set the course name using the "accessor" function myGradeBook.setCourseName (CourseName); // display the message myGradeBook.displayMessage(); return (0); }