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

Module 08 Default Fn Overloading

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module 08 Default Fn Overloading

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Module M08

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outline
Module M08: Default Parameters & Function Overloading

T
Default
Parameters
Examples

P
Highlights
Restrictions
Partha Pratim Das

N
Function
Overloading
Examples
Restrictions
Department of Computer Science and Engineering
Rules
Indian Institute of Technology, Kharagpur
Overload
Resolution
[email protected]
Exact Match
Promotion &
Conversion All url’s in this module have been accessed in September, 2021 and found to be functional
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.1


Module Recap

Module M08

• Introduced reference in C++

L
Partha Pratim
Das
• Studied the difference between call-by-value and call-by-reference

E
Objectives &
Outline • Studied the difference between return-by-value and return-by-reference

T
Default
Parameters • Discussed the difference between References and Pointers
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.2


Module Objectives

Module M08

• Understand Default Parameters

L
Partha Pratim
Das
• Understand Function Overloading and Resolution

E
Objectives &
Outline

T
Default
Parameters
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.3


Module Outline

Module M08 1 Default Parameters


Examples

L
Partha Pratim
Das
Highlights

E
Objectives &
Outline Restrictions on default parameters

T
Default
Parameters 2 Function Overloading
Examples
Examples

P
Highlights
Restrictions Restrictions

N
Function
Overloading
Rules
Examples
Restrictions
3 Overload Resolution
Rules Exact Match
Overload
Resolution
Promotion & Conversion
Exact Match Examples
Promotion &
Conversion Ambiguity
Examples
Ambiguity 4 How to overload Default Parameter?
Defa. Parames in
Overloading 5 Module Summary
Module Summary Programming in Modern C++ Partha Pratim Das M08.4
Default Parameters

Module M08

L
Partha Pratim
Das

E
Objectives &
Outline

T
Default
Parameters
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Default Parameters
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.5


Motivation: Example CreateWindow in MSVC++
Declaration of CreateWindow Calling CreateWindow
Module M08

L
Partha Pratim HWND WINAPI CreateWindow( hWnd = CreateWindow(
Das
_In_opt_ LPCTSTR lpClassName, ClsName,

E
Objectives & _In_opt_ LPCTSTR lpWindowName, WndName,
Outline
_In_ DWORD dwStyle, WS_OVERLAPPEDWINDOW,

T
Default _In_ int x, CW_USEDEFAULT,
Parameters
Examples
_In_ int y, CW_USEDEFAULT,

P
Highlights _In_ int nWidth, CW_USEDEFAULT,
Restrictions _In_ int nHeight, CW_USEDEFAULT,

N
Function _In_opt_ HWND hWndParent, NULL,
Overloading
Examples
_In_opt_ HMENU hMenu, NULL,
Restrictions _In_opt_ HINSTANCE hInstance, hInstance,
Rules _In_opt_ LPVOID lpParam NULL
Overload ); );
Resolution
Exact Match
Promotion & • There are 11 parameters in CreateWindow()
Conversion
Examples
• Of these 11, 8 parameters (4 are CWUSEDEFAULT, 3 are NULL, and 1 is hInstance) usually get same
Ambiguity values in most calls
Defa. Parames in • Instead of using these 8 fixed valued Parameters at call, we may assign the values in formal parameter
Overloading • C++ allows us to do so through the mechanism called Default parameters
Module Summary Programming in Modern C++ Partha Pratim Das M08.6
Program 08.01: Function with a default parameter

Module M08
#include <iostream>

L
Partha Pratim
Das using namespace std;

E
Objectives &
Outline int IdentityFunction(int a = 10) { // Default value for parameter a
return (a);

T
Default
Parameters }
Examples

P
Highlights
Restrictions
int main() {
int x = 5, y;

N
Function
Overloading
Examples y = IdentityFunction(x); // Usual function call. Actual parameter taken as x = 5
Restrictions
Rules
cout << "y = " << y << endl;
Overload
Resolution y = IdentityFunction(); // Uses default parameter. Actual parameter taken as 10
Exact Match cout << "y = " << y << endl;
Promotion &
Conversion
}
Examples ----------
Ambiguity y = 5
Defa. Parames in y = 10
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.7


