34
ht 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application: Introducing Functions Outline 10.1 Test-Driving the Enhanced Wage Calculator Application 10.2 C++ Standard Library Functions and Classes 10.3 Function Definitions 10.4 Completing the Maximum Application 10.5 Using Functions in the Wage Calculator Application 10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands 10.7 Wrap-Up

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

Embed Size (px)

Citation preview

Page 1: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Tutorial 10 – Enhancing the Wage Calculator Application: Introducing

FunctionsOutline10.1 Test-Driving the Enhanced Wage Calculator

Application10.2 C++ Standard Library Functions and Classes10.3 Function Definitions10.4 Completing the Maximum Application10.5 Using Functions in the Wage Calculator Application10.6 Using the Debugger: Controlling Execution Using

the Step Into, Step Over, Step Out and Continue Commands10.7 Wrap-Up

Page 2: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Objectives

• In this tutorial, you will learn to:– Construct applications modularly from pieces called functions—

simplifying your application’s design, implementation and maintenance.

– Save time by working with “built-in” functions.

– Create your own functions to perform custom tasks.

– Control execution using debugger commands to locate logic errors.

Page 3: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.1 Test Driving the Wage Calculator Application

Page 4: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.1 Test Driving the Wage Calculator Application (Cont.)

Figure 10.1 Wage Calculator output.

Page 5: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.2 C++ Standard Library Functions and Classes

• C++ Standard Library functions and classes– Pre-existing code available for reuse

– Designed for maximum efficiency

– Tested to eliminate bugs

• C++ Standard Library Function example– pow (from the <cmath> library)

• C++ Standard Library Class example– cout and cin (from the <iostream> library)

• Programmer-defined functions– Can be created to meet unique requirements of new applications

Page 6: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Library Function Description Example algorithm max(x, y) Returns the larger value of x

and y max(2.3, 12.7) returns 12.7 max(-2.3, -12.7) returns -2.3

algorithm min(x, y) Returns the smaller value of x and y

min(2.3, 12.7) returns 2.3 min(-2.3, -12.7) returns -12.7

cmath sqrt(x) Returns the square root of x sqrt(9.0) returns 3.0 sqrt(2.0) returns 1.41421356237

string getline(x) Stores a line of user input in string x

getline(x); retrieves a line of text that the user entered cout << x << endl; displays the string stored in variable x, which corresponds to the line of text that the user entered

Figure 10.2 Some predefined C++ functions.

10.2 C++ Standard Library Functions and Classes (Cont.)

Page 7: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.3  Hypotenuse Calculator template code.

10.3 Function Definitions

Lengths for sides A and B

Error message displays if negative value (or zero) is entered

Page 8: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.3 Function Definitions (Cont.)

• Function header includes:– Return type

– Function name

– Parameter list

Figure 10.4  Defining the square function.

Function header

A right brace marks the end of a function definition

Page 9: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.3 Function Definitions (Cont.)

• Value returned is the same type declared in the function header

• To promote reusability, each function should perform a single well defined task

Figure 10.5  Coding the square function.

return statement sends a value back to the functions caller

Page 10: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.6  Hypotenuse Calculator application updated.

10.3 Function Definitions (Cont.)

Page 11: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

• Calling functions– A function is invoked (or called) by a function call

• A function call specifies the function name and includes data (arguments) for the callee (function being called)

• Example

• Function name: pow• Arguments: base, exponent

10.3 Function Definitions (Cont.)

Page 12: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.3 Function Definitions (Cont.)Figure 10.7  Invoking the square function.

Calling the square function for each side

Page 13: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.8  Completing the main function.

10.3 Function Definitions (Cont.)

Calling cmath function sqrt

Formatting and displaying the length of the hypotenuse

Page 14: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.3 Function Definitions (Cont.)

• Small functions are easier to test, debug and understand than large ones

Figure 10.9  Output from the completed Hypotenuse Calculator application.

Page 15: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.10  Invoking the determineMaximum function.

10.4 Completing the maximum function

Inputting three values

Calling the function determineMaximum

Page 16: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.4 Completing the maximum function (Cont.)

• Function determineMaximum returns the largest of three values

Figure 10.11  Defining the determineMaximum function.

The empty determineMaximum function

Page 17: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.4 Completing the maximum function (Cont.)

• Invoking pre-existing function max– Included in the <algorithm> library header file

Figure 10.12  max returns the larger of its two arguments.

Calling max twice to determine the maximum of three values

Returning the maximum of all three values

