tutorial 15 – digital clock application: building your own classes and objects
DESCRIPTION
Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects. - PowerPoint PPT PresentationTRANSCRIPT
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects
Outline15.1 Test-Driving the Digital Clock Application15.2 Designing the Digital Clock Application15.3 Separating Interface from Implementation15.4 Initializing Objects: Constructors15.5 Get and Set Functions15.6 Completing the Digital Clock Application15.7 Passing Arguments to a Constructor15.8 Using the Debugger: The Autos Window15.9 Wrap-Up
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
• In this tutorial, you will learn to:– Define your own classes.
– Create and use objects of your own classes.
– Control access to data members.
– Use the public and private keywords.
– Define your own get and set functions.
– Use the setfill stream manipulator to set the fill character for fields.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.1 Test-Driving the Digital Clock Application
Application Requirements An electronics company is considering building digital clocks. The company has asked you to develop an application that simulates a digital clock. The clock will update the time once per second. When the clock is first turned on, it should prompt the user to set the current time. The user should be able to enter a number of hours no less than 1 and no greater than 12, a number of minutes no greater than 59 and a number of seconds no greater than 59. Invalid hours entries will be set to 12, invalid minutes entries to 00 and invalid seconds entries to 00. To speed application development, the company has asked for a simplified simulation that displays a time in the range 12:00:00–11:59:59, but does not indicate whether the current time is AM or PM.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.1 Test-Driving the Digital Clock Application (Cont.)
Figure 15.1 Enter current hour: prompt displayed when your application is executed.
Figure 15.2 Entering valid data in the Digital Clock application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.1 Test-Driving the Digital Clock Application (Cont.)
Figure 15.3 Initial time displayed.
Figure 15.4 Digital Clock advances the minute and resets the seconds to 0 when theseconds reach 60.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.1 Test-Driving the Digital Clock Application (Cont.)
Figure 15.5 Digital Clock application with invalid input.
Figure 15.6 Digital Clock application after invalid input has been entered.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Figure 15.7 Pseudocode for the Time class.
When the time object is created:Set the number of hours to 12Set the number of minutes and number of seconds to 0
When setting the number of hours:If the number of hours is greater than 0 and less than 13
set the number of hours to the specified valueelse
set the number of hours to 12When setting the number of minutes:
If the number of minutes is greater than or equal to 0 and less than 60set the number of minutes to the specified value
elseset the number of minutes to 0
When setting the number of seconds:If the number of seconds is greater than or equal to 0 and less than 60
set the number of seconds to the specified valueelse
set the number of seconds to 0When the time object is incremented:
Increment the number of secondsIf the number of seconds is 60
set the number of seconds to 0Increment the number of minutesIf the number of minutes is 60
set the number of minutes to 0Increment the number of hoursIf the number of hours is 13
set the number of hours to 1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Figure 15.8 Pseudocode for the Digital Clock application.
Prompt the user to enter the current hour, minute and secondSet the current time to the time that the user entered
While the application is running
If one second has passedIncrement and display the time
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1.9 Key Software Trend: Object Technology
• Procedural Programming Languages (verb)
– Focus on actions. Good analogy is a Cooking Recipes.
• Objects (noun)
– Object Circle
– Properties – the attributes of an object (adjective).
– Methods – what the object can do (adverb). May also be referred to as behaviors or functions.
– Class
• An object is an instance of a class definition.
• Example: A basketball is an instance of a ball.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.2 Designing the Digital Clock Application (Cont.)
• Class
– A class is a template for an object, a user-defined data type that contains variables, properties of an object.
– A class defines abstract characteristics of a thing (object), including its characteristics (its attributes, fields or properties) and the things it can do (behaviors, methods, operations or features).
– One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors).
– Classes provide modularity and structure in an object-oriented computer program.
Source: http://en.wikipedia.org/wiki/Object-oriented_programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.2 Designing the Digital Clock Application (Cont.)
• Class
– In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class.
– This blueprint describes the state and behavior that the objects of the class all share.
– An object of a given class is called an instance of the class.
– The class that contains (and was used to create) that instance can be considered as the type of that object, e.g. an object instance of the "Fruit" class would be of the type "Fruit“ like an Apple.
source: http://en.wikipedia.org/wiki/Class_%28computer_science%29
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
OOPs versus C++ Language Standard
Method versus Member Function
• Member Function is the term used in the C++ language standard.
• The Object Oriented Programming (OOP) term Method is equivalent to the C++ term Member Function
• Where your textbook uses the word Member Function I will try and use the word Method.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation
• A class’s interface
– Consists of method prototypes
• Specify return types and parameter lists
– Should be placed in a header file (.h)
– Usually changes infrequently
• A class’s implementation
– Consists of the code that executes when methods are called.
– Should be placed in a source code file (.cpp)
– May change often
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
• For the Digital Clock Application the class’s interface, consisting of the method prototypes – each specifying the return types and parameter lists – are contained in the Time.h file.
• The class’s implementation, consisting of the code that executes when methods are invoked is defined in the Time.cpp file.
• The Digital Clock application which instatiates the Time class is in the DigitalClock.cpp file.
• Do not confuse these three files when working through this material.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.9 Header Files folder in the Solution Explorer window.
Header Files folder (selected)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.10 Selecting the Time.h file in the Add Existing Item dialog.
Time.h file (selected)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.11 Header Files folder containing Time.h.
Time.h file in Header Files folder
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.12 Empty class definition.
Preventing multiple inclusions of the same header file
Empty class definition
• Code between #ifndef and #endif is ignored if TIME_H is already defined
Time.h header file
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.13 Time’s member functions.
Declaring Time’s member functions using the public member-access specifier
Time.h header file
• Public methods can be accessed by using an object followed by the dot operator (.)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.3 Separating Interface from Implementation (Cont.)
Figure 15.14 Time’s data members.
Defining Time’s data members using keyword private
Time.h header file
• Private variables can be accessed only by methods within the same class
• Attempted access from another class definition is a syntax error
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.4 Initializing Objects: Constructors
• The binary scope resolution operator (::) in C++ is used to define the already declared methods (in the header file with the .h extension) of a particular class.
• To distinguish between the private functions and the public methods of the class, one needs to use the scope resolution operator (::) in between the class name and the method name
Source: http://www.ooportal.com/building-cplusplus-classes/module3/scope-resolution-operator.php
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.4 Initializing Objects: Constructors (Cont.)
Figure 15.15 Including the Time class header file.
Including the Time.h header file
• Looking for header files enclosed in angle brackets
• Preprocessor looks only in Standard Library directory
• Looking for header files enclosed in quotation marks
• Preprocessor looks in current directory first
Time.cpp class definition
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.4 Initializing Objects: Constructors (Cont.)
Figure 15.16 Defining an empty constructor.
Empty constructor
• Constructor
• Has the same name as the class
• Has no return type (not even void)
• Is called when an object is instantiated
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.4 Initializing Objects: Constructors (Cont.)
Figure 15.17 Constructor initializing data members.
Constructor initializes data members
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.4 Initializing Objects: Constructors (Cont.)Figure 15.18 Including the Time.h header file.
Including the Time.h header file
Figure 15.19 Instantiating an object of type Time.
Declaring an object of the Time class
• Programmer-defined classes form new data types
• C++ is an extensible language because it can be extended with these new classes
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
OOPs versus Your C++ Textbook
Get and Set Functions versus Getter and Setter Methods
• Get and Set Functions are terms unique to the C++ language. The equivalent Object Oriented Programming (OOP) terms are Getter and Setter Methods.
• The Object Oriented Programming (OOP) term Method is equivalent to the C++ term Function within the context of Getters and Setters.
• Where your textbook uses the words Get and Set Functions I will try and use the OOP words Getter and Setter Methods.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
• Getter or Accessor Method
Return a data member value
• Setter or Mutator Method
Assigns a value to a data member
Usually performs a validation check on the value before assigning it to the data member
• After decades and millions of lines of code, programmers have made it a rule of good programming style to not directly access or modify properties.
• While C++ allows it more modern OOPs languages do not.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.20 getHour definition.
Returning a value from a get function
• The prefix get is not required by C++.
• An alternative naming convention to camel case, would be to separate get and the property name with an underline character (get_hour).
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.21 setHour definition to check validity of data.
Validating data
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.22 getMinute definition.
Returning the minute value
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.23 setMinute definition used to validate data.
Validating data
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.24 getSecond definition.
Returning the second value
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.25 setSecond definition to validate data.
Validating data
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.26 displayTime definition.
Setting the fill character to 0 for displaying the time in hh:mm:ss format
• setfill specifies the character that will fill unoccupied positions in a field in the output stream
• flush immediately outputs the stream output buffer to the screen
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.27 Constructor using set functions to initialize variables.
Assigning data by calling the setHour, setMinute and setSecond functions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.5 Getter and Setter Methods (Cont.)
Figure 15.28 Incrementing the time in the tick function.
Incrementing second
Incrementing minute if second contains 0
Incrementing hour if minute contains 0
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.6 Completing the Digital Clock Application
Figure 15.29 Defining a variable of type time_t.
Creating a variable of type time_t
• time_t is the return type of the time function
• The time function returns the current clock time in seconds
DigitalClock.cpp application
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.6 Completing the Digital Clock Application (Cont.)
Figure 15.30 Setting and displaying the current time.
Prompting user for and inputting the current hour, minute and second, then using the Time class’s set functions to set the current time
Displaying the current time
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.6 Completing the Digital Clock Application (Cont.)
Figure 15.31 Determining the current time.
Using the time function to store the current time, in seconds
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.6 Completing the Digital Clock Application (Cont.)
Figure 15.32 Incrementing and displaying the Digital Clock’s time.
Updating and displaying localTime’s time once per second
• Busy waiting
• Repeatedly testing a condition until it becomes false
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.6 Completing the Digital Clock Application (Cont.)
Figure 15.33 Running the Digital Clock application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.7 Passing Arguments to a Constructor
Figure 15.34 Declaring a Time constructor prototype that specifies parameters.
• Default constructor
• A constructor that declares no parameters
• Function overloading
• Multiple functions with the same name, but different parameter lists
• A function call’s argument list determines which function is called
Time constructor specifies three int parameters
Time.h header file
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.7 Passing Arguments to a Constructor (Cont.)
Figure 15.35 Defining the overloaded Time constructor.
Using the constructor’s parameters to set the hour, minute and second
Time.cpp class definition
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.7 Passing Arguments to a Constructor (Cont.)
Figure 15.36 Modifying DigitalClock.cpp to pass arguments to the Time constructor.
Passing arguments to the Time constructor
DigitalClock.cpp application
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.7 Passing Arguments to a Constructor (Cont.)
Figure 15.37 IntelliSense displaying the Time constructor prototype.
IntelliSense displays the appropriate constructor prototype
• Visual Studio .NET’s IntelliSense feature
• Displays function prototypes when writing a function call
• Use up and down arrows to select from overloaded functions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Time.h (1 of 2)
1 // Tutorial 15: Time.h
2 // Declaration of class Time.
3 // Member functions are defined in Time.cpp
4
5 // prevent multiple inclusions of header file
6 #ifndef TIME_H
7 #define TIME_H
8
9 // Time abstract data type definition
10 class Time
11 {
12 public:
13 Time(); // default constructor
14 Time( int, int, int ); // three-argument constructor
15 void displayTime(); // displays the formatted time
16 void tick(); // increments the time by one second
17
Prevent multiple inclusions of the same header file
Declare Time’s member functions using the public keyword
Time constructor that specifies three int parameters
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
18 // get functions
19 int getHour(); // gets the current hour
20 int getMinute(); // gets the current minute
21 int getSecond(); // gets the current second
22
23 // set functions
24 void setHour( int ); // sets the hour
25 void setMinute( int ); // sets the minute
26 void setSecond( int ); // sets the second
27
28 private:
29 int hour; // stores the hour (range: 1 - 12)
30 int minute; // stores the minute (range: 0 - 59)
31 int second; // stores the second (range: 0 - 59)
32
33 }; // end class Time
34
35 #endif
Time.h (2 of 2)
Define Time’s data members using the private keyword
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 // Tutorial 15: Time.cpp
2 // Member-function definitions for class Time.
3 #include <iostream> // required to perform C++ stream I/O
4 #include <iomanip> // required for parameterized stream manipulators
5
6 // include definition of class Time from Time.h
7 #include "Time.h"
8
9 using namespace std; // for accessing C++ Standard Library members
10
11 // Time constructor initializes each data member and ensures
12 // that all Time objects start in a consistent state
13 Time::Time()
14 {
15 // initialize each value
16 setHour( 12 );
17 setMinute( 0 );
18 setSecond( 0 );
19
20 } // end Time default constructor
21
Time.cpp (1 of 6)
Include the Time.h header file
Constructor initializes data members
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
22 // Time constructor initializes each data member to values
23 // specified in the parameter list
24 Time::Time( int hourValue, int minuteValue, int secondValue )
25 {
26 // initialize each value
27 setHour( hourValue );
28 setMinute( minuteValue );
29 setSecond( secondValue );
30
31 } // end Time overloaded constructor
32
33 // return hour value
34 int Time::getHour()
35 {
36 return hour;
37
38 } // end function getHour
39
Time.cpp (2 of 6)
Use the constructor’s parameters to set the hour, minute and second
Return a value from a get function
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Time.cpp (3 of 6)
40 // sets new hour value, performs validity checks on the data value
41 // and sets invalid values to 12.
42 void Time::setHour( int hourValue )
43 {
44 // update hour value
45 if ( hourValue > 0 && hourValue < 13 )
46 {
47 hour = hourValue; // use hourValue if it is in the valid range
48 } // end if
49 else // otherwise, set hour to 12
50 {
51 hour = 12;
52 } // end else
53
54 } // end function setHour
55
56 // return minute value
57 int Time::getMinute()
58 {
59 return minute;
60
61 } // end function getMinute
62
Return the minute value
Validate data
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
63 // sets new minute value, performs validity checks on the data value
64 // and sets invalid values to 0.
65 void Time::setMinute( int minuteValue )
66 {
67 // update minute value
68 if ( minuteValue >= 0 && minuteValue < 60 )
69 {
70 minute = minuteValue; // use minuteValue if within valid range
71 } // end if
72 else // otherwise, set minute to 0
73 {
74 minute = 0;
75 } // end else
76
77 } // end function setMinute
78
79 // return second value
80 int Time::getSecond()
81 {
82 return second;
83
84 } // end function getSecond
85
Time.cpp (4 of 6)
Validate data
Return the second value
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
86 // sets new second value, performs validity checks on the data value
87 // and sets invalid values to 0.
88 void Time::setSecond( int secondValue )
89 {
90 // update second value
91 if ( secondValue >= 0 && secondValue < 60 )
92 {
93 second = secondValue; // use secondValue if within valid range
94 } // end if
95 else // otherwise, set second to 0
96 {
97 second = 0;
98 } // end else
99
100 } // end function setSecond
101
102 // display Time
103 void Time::displayTime()
104 {
105 cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
106 << setw( 2 ) << getMinute() << ":"
107 << setw( 2 ) << getSecond() << flush;
108
109 } // end function displayTime
110
Time.cpp (5 of 6)
Validate data
Set the fill character to 0 to display the time in hh:mm:ss format
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
111 // increments the current time by one second
112 void Time::tick()
113 {
114 // increment time by one second
115 setSecond( ( getSecond() + 1 ) % 60 );
116
117 // update minutes if seconds was previously 59
118 if ( getSecond() == 0 )
119 {
120 setMinute( ( getMinute() + 1 ) % 60 );
121
122 // update hours if minutes previously was 59
123 if ( getMinute() == 0 )
124 {
125 setHour( ( getHour() % 12 ) + 1 );
126 } // end if
127
128 } // end if
129
130 } // end function tick
Time.cpp (6 of 6)
Increment second
Increment minute if second contains 0
Increment hour if minute contains 0
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 // Tutorial 15: DigitalClock.cpp
2 // Simulates the operation of a digital clock.
3 // NOTE: This file must be compiled with Time.cpp.
4 #include <iostream> // required to perform C++ stream I/O
5 #include <ctime> // contains prototype for function time
6
7 // include definition of class Time from Time.h
8 #include "Time.h"
9
10 using namespace std; // for accessing C++ Standard Library members
11
12 int main()
13 {
14 // define variables
15 int hour; // store the current hour
16 int minute; // store the current minute
17 int second; // store the current second
18 time_t currentTime; // store time program has been running
19
DigitalClock.cpp (1 of 3)
Including the Time.h header file
Create a variable of type time_t
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20 // prompt the user for localTime's initial values
21 cout << "Enter current hour: ";
22 cin >> hour;
23
24 cout << "Enter current minute: ";
25 cin >> minute; // set current minute
26
27 cout << "Enter current second: ";
28 cin >> second; // set current second
29
30 // instantiate object localTime of class Time
31 Time localTime( hour, minute, second );
32
33 cout << endl;
34
35 localTime.displayTime();
36
DigitalClock.cpp (2 of 3)
Prompt user for and input current hour, minute and second
Passing arguments to the Time constructor
Display the time
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
37 // update the clock indefinitely
38 while ( true )
39 {
40 currentTime = time( 0 ); // store the current time
41
42 // busy wait until one second has passed
43 while ( time( 0 ) < ( currentTime + 1 ) );
44
45 localTime.tick(); // advance the time in localTime
46 cout << "\r”;
47 localTime.displayTime(); // display the updated time
48 } // end while
49
50 return 0; // indicate that program ended successfully
51
52 } // end function main
DigitalClock.cpp (3 of 3)
Use the time function to find the current time
Update and display localTime’s time once per second
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window
Figure 15.41 Setting breakpoints in the Digital Clock application.
Setting breakpoints
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.42 Autos window displaying the state of several local variables.
Autos window displaying name and values of several recently used variables
• Autos window
• Displays name, value and type of variables used in previous statement and next statement
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.43 Autos window displaying the state of localTime.
localTime data members’ values
• Click the plus box next to the object name to view each data member individually
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.44 Locals window displaying local variables.
Local variables
• As with the Autos window, click the plus box next to the object name to view each data member individually
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.45 Autos window displaying updated variable values.
localTime data members’ values have been initialized
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.46 Locals window displaying changed variable values in red.
currentTime’s value has changed
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.47 Changing the value of a variable in the Autos window.
Value changed by user
• As with the Locals window, double click a value to manually change it while debugging
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15.8 Using the Debugger: The Autos Window (Cont.)
Figure 15.48 Updated variables listed in the Autos window.
Modified variable values displayed in red
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Lab and Homework Assignment
• Tutorial 15 − Digital Clock Application. Turn in annotated source file with your own comments for Exercise 15.12.
• Answer and Turn-in Tutorial 15 Questions 15.1 to 15.10. Always write the question followed by the answer. Remember to highlight the answer.
• Exercises 15.11, 15.12, and 15.13. For exercises 15.11 and 15.12 start with the provided templates. For Exercise 15.12 start from your completed Tutorial.
• Due next Wednesday