3.11
GradeBook类定义:
#include
// constructor initializes course name and instructor name GradeBook( string, string );
void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void setInstructorName( string ); // function to set instructor name string getInstructorName(); // function to retrieve instructor name void displayMessage(); // display welcome message and instructor name private:
string courseName; // course name for this GradeBook string instructorName; // instructor name for this GradeBook }; // end class GradeBook 类成员函数:
#include
// constructor initializes courseName and instructorName // with strings supplied as arguments
GradeBook::GradeBook( string course, string instructor ) {
setCourseName( course ); // initializes courseName
setInstructorName( instructor ); // initialiZes instructorName } // end GradeBook constructor // function to set the course name
void GradeBook::setCourseName( string name ) {
courseName = name; // store the course name } // end function setCourseName
// function to retrieve the course name string GradeBook::getCourseName() { return courseName;
} // end function getCourseName // function to set the instructor name
void GradeBook::setInstructorName( string name ) { instructorName = name; // store the instructor name
} // end function setInstructorName
// function to retrieve the instructor name string GradeBook::getInstructorName() { return instructorName;
} // end function getInstructorName
// display a welcome message and the instructor's name void GradeBook::displayMessage() {
// display a welcome message containing the course name cout << Welcome to the grade book for\\n << getCourseName() << ! << endl;
// display the instructor's name cout << This course is presented by: << getInstructorName() << endl; } // end function displayMessage 测试文件:
#include
// include definition of class GradeBook from GradeBook.h #include GradeBook.h
// function main begins program execution int main() {
// create a GradeBook object; pass a course name and instructor name GradeBook gradeBook(
CS101 Introduction to C++ Programming, Professor Smith ); // display initial value of instructorName of GradeBook object cout << gradeBook instructor name is:
<< gradeBook.getInstructorName() << \\n\\n; // modify the instructorName using set function
gradeBook.setInstructorName( Assistant Professor Bates ); // display new value of instructorName
cout << new gradeBook instructor name is:
<< gradeBook.getInstructorName() << \\n\\n; // display welcome message and instructor's name
C大学教程第五版课后习题答案第39章
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)