1 / 65

Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects

Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects.

coyne
Download Presentation

Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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. 15.1 Test-Driving the Digital Clock Application

  4. 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. 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. 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. 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. 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 passed Increment and display the time

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

  11. 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

  12. 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 Methodis equivalent to the C++ term Member Function • Where your textbook uses the word Member Function I will try and use the word Method.

  13. 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. 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. Header Files folder (selected) 15.3 Separating Interface from Implementation (Cont.) Figure 15.9 Header Files folder in the Solution Explorer window.

  16. Time.h file (selected) 15.3 Separating Interface from Implementation (Cont.) Figure 15.10 Selecting the Time.h file in the Add Existing Item dialog.

  17. Time.hfile in Header Files folder 15.3 Separating Interface from Implementation (Cont.) Figure 15.11 Header Files folder containing Time.h.

  18. Preventing multiple inclusions of the same header file Empty class definition 15.3 Separating Interface from Implementation (Cont.) Figure 15.12 Empty class definition. Time.h header file • Code between #ifndef and #endif is ignored if TIME_H is already defined

  19. Time.h header file Declaring Time’s member functions using the public member-access specifier 15.3 Separating Interface from Implementation (Cont.) Figure 15.13 Time’s member functions. • Public methods can be accessed by using an object followed by the dot operator (.)

  20. Time.h header file Defining Time’s data members using keyword private 15.3 Separating Interface from Implementation (Cont.) Figure 15.14 Time’s data members. • Private variables can be accessed only by methods within the same class • Attempted access from another class definition is a syntax error

  21. 15.4 Initializing Objects: Constructors • The binary scope resolution operator (::) in C++ is used to define the already declaredmethods (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

  22. Including theTime.hheader file 15.4 Initializing Objects: Constructors (Cont.) Figure 15.15 Including the Time class header file. Time.cpp class definition • 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

  23. Empty constructor 15.4 Initializing Objects: Constructors (Cont.) Figure 15.16 Defining an 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. Constructor initializes data members 15.4 Initializing Objects: Constructors (Cont.) Figure 15.17 Constructor initializing data members.

  25. Including the Time.h header file Declaring an object of the Time class 15.4 Initializing Objects: Constructors (Cont.) Figure 15.18 Including the Time.h header file. Figure 15.19 Instantiating an object of type Time. • Programmer-defined classes form new data types • C++ is an extensible language because it can be extended with these new classes

  26. 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 Methodis 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. 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. Returning a value from a get function 15.5 Getter and Setter Methods (Cont.) Figure 15.20 getHour definition. • 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. Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.21 setHour definition to check validity of data.

  30. Returning the minute value 15.5 Getter and Setter Methods (Cont.) Figure 15.22 getMinute definition.

  31. Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.23 setMinute definition used to validate data.

  32. Returning the second value 15.5 Getter and Setter Methods (Cont.) Figure 15.24 getSecond definition.

  33. Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.25 setSecond definition to validate data.

  34. Setting the fill character to 0 for displaying the time in hh:mm:ss format 15.5 Getter and Setter Methods (Cont.) Figure 15.26 displayTime definition. • setfill specifies the character that will fill unoccupied positions in a field in the output stream • flush immediately outputs the stream output bufferto the screen

  35. Assigning data by calling the setHour, setMinute and setSecond functions 15.5 Getter and Setter Methods (Cont.) Figure 15.27 Constructor using set functions to initialize variables.

  36. Incrementing second Incrementing minute if second contains 0 Incrementing hour if minute contains 0 15.5 Getter and Setter Methods (Cont.) Figure 15.28 Incrementing the time in the tick function.

  37. Creating a variable of type time_t 15.6 Completing the Digital Clock Application Figure 15.29 Defining a variable of type time_t. DigitalClock.cpp application • time_t is the return type of the time function • The time function returns the current clock time in seconds

  38. 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 15.6 Completing the Digital Clock Application (Cont.) Figure 15.30 Setting and displaying the current time.

  39. Using the time function to store the current time, in seconds 15.6 Completing the Digital Clock Application (Cont.) Figure 15.31 Determining the current time.

  40. Updating and displaying localTime’s time once per second 15.6 Completing the Digital Clock Application (Cont.) Figure 15.32 Incrementing and displaying the Digital Clock’s time. • Busy waiting • Repeatedly testing a condition until it becomes false

  41. 15.6 Completing the Digital Clock Application (Cont.) Figure 15.33 Running the Digital Clock application.

  42. Time constructor specifies three int parameters 15.7 Passing Arguments to a Constructor Figure 15.34 Declaring a Time constructor prototype that specifies parameters. Time.h header file • 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

  43. Using the constructor’s parameters to set the hour, minute and second 15.7 Passing Arguments to a Constructor (Cont.) Figure 15.35 Defining the overloaded Time constructor. Time.cpp class definition

  44. Passing arguments to the Time constructor 15.7 Passing Arguments to a Constructor (Cont.) Figure 15.36 Modifying DigitalClock.cpp to pass arguments to the Time constructor. DigitalClock.cpp application

  45. IntelliSense displays the appropriate constructor prototype 15.7 Passing Arguments to a Constructor (Cont.) Figure 15.37 IntelliSense displaying the Time 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. Prevent multiple inclusions of the same header file Declare Time’s member functions using the public keyword Time constructor that specifies three int parameters Time.h (1 of 2)

  47. Define Time’s data members using the private keyword Time.h (2 of 2)

  48. Include the Time.h header file Constructor initializes data members Time.cpp (1 of 6)

  49. Use the constructor’s parameters to set the hour, minute and second Return a value from a get function Time.cpp (2 of 6)

  50. Validate data Return the minute value Time.cpp (3 of 6)

More Related