Program 08.02: Function with 2 default parameters
#include<iostream>
Module M08
using namespace std;

L
Partha Pratim
Das
int Add(int a = 10, int b = 20) {

E
Objectives & return (a + b);
Outline
}

T
Default int main() { int x = 5, y = 6, z;
Parameters
Examples

P
Highlights z = Add(x, y); // Usual function call -- a = x = 5 & b = y = 6
Restrictions cout << "Sum = " << z << endl;

N
Function
Overloading
Examples
z = Add(x); // One parameter defaulted -- a = x = 5 & b = 20
Restrictions cout << "Sum = " << z << endl;
Rules

Overload z = Add(); // Both parameter defaulted -- a = 10 & b = 20


Resolution
cout << "Sum = " << z << endl;
Exact Match
Promotion &
}
Conversion
----------
Examples
Ambiguity
Sum = 11
Defa. Parames in
Sum = 25
Overloading Sum = 30
Module Summary Programming in Modern C++ Partha Pratim Das M08.8
Default Parameter: Highlighted Points

Module M08

• C++ allows programmer to assign default values to the function parameters

L
Partha Pratim
Das
• Default values are specified while prototyping the function

E
Objectives &
Outline • Default parameters are required while calling functions with fewer arguments or without

T
Default
Parameters
any argument
Examples
• Better to use default value for less used parameters

P
Highlights
Restrictions
• Default arguments may be expressions also

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.9


Restrictions on default parameters

Module M08
• All parameters to the right of a parameter with default argument must have default
arguments (function f violates)

L
Partha Pratim
Das
• Default arguments cannot be re-defined (second signature of function g violates)

E
Objectives &
Outline • All non-defaulted parameters needed in a call (first call of g() violates)
#include <iostream>

T
Default
Parameters
Examples void f(int, double = 0.0, char *);

P
Highlights // Error C2548: f: missing default parameter for parameter 3
Restrictions

N
Function void g(int, double = 0, char * = NULL); // OK
Overloading void g(int, double = 1, char * = NULL);
Examples // Error C2572: g: redefinition of default parameter : parameter 3
Restrictions // Error C2572: g: redefinition of default parameter : parameter 2
Rules

Overload int main() {


Resolution int i = 5; double d = 1.2; char c = ’b’;
Exact Match
Promotion &
Conversion g(); // Error C2660: g: function does not take 0 arguments
Examples g(i);
Ambiguity g(i, d);
Defa. Parames in
g(i, d, &c);
Overloading }
Module Summary Programming in Modern C++ Partha Pratim Das M08.10
Restrictions on default parameters

Module M08 • Default parameters to be supplied only in a header file and not in the definition of a
function

L
Partha Pratim
Das
// Header file: myFunc.h

E
Objectives & void g(int, double, char = ’a’); // Defaults ch
Outline
void g(int i, double f = 0.0, char ch); // A new overload. Defaults f & ch

T
Default void g(int i = 0, double f, char ch); // A new overload. Defaults i, f & ch
Parameters
// void g(int i = 0, double f = 0.0, char ch = ’a’); // Alternate signature. Defaults all in one go
Examples

P
----------------------------------------------------
Highlights
// Source File
Restrictions
#include <iostream>

N
Function using namespace std;
Overloading
#include "myFunc.h" // Defaults taken from header
Examples
Restrictions
void g(int i, double d, char c) { cout << i << ’ ’ << d << ’ ’ << c << endl; } // No defaults here
Rules
----------------------------------------------------
// Application File
Overload
Resolution
#include <iostream>
Exact Match
#include "myFunc.h"
Promotion &
int main() { int i = 5; double d = 1.2; char c = ’b’;
Conversion g(); // Prints: 0 0 a
Examples g(i); // Prints: 5 0 a
Ambiguity
g(i, d); // Prints: 5 1.2 a
Defa. Parames in g(i, d, c); // Prints: 5 1.2 b
Overloading }
Module Summary Programming in Modern C++ Partha Pratim Das M08.11
Function Overloading

