will match both a formal argument of a pointer type and a formal argument
of a numeric type
•
a pointer of any type will match a formal argument of void*
Note:
All standard conversions are treated equally, there is no precedence.
If more than one match is found by standard conversion, a compiler error occurs!
Ambiguity can also be resolved by an explicit cast.
//Example promotion and standard conversion
#include
<iostream>
using namespace
std;
void
ff
(
int
x) {cout<
<"int=
"<<x<<
end
l;}
int
mai
n()
{
ff
(2L);
ff
(0);
ff
(3.14);
ff
('a');
ff
(
tru
e);
return
0;
}

11/29/17
19
//Example promotion and standard conversion
#include
<iostream>
using namespace
std;
void
ff
(
int
x) {cout<
<"int=
"<<x<<
end
l;}
int
mai
n()
{
ff
(2L);
//standard conversion
ff
(0);
//exact match
ff
(3.14);
//standard conversion
ff
('a');
//promotion
ff
(
tru
e);
//standard conversion
return
0;
}
Output
int=2
int=0
int=3
int=97
int=1
Multiple Argument Calls
Matching rules are applied
to each argument in turn
.
If no exact match is found,
the function chosen is the one for which the resolution of each argument
is the
same or better
than for all other functions in the overloaded set, and is strictly
better
than all other functions for at least one argument.
NOTE:
1.
A call is ambiguous if no one function instance contains a better match.
example:
min(long, long);
min(double, double);
min(int x, int y);
//ambiguous, no best match
2. If more than one function contains a better match, then the call is also ambiguous
example:
foo(int, int);
foo(double, double);
foo('a', 3.14F);
//ambiguous, no best match
Both instances of foo() contain a best match through promotion.

11/29/17
20
Default arguments and overloading
Overloaded functions can have default
arguments.
An overloaded function instance with default
arguments will match a call that provides all or
some subset of its arguments.
void ff(int);
void ff(long, int=0);
ff(0,0);
//matches ff(long, int)
ff(0);
//matches ff(int)? No ambiguous
ff(3.14);
//error ambiguous; because both need a
//standard conversion
#include
<iostream>
using namespace
std
;
//prototype
void
test(
int
=
2
,
int
=
4
,
int
=
6
);
int
main() {
test
();
test
(
6
);
test
(
3
,
9
);
test
(
1
,
5
,
7
);
return
0
;
}
//function definition
void
test(
int
a,
int
b,
int
c)
{
cout
<<
a
<<
b
<<
c
<<
endl
;
}
What is the output?
246
646
396
157

11/29/17
21
#include
<iostream>
#include
<string>
using namespace
std
;
int
main(
void
) {
int
a
=
0
,
b
=
0
;
string
s
=
"
Hello
"
;
print
(
a
);
print
(
b
,
3
);
print
(
'
A
'
);
print
(
5
);
print
(
s
);
print
(
1.44
);
print
(
'
a
'
,
'
b
'
);
print
((
double
)a);
return
0
; }
//function definition
void
print(
int
x )
{
cout
<<
"blue\n"
; }
void
print(
int
x,
int
y) {
cout
<<
"purple\n"
; }
void
print(
string
s )
{
cout
<<
"pink\n"
;
}
void
print(
char
x )
{
cout
<<
"red\n"
; }
void
print(
double
x )
{
cout
<<
"green\n"
;
}
//function prototype
void
print(
int
x );
void
print(
int
x,
int
y);
void
print(
string
s );
void
print(
char
x );
void
print(
double
x );
What is the output?
blue
purple
red
blue
pink
green
purple
green
Recursion
• A recursive function is a
function that calls itself
either directly or
indirectly through another
function.

11/29/17
22
Recursive Definitions
•
Definition:
The process of solving a problem by reducing it
to smaller versions of itself is called recursion.


You've reached the end of your free preview.
Want to read all 25 pages?
- Fall '15
- Marinolent