














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Function overloading in C++, which allows the use of the same function name with different parameter lists. It also covers automatic type conversion and its impact on function call resolution. examples and best practices for using function overloading and automatic type conversion.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Function name & parameter list Must be ‘unique’ for each function definition
avg = average(5.2, 6.7); Calls ‘two-parameter average()’ avg = average(6.5, 8.5, 4.2); Calls ‘three-parameter average()’
‘Matches’ call with appropriate function Each considered separate function
A mpg() function should always perform same task, in all overloads Otherwise, unpredictable results
st : looks for exact signature
nd : looks for ‘compatible’ signature
Calls # f(5.3, 4);
Calls # f(4.3, 5.2);
Calls ???
Any ‘subordinate’ data automatically promoted int Æ double float Æ double char Æ double *More on this later!
void showVolume( int length, int width = 1, int height = 1); Last 2 arguments are defaulted Possible calls: showVolume(2, 4, 6); //All arguments supplied showVolume(3, 5); //height defaulted to 1 showVolume(7); //width & height defaulted to 1
Display 4.8,page 160
Preconditions & Postconditions Typical assert use: confirm their validity Syntax: assert(<assert_condition>); No return value Evaluates assert_condition Terminates if false, continues if true
Macros used similarly as functions
int coinValue, int& number, int& amountLeft); //Precondition: 0 < coinValue < 100 0 <= amountLeft < //Postcondition: number set to max. number of coins
assert ((0 < currentCoin) && (currentCoin < 100) && (0 <= currentAmountLeft) && (currentAmountLeft < 100)); If precondition not satisfied Æ condition is false Æ program execution terminates!
Turns OFF all assertions throughout program
Turns assertions back on
Each function designed, coded, tested separately Ensures validity of each unit Divide & Conquer Transforms one big task Æ smaller, manageable tasks
Driver programs
Low-level functions last ‘Stub-out’ functions until implementation Example: double unitPrice(int diameter, double price) { return (9.99); // not valid, but noticeably // a ‘temporary’ value } Calls to function will still ‘work’
Test every function in a program where every other function has already been fully tested and debugged Avoids ‘error-cascading’ & conflicting results