Problem

In this exercise, you prevent a function from changing the value of a named constant pas...

In this exercise, you prevent a function from changing the value of a named constant passed to it.

a. If necessary, create a new project named Advanced22 Project. Copy the instructions from the Lab10-2.cpp file into a source file named Advanced22.cpp. (Alternatively, you can enter the instructions from Figure 10-31 into the Advanced22.cpp file.) Change the filename in the first comment to Advanced22.cpp.

b. The program passes the value of the UNIT _CHG named constant to the calcBill function, which stores the value it receives (.09) in the chgPerUni t variable. Because the function stores the value in a variable, the value can be changed by the function. First, you will verify that the cal cBill function can change the value stored in the chg PerUn i t variable. Insert a blank line above the totChg = numUni is * chg PerUn i t; statement in the calcBill function. In the blank line, type chgPerUni t = . 25; . Save and then run the program. Enter 3000 and 2000 as the current and previous readings. The program displays 1000 as the number of units used, which is correct. How-ever, rather than displaying $90.00 as the total charge, the pro-gram displays $250.00.

c. To prevent the calcBill function from changing the charge per unit value, you must use the const keyword to indicate that the value being passed is a constant. Make the appropriate modification to the cal cBill function's prototype and its header. Save and then run the program. The compiler displays an error message indicating that you cannot assign a value to the chgP rUn i t variable, which is defined using the const keyword.

d. Delete the chg PerUni t = .25 ; statement from the calcBill function. Save and then run the program. Enter 3000 and 2000 as the current and previous readings. The program correctly displays 1000 and $90.00 as the units used and total charge, respectively.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT