Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your."— Presentation transcript:

1 © 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 Outline 15.1 Test-Driving the Digital Clock Application 15.2 Designing the Digital Clock Application 15.3 Separating Interface from Implementation 15.4 Initializing Objects: Constructors 15.5 Get and Set Functions 15.6 Completing the Digital Clock Application 15.7 Passing Arguments to a Constructor 15.8 Using the Debugger: The Autos Window 15.9 Wrap-Up

2 © 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.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.1 Test-Driving the Digital Clock Application

4 © 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.

5 © 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 the seconds reach 60.

6 © 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.

7 © 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 12 Set 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 value else set the number of hours to 12 When setting the number of minutes: If the number of minutes is greater than or equal to 0 and less than 60 set the number of minutes to the specified value else set 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 value else set the number of seconds to 0 When the time object is incremented: Increment the number of seconds If the number of seconds is 60 set the number of seconds to 0 Increment the number of minutes If the number of minutes is 60 set the number of minutes to 0 Increment the number of hours If the number of hours is 13 set the number of hours to 1

8 © 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 second Set the current time to the time that the user entered While the application is running If one second has passed Increment and display the time

9 © 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.

10 © 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).fieldspropertiesmethods –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.modularitystructure Source: http://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Object-oriented_programming

11 © 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.object-oriented programming blueprintobjects –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.instance –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.type source: http://en.wikipedia.org/wiki/Class_%28computer_science%29http://en.wikipedia.org/wiki/Class_%28computer_science%29

12 © 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.

13 © 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

14 © 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.

15 © 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)

16 © 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)

17 © 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

18 © 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

19 © 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 (. )

20 © 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

21 © 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.phphttp://www.ooportal.com/building-cplusplus-classes/module3/scope-resolution-operator.php

22 © 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

23 © 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

24 © 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

25 © 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

26 © 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.

27 © 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.

28 © 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 ).

29 © 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

30 © 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

31 © 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

32 © 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

33 © 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

34 © 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

35 © 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

36 © 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

37 © 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

38 © 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

39 © 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

40 © 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

41 © 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.

42 © 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

43 © 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

44 © 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

45 © 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

46 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.h (1 of 2) Prevent multiple inclusions of the same header file Declare Time ’s member functions using the public keyword Time constructor that specifies three int parameters

47 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.h (2 of 2) Define Time ’s data members using the private keyword

48 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (1 of 6) Include the Time.h header file Constructor initializes data members

49 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (2 of 6) Use the constructor’s parameters to set the hour, minute and second Return a value from a get function

50 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (3 of 6) Return the minute value Validate data

51 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (4 of 6) Validate data Return the second value

52 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (5 of 6) Validate dataSet the fill character to 0 to display the time in hh : mm : ss format

53 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.cpp (6 of 6) Increment second Increment minute if second contains 0 Increment hour if minute contains 0

54 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DigitalClock.cpp (1 of 3) Including the Time.h header file Create a variable of type time_t

55 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DigitalClock.cpp (2 of 3) Prompt user for and input current hour, minute and second Passing arguments to the Time constructor Display the time

56 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. DigitalClock.cpp (3 of 3) Use the time function to find the current time Update and display localTime ’s time once per second

57 © 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

58 © 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

59 © 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

60 © 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

61 © 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

62 © 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

63 © 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

64 © 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

65 © 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


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your."

Similar presentations


Ads by Google