Вы находитесь на странице: 1из 2

EECE 1313 LAB 5

16 NOVEMBER 2016

NAME:

MATRIC NO.:

The C++ class construct.


Normally a class definition is separated into two sections: specification and implementation.
To simplify our view, we will first give the class definition as one unit. Then we will separate
our definition into two parts: specification and implementation.
The class definition begins with the line with the C++ reserved word class and consists
of everything between the opening brace, {, up to and including the semicolon after the
closing brace,}. This definition includes a public and a private section. The public section
describes the public interface of the class. The public interface includes variables, types,
constants, and methods (functions) that a programmer needs to know to use the class. The
private section includes variables, constants, data types, and methods (functions) which are to
be hidden (not to be used) by other program components except those in the class definition.
Whether in the public or private section, variables and constants are referred to as data
members and functions are referred to as methods.
The data and method prototypes for a class are normally placed in a .h file and is
included in appropriate program files when the class definition is used. The implementation
of the methods is usually placed in a .cpp file.
In this exercise, for clarity place all in one file/block of code.
Follow the instructions below to create a complete definition of the class called Clock given
as one unit.
1. Begin your program by including pre-processor directives and namespace std. (This
is needed for the cout).
2. Write the Clock class definition. It should contain the following:
a. Private data members:
i. int hour
ii. int minute
iii. int second
iv. string meridian (whether its AM or PM)
b. Public accessible methods:
i. setClock
- The method set the clock to the specified time.
- Contains 4 arguments
- Does not return any value
ii. displayStandard
- The method display the time in standard notation
- Contains no arguments
- Does not return any value
In addition to the specification section, an implementation section must be developed
which defines each function included in the class definition. As noted above, the
implementation section for a class is usually placed in a separate file.
Note that the method has direct access to the private data members of the class such as
hour, minute, second, and meridian. Methods outside the class implementation file do not
have this access! One difference between methods in the implementation file and functions
outside the implementation file is the function heading.
The operator :: is used and is preceded by the name of the class in which the method
defined. The operator :: is called the scope resolution operator. It is possible for several
different classes to have member functions with the same name. The scope resolution
operator is used to tell the compiler to what class a member function belongs. Thus
when Clock:: precedes the name of the function setClock, we automatically know that
setClock is a member function in the class Clock.
3. Write the implementation section for each method define in class Clock.
a. Declare two Clock objects myClock and newClock
b. Set the time of myClock to 12:45:30 am.
c. Set the time of newClock to 2:15:30 pm.
d. Display the time of myClock and newClock.
Now, we can use this class specification and implementation in the main program that
uses our Clock class.
The dot operator is used to select a particular member function (method) to invoke. This
is how "messages" are passed to an object in C++. Thus,
myClock.setClock(12,45,30,"am");

sends the setClock message to the object myClock.

Note that nowhere in our main function do we try to access the data
variables hour, second, minute, and meridian which were defined in the Clock class. In fact,
we could not do so because these data members were placed in the private section of the class
which means that only members of the class can access them. Thus we could not incorporate
the following statements in the main function:
//This is an invalid member access
myClock.hour = 2;

*PLEASE SUBMIT YOUR WORK AT THE END OF THE LAB SESSION

Вам также может понравиться