We begin with an example (Fig. 3.1) that consists of class GradeBook
(lines 8–16)—which, when it’s fully developed in Chapter 7, will represent a grade book that an instructor can use to maintain student test scores—and a main
function (lines 19–23) that creates a GradeBook
object. Function main
uses this object and its displayMessage
member function (lines 12–15) to display a message on the screen welcoming the instructor to the grade-book program.
1 // Fig. 3.1: fig03_01.cpp
2 // Define class GradeBook with a member function displayMessage,
3 // create a GradeBook object, and call its displayMessage function.
4 #include <iostream>
5 using namespace std;
6
7 // GradeBook class definition
8 class GradeBook
9 {
10 public:
11 // function that displays a welcome message to the GradeBook user
12 void displayMessage() const
13 {
14 cout << "Welcome to the Grade Book!" << endl;
15 } // end function displayMessage
16 }; // end class GradeBook
17
18 // function main begins program execution
19 int main()
20 {
21 GradeBook myGradeBook; // create a GradeBook object named myGradeBook
22 myGradeBook.displayMessage(); // call object's displayMessage function
23 } // end main
Welcome to the Grade Book!