One of the benefits of creating class definitions is that, when packaged properly, your classes can be reused by other programmers. For example, you can reuse C++ Standard Library type string
in any C++ program by including the header <string>
(and, as you’ll see, by being able to link to the library’s object code).
Programmers who wish to use our GradeBook
class cannot simply include the file from Fig. 3.7 in another program. As you learned in Chapter 2, function main
begins the execution of every program, and every program must have exactly one main
function. If other programmers include the code from Fig. 3.7, they get extra “baggage”—our main
function—and their programs will then have two main
functions. Attempting to compile a program with two main
functions produces an error. So, placing main
in the same file with a class definition prevents that class from being reused by other programs. In this section, we demonstrate how to make class GradeBook
reusable by separating it into another file from the main
function.