Function Overloading (ad hoc polymorphism)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

what does polymorphism mean?

"poly" - means many and morphe meaning form, shape; it provides different implementations depending on the type of arguments to which its applied

how many functions does the compiler find

1 function whose parameters are the best match for the actual argument

function overloading

also known as ad hoc polymorphism

what types of matches are included?

an exact match a match through a promotion ex: char to int, float to double etc match through standard conversions int to double, double to int etc.

overloaded functions

functions that have the same name, but different parameter lists and appear in the same scope

how does the compiler determine which function to call?

it compares the arguments against the parameters of each function in the set of overloaded functions

what is the motivation behind function overloading?

it eliminates the need to define different names for functions that perform the same general action but on different parameter types.

are there functions with parameters that are a best match (exact match or compatible with ) the arguments

no; the compiler will report that there is no match

what attributes are associated with different parameter configurations?

number of parameters types of parameters order for parameter types

overload resolution

process by which the compiler determines which specific function is called from a set of overloaded functions

which ones are safe vs not safe

safe: exact match; match through promotion unsafe: standard conversions

when we overload the fcuntions, we are creating multiple functions that have the

same name different parameter configurations

ex of function overloading

string to_str(double) { stringstream ss; ss<<d; return ss.str(); } string to_str(char) { stringstream ss; ss<<c; return ss.str(); }

when a name is semantically significant?

the convenience of overloading becomes practically essential

what types of functions does c++ forbid?

the functions that differ only in return type; this would introduce ambiguity as to which function is to be called

what does ad hoc refer to?

the notion that the overloaded functions have been defined explicitly for distinct parameter configurations

what if there is more than one function that matches and amongst the matches?

there isn't a best match and the compiler will report an ambigious call

what can we do instead of providing different names?

we can use the same name and let the compiler figure out which function to call based on the types and arguments in the call

when should u use function overloading?

when a name is semantically significant amongst different data types otherwise should probably construct functions that are id by different names


Set pelajaran terkait

Lsn 21 Homework, VAP 11.8 - 11.13

View Set

Health Exam Simulated Study Guide

View Set

Intro to Computer Forensics Quiz

View Set

Texas Promulgated Forms PRACTICE EXAM 1

View Set

Internal Environment -Value Chain Analysis

View Set

Pharmacology - Chapters 30, 32, 33, 34

View Set