Module M08

L
Partha Pratim
Das

E
Objectives &
Outline

T
Default
Parameters
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Function Overloading
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.12


Function overloads: Matrix Multiplication in C

Module M08
• Similar functions with different data types and algorithms
typedef struct { int data[10][10]; } Mat;

L
Partha Pratim // 2D Matrix
Das typedef struct { int data[1][10]; } VecRow; // Row Vector
typedef struct { int data[10][1]; } VecCol; // Column Vector

E
Objectives &
Outline
void Multiply_M_M (Mat a, Mat b, Mat* c); // c = a * b

T
Default
Parameters
void Multiply_M_VC (Mat a, VecCol b, VecCol* c); // c = a * b
Examples
void Multiply_VR_M (VecRow a, Mat b, VecRow* c); // c = a * b

P
Highlights
void Multiply_VC_VR(VecCol a, VecRow b, Mat* c); // c = a * b
Restrictions void Multiply_VR_VC(VecRow a, VecCol b, int* c); // c = a * b

N
Function
Overloading int main() {
Examples Mat m1, m2, rm; VecRow rv, rrv; VecCol cv, rcv; int r;
Restrictions Multiply_M_M (m1, m2, &rm); // rm <-- m1 * m2
Rules Multiply_M_VC (m1, cv, &rcv); // rcv <-- m1 * cv
Overload
Multiply_VR_M (rv, m2, &rrv); // rrv <-- rv * m2
Resolution Multiply_VC_VR(cv, rv, &rm); // rm <-- cv * rv
Exact Match Multiply_VR_VC(rv, cv, &r); // r <-- rv * cv
Promotion & return 0;
Conversion
}
Examples
Ambiguity • 5 multiplication functions share similar functionality but different argument types
• C treats them by 5 different function names. Makes it difficult for the user to remember and use
Defa. Parames in
Overloading • C++ has an elegant solution
Module Summary Programming in Modern C++ Partha Pratim Das M08.13
Function overloads: Matrix Multiplication in C++

Module M08
• Functions having the same name, similar functionality but different algorithms, and identified
by different interfaces data types

L
Partha Pratim
Das
typedef struct { int data[10][10]; } Mat; // 2D Matrix
typedef struct { int data[1][10]; } VecRow; // Row Vector

E
Objectives &
Outline typedef struct { int data[10][1]; } VecCol; // Column Vector

T
Default
Parameters void Multiply(const Mat& a, const Mat& b, Mat& c); // c = a * b
Examples void Multiply(const Mat& a, const VecCol& b, VecCol& c); // c = a * b

P
Highlights void Multiply(const VecRow& a, const Mat& b, VecRow& c); // c = a * b
Restrictions void Multiply(const VecCol& a, const VecRow& b, Mat& c); // c = a * b

N
Function void Multiply(const VecRow& a, const VecCol& b, int& c); // c = a * b
Overloading
Examples int main() {
Restrictions Mat m1, m2, rm; VecRow rv, rrv; VecCol cv, rcv; int r;
Rules
Multiply(m1, m2, rm); // rm <-- m1 * m2
Overload Multiply(m1, cv, rcv); // rcv <-- m1 * cv
Resolution Multiply(rv, m2, rrv); // rrv <-- rv * m2
Exact Match
Multiply(cv, rv, rm); // rm <-- cv * rv
Promotion &
Conversion Multiply(rv, cv, r); // r <-- rv * cv
Examples return 0;
Ambiguity }
Defa. Parames in • These 5 functions having different argument types are represented as one function name (Multiply) in C++
Overloading • This is called Function Overloading or Static Polymorphism
Module Summary Programming in Modern C++ Partha Pratim Das M08.14
Program 08.03/04: Function Overloading

Module M08
• Define multiple functions having the same name
• Binding happens at compile time

L
Partha Pratim
Das
Same # of Parameters Different # of Parameters

