800 likes | 1.34k Views
Tutorial 15 – Digital Clock Application: Building Your Own Classes and Objects.
E N D
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
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.
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.
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.
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.
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
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
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.
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
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
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.
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
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.
Header Files folder (selected) 15.3 Separating Interface from Implementation (Cont.) Figure 15.9 Header Files folder in the Solution Explorer window.
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.
Time.hfile in Header Files folder 15.3 Separating Interface from Implementation (Cont.) Figure 15.11 Header Files folder containing Time.h.
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
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 (.)
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
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
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
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
Constructor initializes data members 15.4 Initializing Objects: Constructors (Cont.) Figure 15.17 Constructor initializing data members.
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
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.
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.
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).
Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.21 setHour definition to check validity of data.
Returning the minute value 15.5 Getter and Setter Methods (Cont.) Figure 15.22 getMinute definition.
Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.23 setMinute definition used to validate data.
Returning the second value 15.5 Getter and Setter Methods (Cont.) Figure 15.24 getSecond definition.
Validating data 15.5 Getter and Setter Methods (Cont.) Figure 15.25 setSecond definition to validate data.
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
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.
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.
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
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.
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.
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
15.6 Completing the Digital Clock Application (Cont.) Figure 15.33 Running the Digital Clock application.
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
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
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
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
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)
Define Time’s data members using the private keyword Time.h (2 of 2)
Include the Time.h header file Constructor initializes data members Time.cpp (1 of 6)
Use the constructor’s parameters to set the hour, minute and second Return a value from a get function Time.cpp (2 of 6)
Validate data Return the minute value Time.cpp (3 of 6)