In our prior examples, each class definition contained the complete definitions of the class’s public
member functions and the declarations of its private
data members. However, it’s better software engineering to define member functions outside the class definition, so that their implementation details can be hidden from the client code. This practice ensures that you do not write client code that depends on the class’s implementation details.
The program of Figs. 3.11–3.13 separates class GradeBook
’s interface from its implementation by splitting the class definition of Fig. 3.9 into two files—the header GradeBook.h
(Fig. 3.11) in which class GradeBook
is defined, and the source-code file GradeBook.cpp
(Fig. 3.12) in which GradeBook
’s member functions are defined. By convention, member-function definitions are placed in a source-code file of the same base name (e.g., GradeBook
) as the class’s header but with a .cpp
filename extension. The source-code file fig03_13.cpp
(Fig. 3.13) defines function main
(the client code). The code and output of Fig. 3.13 are identical to that of Fig. 3.10. Figure 3.14 shows how this three-file program is compiled from the perspectives of the GradeBook
class programmer and the client-code programmer—we’ll explain this figure in detail.
1 // Fig. 3.11: GradeBook.h
2 // GradeBook class definition. This file presents GradeBook's public
3 // interface without revealing the implementations of GradeBook's member
4 // functions, which are defined in GradeBook.cpp.
5 #include <string> // class GradeBook uses C++ standard string class
6
7 // GradeBook class definition
8 class GradeBook
9 {
10 public:
11 explicit GradeBook( std::string ); // constructor initialize courseName
12 void setCourseName( std::string ); // sets the course name
13 std::string getCourseName() const; // gets the course name
14 void displayMessage() const; // displays a welcome message
15 private:
16 std::string courseName; // course name for this GradeBook
17 }; // end class GradeBook
1 // Fig. 3.12: GradeBook.cpp
2 // GradeBook member-function definitions. This file contains
3 // implementations of the member functions prototyped in GradeBook.h.
4 #include <iostream>
5 #include "GradeBook.h" // include definition of class GradeBook
6 using namespace std;
7
8 // constructor initializes courseName with string supplied as argument
9 GradeBook::GradeBook( string name )
10 : courseName( name ) // member initializer to initialize courseName
11 {
12 // empty body
13 } // end GradeBook constructor
14
15 // function to set the course name
16 void GradeBook::setCourseName( string name )
17 {
18 courseName = name; // store the course name in the object
19 } // end function setCourseName
20
21 // function to get the course name
22 string GradeBook::getCourseName() const
23 {
24 return courseName; // return object's courseName
25 } // end function getCourseName
26
27 // display a welcome message to the GradeBook user
28 void GradeBook::displayMessage() const
29 {
30 // call getCourseName to get the courseName
31 cout << "Welcome to the grade book for
" << getCourseName()
32 << "!" << endl;
33 } // end function displayMessage
1 // Fig. 3.13: fig03_13.cpp
2 // GradeBook class demonstration after separating
3 // its interface from its implementation.
4 #include <iostream>
5 #include "GradeBook.h" // include definition of class GradeBook
6 using namespace std;
7
8 // function main begins program execution
9 int main()
10 {
11 // create two GradeBook objects
12 GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
13 GradeBook gradeBook2( "CS102 Data Structures in C++" );
14
15 // display initial value of courseName for each GradeBook
16 cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
17 << "
gradeBook2 created for course: " << gradeBook2.getCourseName()
18 << endl;
19 } // end main
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++