E
Objectives &
Outline #include <iostream> #include <iostream>

T
Default using namespace std; using namespace std;
Parameters int Add(int a, int b) { return (a + b); } int Area(int a, int b) return (a * b);
Examples double Add(double c, double d) { return (c + d); } int Area(int c) { return (c * c); }

P
Highlights
int main() { int main() {
Restrictions
int x = 5, y = 6, z; int x = 10, y = 12, z = 5, t;

N
Function z = Add(x, y); // int Add(int, int) t = Area(x, y); // int Area(int, int)
Overloading cout << "int sum = " << z; cout << "Area of Rectangle = " << t;
Examples
Restrictions
double s = 3.5, t = 4.25, u; int z = 5, u;
Rules
u = Add(s, t); // double Add(double, double) u = Area(z); // int Area(int)
Overload cout << "double sum = " << u << endl; cout << " Area of Square = " << u << endl;
Resolution
} }
Exact Match
Promotion &
Conversion int sum = 11 double sum = 7.75 Area of Rectangle = 12 Area of Square = 25
Examples
Ambiguity • Same Add function to add two ints or two doubles • Same Area function for rectangles and for squares
Defa. Parames in • Same # of parameters but different types • Different number of parameters
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.15


Program 08.05: Restrictions in Function Overloading

Module M08 • Two functions having the same signature but different return types cannot be overloaded
#include <iostream>

L
Partha Pratim
Das using namespace std;

E
Objectives &
Outline
int Area(int a, int b) { return (a * b); }
double Area(int a, int b) { return (a * b); }

T
Default // Error C2556: double Area(int,int): overloaded function differs only by return type
Parameters
// from int Area(int,int)
Examples

P
Highlights
// Error C2371: Area: redefinition; different basic types
Restrictions
int main() {

N
Function
Overloading
int x = 10, y = 12, z = 5, t;
Examples
double f;
Restrictions
Rules t = Area(x, y);
// Error C2568: =: unable to resolve function overload
Overload
Resolution // Error C3861: Area: identifier not found
Exact Match
Promotion & cout << "Multiplication = " << t << endl;
Conversion
Examples
f = Area(y, z); // Errors C2568 and C3861 as above
Ambiguity
cout << "Multiplication = " << f << endl;
Defa. Parames in }
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.16


Function Overloading: Summary of Rules

Module M08

• The same function name may be used in several definitions

L
Partha Pratim
Das
• Functions with the same name must have different number of formal parameters and/or

E
Objectives &
Outline different types of formal parameters

T
Default
Parameters
• Function selection is based on the number and the types of the actual parameters at
Examples the places of invocation

P
Highlights
Restrictions
• Function selection (Overload Resolution) is performed by the compiler

N
Function
Overloading • Two functions having the same signature but different return types will result in a
Examples
Restrictions
compilation error due to attempt to re-declare
Rules
• Overloading allows Static Polymorphism
Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.17


Overload Resolution

Module M08

L
Partha Pratim
Das

E
Objectives &
Outline

T
Default
Parameters
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Overload Resolution
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.18


Overload Resolution

Module M08

• To resolve overloaded functions with one parameter

L
Partha Pratim
Das
◦ Identify the set of Candidate Functions

E
Objectives &
Outline ◦ From the set of candidate functions identify the set of Viable Functions
◦ Select the Best viable function through (Order is important)

T
Default
Parameters
Examples . Exact Match

P
Highlights
Restrictions
. Promotion
. Standard type conversion

N
Function
Overloading
Examples
. User defined type conversion
Restrictions
Rules

Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.19


Overload Resolution: Exact Match

Module M08

• lvalue-to-rvalue conversion: Read the value from an object

L
Partha Pratim
Das
◦ Most common

E
Objectives &
Outline • Array-to-pointer conversion

T
Default
Parameters
Definitions: int ar[10];
Examples void f(int *a);

P
Highlights
Restrictions
Call: f(ar)

