Time Class Definition

The class definition (Fig. 9.1) contains prototypes (lines 13–16) for member functions Time, setTime, printUniversal and printStandard, and includes private unsigned int members hour, minute and second (lines 18–20). Class Time’s private data members can be accessed only by its member functions. Chapter 11 introduces a third access specifier, protected, as we study inheritance and the part it plays in object-oriented programming.


 1   // Fig. 9.1: Time.h
 2   // Time class definition.                  
 3   // Member functions are defined in Time.cpp
 4
 5   // prevent multiple inclusions of header
 6   #ifndef TIME_H
 7   #define TIME_H
 8
 9   // Time class definition
10   class Time
11   {
12   public:
13      Time(); // constructor
14      void setTime( int, int, int ); // set hour, minute and second
15      void printUniversal() const; // print time in universal-time format
16      void printStandard() const; // print time in standard-time format
17   private:
18      unsigned int hour; // 0 - 23 (24-hour clock format)
19      unsigned int minute; // 0 - 59
20      unsigned int second; // 0 - 59
21   }; // end class Time
22
23   #endif


Fig. 9.1. Time class definition.


Image Good Programming Practice 9.1

For clarity and readability, use each access specifier only once in a class definition. Place public members first, where they’re easy to locate.



Image Software Engineering Observation 9.1

Each member of a class should have private visibility unless it can be proven that the element needs public visibility. This is another example of the principle of least privilege.


In Fig. 9.1, the class definition is enclosed in the following include guard (lines 6, 7 and 23):

// prevent multiple inclusions of header
#ifndef TIME_H
#define TIME_H
   ...
#endif

When we build larger programs, other definitions and declarations will also be placed in headers. The preceding include guard prevents the code between #ifndef (which means “if not defined”) and #endif from being included if the name TIME_H has been defined. If the header has not been included previously in a file, the name TIME_H is defined by the #define directive and the header statements are included. If the header has been included previously, TIME_H is defined already and the header is not included again. Attempts to include a header multiple times (inadvertently) typically occur in large programs with many headers that may themselves include other headers.


Image Error-Prevention Tip 9.1

Use #ifndef, #define and #endif preprocessing directives to form an include guard that prevents headers from being included more than once in a source-code file.



Image Good Programming Practice 9.2

By convention, use the name of the header in uppercase with the period replaced by an underscore in the #ifndef and #define preprocessing directives of a header.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.12.175