Page 18: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.4 Completing the maximum function (Cont.)

Figure 10.13 Maximum application output.

Page 19: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.14  main calls the calculatePay function.

10.5 Using Functions in the Wage Calculator Application

Inputting wage and hours worked

Calling the calculatePay function

Formatting and displaying the total wage

Page 20: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.15  Defining the calculatePay function.

10.5 Using Functions in the Wage Calculator Application

Function header

Calculating gross wages based on the number of hours worked

Return the wages for the week

Page 21: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.5 Using Functions in the Wage Calculator Application

Figure 10.16 Wage Calculator application output.

Page 22: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 // Tutorial 10: WageCalculator.cpp

2 // This application inputs the hourly wage and number of hours

3 // worked for an employee, then calculates the employee's gross

4 // wages (with overtime for hours worked over 40 hours).

5 #include <iostream> // required to perform C++ stream I/O

6 #include <iomanip> // required for parameterized stream manipulators

7

8 using namespace std; // for accessing C++ Standard Library members

9

10 // calculate and display wages

11 double calculatePay( double hours, double wages )

12 {

13 double total; // gross wages for week

14

15 // constant for maximum hours employee can

16 // work before being paid for overtime

17 const double HOUR_LIMIT = 40.0; // define constant

18

WageCalculator.cpp (1 of 4)

The calculatePay function takes two double parameters, returns a double

The total variable will contain users wages for the week

Nonovertime hour limit

Page 23: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

19 // determine gross wages

20 if ( hours <= HOUR_LIMIT )

21 {

22 // regular wages for HOUR_LIMIT (40) hours or less

23 total = wages * hours;

24 } // end if

25 else // worked more than HOUR_LIMIT (40) hours

26 {

27 // wages for first 40 hours with time and a half added

28 total = ( wages * HOUR_LIMIT ) + ( hours - HOUR_LIMIT ) *

29 ( 1.5 * wages );

30 } // end else

31

32 return total;

33

34 } // end function calculatePay

35

WageCalculator.cpp (2 of 4)

Calculate wages based on the number of hours worked

Returning total to calling function

Closing brace ends function body

Page 24: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

36 // function main begins program execution

37 int main()

38 {

39 // define variables

40 double hourlyWage; // stores hourly wage

41 double hoursWorked; // stores hours worked

42

43 // prompt user for hourly wage

44 cout << "\nEnter hourly wage: ";

45 cin >> hourlyWage; // get hourly wage

46

47 // prompt user for hours worked this week

48 cout << "Enter hours worked this week: ";

49 cin >> hoursWorked; // get number of hours worked this week

50

WageCalculator.cpp (3 of 4)

Prompt for user inputs

Page 25: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

51 // gross wages for week; returned from function calculatePay

52 double totalWages = calculatePay( hoursWorked, hourlyWage );

53

54 // specify output format

55 cout << fixed << setprecision( 2 );

56

57 // display gross wages

58 cout << "\nGross wages: $" << totalWages << "\n" << endl;

59

60 return 0; // indicate that program ended successfully

61

62 } // end function main

WageCalculator.cpp (4 of 4)

Call the calculatePay function

Format and display value returned from the calculatePay function

Page 26: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over,

Step Out and Continue Commands• Step Into command

– Executes the next statement in the program• If the next statement is a function call, program control goes to that

function

• Step Over command– Acts like the Step Into command

• If next statement is a function call, the function executes in its entirety and control goes to the next executable line

Page 27: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

• Step Out command– Executes remaining statements in a function and returns control

to the caller

• Continue Command– Executes until the next breakpoint or the end of main

(whichever comes first)

Page 28: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Figure 10.18  Setting a breakpoint in the Wage Calculator.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

Setting a breakpoint

Page 29: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

• Use the Step Into command to transfer program control to the called function

Figure 10.19  Reaching a breakpoint in the Wage Calculator application.

Next statement to execute is a function call

Page 30: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)Figure 10.20  Stepping into the calculatePay function.

Definitions (without assignments) are not considered executable statements

Next statement to execute

Page 31: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

Figure 10.21  Stepping over a statement in the calculatePay function.

Control is transferred to the if…else statement

Page 32: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

Figure 10.22  Setting a second breakpoint in the enhanced Wage Calculator application.

Setting second breakpoint

Page 33: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)

Figure 10.23  Using the debugger’s Continue command.

Next executable statement

Page 34: © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.)Figure 10.24  Using the debugger’s Step Over command.

The calculatePay function is executed without stepping into it when the Step Over command is selected