N
Function
Overloading
Definitions: typedef int (*fp) (int);
Examples void f(int, fp);
Restrictions • Function-to-pointer conversion
Rules int g(int);
Overload Call: f(5, g)
Resolution
Exact Match • Qualification conversion
Promotion &
Conversion
Examples
◦ Converting pointer (only) to const pointer
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.20


Overload Resolution: Promotion & Conversion

Module M08

• Promotion

L
Partha Pratim
Das
◦ Objects of an integral type can be converted to another wider integral type, that is,

E
Objectives &
Outline a type that can represent a larger set of values. This widening type of conversion is
called integral promotion

T
Default
Parameters
Examples
◦ C++ promotions are value-preserving, as the value after the promotion is

P
Highlights
Restrictions
guaranteed to be the same as the value before the promotion
◦ Examples

N
Function
Overloading
Examples
. char to int; float to double
Restrictions . enum to int / short / unsigned int / ...
Rules

Overload
. bool to int
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.21


Overload Resolution: Promotion & Conversion

Module M08 • Standard Conversions


◦ Integral conversions between integral types – char, short, int, and long with or without

L
Partha Pratim
Das
qualifiers signed or unsigned

E
Objectives &
Outline ◦ Floating point Conversions from less precise floating type to a more precise floating type
like float to double or double to long double. Conversion can happen to a less precise

T
Default
Parameters
type, if it is in a range representable by that type
Examples

P
Highlights ◦ Conversions between integral and floating point types: Certain expressions can cause
Restrictions
objects of floating type to be converted to integral types, or vice versa. May be dangerous!

N
Function
Overloading . When an object of integral type is converted to a floating type, and the original value is
Examples
not representable exactly, the result is either the next higher or the next lower
Restrictions
Rules representable value
Overload . When an object of floating type is converted to an integral type, the fractional part is
Resolution
Exact Match
truncated, or rounded toward zero. A number like 1.3 is converted to 1, and -1.3 is
Promotion & converted to -1
Conversion
Examples ◦ Pointer Conversions: Pointers can be converted during assignment, initialization,
Ambiguity
comparison, and other expressions
Defa. Parames in
Overloading ◦ Bool Conversion: int to bool or vice versa based on the context
Module Summary Programming in Modern C++ Partha Pratim Das M08.22
Example: Overload Resolution with one parameter

Module M08
• In the context of a list of function prototypes:

L
Partha Pratim
Das int g(double); // F1
void f(); // F2

E
Objectives &
Outline
void f(int); // F3
double h(void); // F4

T
Default int g(char, int); // F5
Parameters
Examples
void f(double, double = 3.4); // F6

P
Highlights
void h(int, double); // F7
Restrictions void f(char, char *); // F8
The call site to resolve is:

