Each of the previous examples in the chapter consists of a single .cpp
file, also known as a source-code file, that contains a GradeBook
class definition and a main
function. When building an object-oriented C++ program, it’s customary to define reusable source code (such as a class) in a file that by convention has a .h
filename extension—known as a header. Programs use #include
preprocessing directives to include headers and take advantage of reusable software components, such as type string
provided in the C++ Standard Library and user-defined types like class GradeBook
.
Our next example separates the code from Fig. 3.7 into two files—GradeBook.h
(Fig. 3.9) and fig03_10.cpp
(Fig. 3.10). As you look at the header in Fig. 3.9, notice that it contains only the GradeBook
class definition (lines 7–38) and the headers on which the class depends. The main
function that uses class GradeBook
is defined in the source-code file fig03_10.cpp
(Fig. 3.10) in lines 8–18. To help you prepare for the larger programs you’ll encounter later in this book and in industry, we often use a separate source-code file containing function main
to test our classes (this is called a driver program). You’ll soon learn how a source-code file with main
can use the class definition found in a header to create objects of a class.
1 // Fig. 3.9: GradeBook.h
2 // GradeBook class definition in a separate file from main.
3 #include <iostream>
4 #include <string> // class GradeBook uses C++ standard string class
5
6 // GradeBook class definition
7 class GradeBook
8 {
9 public:
10 // constructor initializes courseName with string supplied as argument
11 explicit GradeBook( std::string name )
12 : courseName( name ) // member initializer to initialize courseName
13 {
14 // empty body
15 } // end GradeBook constructor
16
17 // function to set the course name
18 void setCourseName( std::string name )
19 {
20 courseName = name; // store the course name in the object
21 } // end function setCourseName
22
23 // function to get the course name
24 std::string getCourseName() const
25 {
26 return courseName; // return object's courseName
27 } // end function getCourseName
28
29 // display a welcome message to the GradeBook user
30 void displayMessage() const
31 {
32 // call getCourseName to get the courseName
33 std::cout << "Welcome to the grade book for
" << getCourseName()
34 << "!" << std::endl;
35 } // end function displayMessage
36 private:
37 std::string courseName; // course name for this GradeBook
38 }; // end class GradeBook
1 // Fig. 3.10: fig03_10.cpp
2 // Including class GradeBook from file GradeBook.h for use in main.
3 #include <iostream>
4 #include "GradeBook.h" // include definition of class GradeBook
5 using namespace std;
6
7 // function main begins program execution
8 int main()
9 {
10 // create two GradeBook objects
11 GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
12 GradeBook gradeBook2( "CS102 Data Structures in C++" );
13
14 // display initial value of courseName for each GradeBook
15 cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
16 << "
gradeBook2 created for course: " << gradeBook2.getCourseName()
17 << endl;
18 } // end main
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++