0% found this document useful (0 votes)
4 views

Revision of Overloading and Assert in Preparation For Assignment Two

NOTES
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Revision of Overloading and Assert in Preparation For Assignment Two

NOTES
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Revision: Overloading

function names and the


Assert Statement
Overloading
• Providing two or more different definitions of the same function name.

• Overloading a function: more than one function in the same class having the same
name but differing either in the number of parameters or the types of their
parameters.

• The easiest way to remember this: the functions’ parameters should qualify any one
or more than one of the following conditions:

 They should have a different type.

 They should have a different number.

 They should have a different sequence of parameters.

If we have to perform a single operation with different numbers or types of arguments,


we need to overload the function.
Why overload?

 Makes a program easier to read.

 The programmer does not have to remember various


function names.

Saves you from going crazy trying to think up a new name


for a function just because you already used the most
natural name in some other function definition!

N.B. You should not use the same function name for two
unrelated functions.
Assignment Two 2022: Question One

 In this question, students were required to write an overloaded function


max that takes either two or three parameters of type double and returns
the largest of them.
1. double max(double value1, double value2)
2. {
The functions on lines 1 and 7 have the
3. if (value1 > value2)
same function name max(). The function
4. return value1; is overloaded. The difference is the number
5. else return value2; of parameters in these functions. In line 1
the function has two double parameters and
6. } in line 7, three double parameters. The
compiler will use the number of parameters
7. double max(double value1, double value2, to decide which function to call. For instance,
double value3) when the function is called with two
8. { parameters, it will use the function that starts
at line 1. When max() is called with three
9. if ((value1 > value2) && (value1 > value3)) double parameters it will use the function
10.return value1; starting at line 7.
11.else if ((value2 > value1) && (value2 >
value3))
12.return value2;
13.else if ((value3 >value1) && (value3 >
value2))
14.return value3;
15.}
Assert macro
Assert is a preprocessor macro that is used to evaluate a
conditional expression.
A macro is a construct similar to a function which is
replaced by its definition inline each time it is used.
The assert macro is a tool to ensure that the expected
conditions are true at the location of the assert statement.
Each assertion contains a boolean expression that you
believe will be true when the assertion executes.
If the conditional expression evaluates to false, then the
program is terminated after displaying the error message.
By verifying that the boolean expression is indeed true, the
assertion confirms your assumptions about the behaviour
of your program, increasing your confidence that the
program is free of errors.
Assert macro

The assert() function evaluates a boolean expression.

• If the result is 1 (true), the program continues.

• If the result is 0 (false), the program aborts with an


exception.
Assert macro
 To use assert:-
 Firstly, include the definition of assert in your program
with the following include statement:
#include <cassert>
Secondly, add the following line of code at the location
where you would like to enforce the assertion with a
boolean expression that should evaluate to true:
assert(boolean_expression);
Assignment Two 2022: Question Two

 In this question, students were required to write a C++ program that includes a function
to calculate the discount applicable on the price of an item. The function should have
three arguments: the price as a reference parameter to a double, the discount as a
double value, and a bool to indicate if the discount is calculated as a percentage or a
fixed amount. The parameter to indicate whether the discount is a fixed amount, or a
percentage, should be called fixed. When fixed is true, it will indicate that the discount
is a fixed amount and when it is false, the discount is a percentage. The function
should calculate the discount and modify the price of the item accordingly.

 The function should check that the discount is not negative and that the price does not
drop to zero or below zero after applying the discount. The following solution uses the
assert() function to ensure that the discount is not negative and that the price does not
drop to zero or below zero once the discount is applied.
Solution using Assert
1. //This program demonstrates using the assert statement to ensure
the input
2. //and discount calculation meet certain criteria.
3. //The discount value may not be negative and the price resulting
from the
4. //discount may not be 0 or below 0.
5.
6. #include <iostream>
7. #include <cassert>

8. using namespace std;


Solution using Assert
double calcDiscount(double price, double discount,
bool fixed)
{
if (fixed)
price = price - discount;
else price -= discount/100 * price;
return price;
}
Solution using Assert
int main()
{
double itemPrice, itemDiscount, discountPrice;
char answer;
bool fixedDiscount;

cout << endl << "Please enter the price and discount separated by spaces
" << endl;
cin >> itemPrice >> itemDiscount;
cout << "Is the discount a fixed amount or a percentage? Type F for a
fixed amount, P for a percentage:";

cin >> answer;


Solution using Assert
The assert function is used to check that
the discount is not negative. If this criterion
is not met, the assert function will throw an
assert (itemDiscount > 0); exception error.

fixedDiscount = (answer== 'f' || answer == 'F’);

discountPrice = calcDiscount(itemPrice, itemDiscount, fixedDiscount);

assert(discountPrice >= 0);


The assert function is used to check that the
cout.setf(ios::fixed); result of the discount does not produce a price
equal to or below zero. If this criterion is not met,
the assert function will throw an exception
cout.setf(ios::showpoint); error.

cout.precision(2);

cout << "The discounted price for R" << itemPrice << " is R " <<
discountPrice << endl;

return 0;}
Removing Assertions
• To remove assert statements
 No need to delete them manually
 Include:

#define NDEBUG

All subsequent assert statements are ignored

14

You might also like