3.3. Overview of the Chapter Examples
The remainder of this chapter
presents seven simple examples that demonstrate the concepts we introduced in
the context of the car analogy. These examples, summarized below, incrementally
build a GradeBook class to demonstrate
these concepts:
The first example presents a
GradeBook class with one member function that
simply displays a welcome message when it is called. We show how to create an
object of that class and call the member function so that it displays the
welcome message.
The second example modifies the
first by allowing the member function to receive a course name as a so-called
argument. Then, the member function displays the course name as part of the
welcome message.
The third example shows how to store the
course name in a GradeBook object. For this
version of the class, we also show how to use member functions to set the course
name in the object and get the course name from the object.
The fourth example demonstrates how the
data in a GradeBook object can be
initialized when the object is created—the initialization is performed by a
special member function called the class's constructor. This example also
demonstrates that each GradeBook object
maintains its own course name data member.
The fifth example modifies the
fourth by demonstrating how to place class GradeBook into a separate file to enable software
reusability.
The sixth example modifies the
fifth by demonstrating the good software-engineering principle of separating the
interface of the class from its implementation. This makes the class easier to
modify without affecting any clients of the
class's objects—that is, any classes or
functions that call the member functions of the class's objects from outside the
objects.
The last example enhances class GradeBook by introducing data validation, which ensures that data
in an object adheres to a particular format or is in a proper value range. For
example, a Date object would require a month
value in the range 1–12. In this GradeBook
example, the member function that sets the course name for a
GradeBook object ensures that the course name
is 25 characters or fewer. If not, the member function uses only the first 25
characters of the course name and displays a warning
message.
Note that the GradeBook examples in this chapter do not actually process or store
grades. We begin processing grades with class GradeBook in Chapter
4 and we store grades in a GradeBook object
in Chapter
7, Arrays and Vectors.
No comments:
Post a Comment