N
Function
Overloading
Examples
f(5.6);
Restrictions
Rules
• Resolution:
Overload
◦ Candidate functions (by name): F2, F3, F6, F8
Resolution ◦ Viable functions (by # of parameters): F3, F6
Exact Match ◦ Best viable function (by type double – Exact Match): F6
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.23


Example: Ambiguity in Overload Resolution

Module M08
• Consider the overloaded function signatures:

L
Partha Pratim
Das int fun(float a) {...} // Function 1
int fun(float a, int b) {...} // Function 2

E
Objectives &
Outline int fun(float x, int y = 5) {...} // Function 3

T
Default
Parameters int main() {
Examples
float p = 4.5, t = 10.5;

P
Highlights
Restrictions
int s = 30;

N
Function
Overloading fun(p, s); // CALL - 1
Examples fun(t); // CALL - 2
Restrictions
Rules
return 0;
}
Overload
Resolution
• CALL - 1: Matches Function 2 & Function 3
Exact Match
Promotion & • CALL - 2: Matches Function 1 & Function 3
Conversion
Examples • Results in ambiguity for both calls
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.24


Default Parameters in Overloading

Module M08

L
Partha Pratim
Das

E
Objectives &
Outline

T
Default
Parameters
Examples

P
Highlights
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Default Parameters in Overloading
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.25


Program 08.06/07: Default Parameter & Function Overload

Module M08
• Compilers deal with default parameters as a special case of function overloading
• These need to be mixed carefully

L
Partha Pratim
Das

E
Objectives & Default Parameters Function Overload
Outline

T
Default #include <iostream> #include <iostream>
Parameters using namespace std; using namespace std;
Examples int f(int a = 1, int b = 2); int f();

P
Highlights
int f(int);
Restrictions
int f(int, int);

N
Function
Overloading
int main() { int main() {
Examples
int x = 5, y = 6; int x = 5, y = 6;
Restrictions
Rules
f(); // a = 1, b = 2 f(); // int f();
Overload f(x); // a = x = 5, b = 2 f(x); // int f(int);
Resolution
f(x, y); // a = x = 5, b = y = 6 f(x, y); // int f(int, int);
Exact Match
Promotion &
} }
Conversion
Examples
Ambiguity • Function f has 2 parameters defaulted • Function f is overloaded with up to 2 parameters
Defa. Parames in • f can have 3 possible forms of call • f can have 3 possible forms of call
Overloading • No overload here use default parameters. Can it?
Module Summary Programming in Modern C++ Partha Pratim Das M08.26
Program 08.08: Default Parameter & Function Overload

Module M08 • Function overloading can use default parameter


• However, with default parameters, the overloaded functions should still be resolvable

L
Partha Pratim
Das
#include <iostream>

E
Objectives & using namespace std;
Outline // Overloaded Area functions
int Area(int a, int b = 10) { return (a * b); }

T
Default
Parameters double Area(double c, double d) { return (c * d); }
Examples int main() { int x = 10, y = 12, t; double z = 20.5, u = 5.0, f;

P
Highlights t = Area(x); // Binds int Area(int, int = 10)
Restrictions cout << "Area = " << t << endl; // Area = 100

N
Function
Overloading t = Area(x, y); // Binds int Area(int, int = 10)
Examples cout << "Area = " << t << endl; // Area = 120
Restrictions
Rules f = Area(z, u); // Binds double Area(double, double)
Overload cout << "Area = " << f << endl; // Area = 102.5
Resolution
Exact Match f = Area(z); // Binds int Area(int, int = 10)
Promotion &
Conversion
cout << "Area = " << f << endl; // Area = 200
Examples
Ambiguity // Un-resolvable between int Area(int a, int b = 10) and double Area(double c, double d)
f = Area(z, y); // Error: call of overloaded Area(double&, int&) is ambiguous
Defa. Parames in
Overloading }
Module Summary Programming in Modern C++ Partha Pratim Das M08.27
Program 08.09: Default Parameter & Function Overload

Module M08
• Function overloading with default parameters may fail

L
Partha Pratim
Das
#include <iostream>
using namespace std;

E
Objectives &
Outline int f();
int f(int = 0);

T
Default
Parameters int f(int, int);
Examples

P
Highlights int main() {
Restrictions int x = 5, y = 6;

N
Function
Overloading f(); // Error C2668: f: ambiguous call to overloaded function
Examples // More than one instance of overloaded function f
Restrictions // matches the argument list:
Rules // function f()
Overload // function f(int = 0)
Resolution
Exact Match f(x); // int f(int);
Promotion & f(x, y); // int f(int, int);
Conversion
Examples
Ambiguity
return 0;
}
Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.28


Module Summary

Module M08

• Introduced the notion of Default parameters and discussed several examples

L
Partha Pratim
Das
• Identified the necessity of function overloading

E
Objectives &
Outline • Introduced static Polymorphism and discussed examples and restrictions

T
Default
Parameters • Discussed an outline for Overload resolution
Examples

P
Highlights • Discussed the mix of default Parameters and function overloading
Restrictions

N
Function
Overloading
Examples
Restrictions
Rules

Overload
Resolution
Exact Match
Promotion &
Conversion
Examples
Ambiguity

Defa. Parames in
Overloading

Module Summary Programming in Modern C++ Partha Pratim Das M08.29

You might also like