0% found this document useful (0 votes)
18 views24 pages

Module 8

The document discusses default parameters and function overloading in C++. It explains that default parameters allow functions to be called with fewer arguments by specifying default values for some parameters. It also discusses function overloading, which involves defining multiple functions with the same name but different parameters. The document provides examples of functions using default parameters and overloaded functions. It outlines restrictions on default parameters, such as requiring all subsequent parameters to also have defaults.
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)
18 views24 pages

Module 8

The document discusses default parameters and function overloading in C++. It explains that default parameters allow functions to be called with fewer arguments by specifying default values for some parameters. It also discusses function overloading, which involves defining multiple functions with the same name but different parameters. The document provides examples of functions using default parameters and overloaded functions. It outlines restrictions on default parameters, such as requiring all subsequent parameters to also have defaults.
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/ 24

Module 08

Intructors: Abir
Das and
Sourangshu
Bhattacharya
Module 08: Programming C++
Objectives & Default Parameters & Function Overloading
Outline

Default
Parameter
Highlights

Function
Intructors: Abir Das and Sourangshu Bhattacharya
Overloading

Overload
Resolution Department of Computer Science and Engineering
Promotion &
Indian Institute of Technology, Kharagpur
Conversion
{abir, sourangshu}@cse.iitkgp.ac.in
Default
Parameters in
Overloading

Summary Slides taken from NPTEL course on Programming in Modern C++

by Prof. Partha Pratim Das

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 1
Module Objectives

Module 08

Intructors: Abir • Understand default parameters


Das and
Sourangshu
Bhattacharya
• Understand function overloading and Resolution
Objectives &
Outline

Default
Parameter
Highlights

Function
Overloading

Overload
Resolution
Promotion &
Conversion

Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 2
Module Outline

Module 08

Intructors: Abir • Default parameter


Das and
Sourangshu
Bhattacharya
◦ Motivation
◦ Call function with default parameter
Objectives &
Outline
◦ Highlighted Points
Default ◦ Restrictions
Parameter
Highlights • Function overloading
Function
Overloading
◦ Meaning & Motivation
Overload ◦ Necessity of function overloading in Contrast with C
Resolution
Promotion & • Static Polymorphism
Conversion

Default ◦ Meaning
Parameters in
Overloading ◦ Overloading function
Summary • Overload Resolution
• Default parameters and Function Overloading

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 3
Motivation: Example CreateWindow in MSDN
Declaration of CreateWindow Calling CreateWindow
Module 08

Intructors: Abir HWND WINAPI CreateWindow( hWnd = CreateWindow(


Das and
Sourangshu _In_opt_ LPCTSTR lpClassName, ClsName,
Bhattacharya
_In_opt_ LPCTSTR lpWindowName, WndName,
Objectives &
_In_ DWORD dwStyle, WS_OVERLAPPEDWINDOW,
Outline _In_ int x, CW_USEDEFAULT,
Default _In_ int y, CW_USEDEFAULT,
Parameter _In_ int nWidth, CW_USEDEFAULT,
Highlights
_In_ int nHeight, CW_USEDEFAULT,
Function
Overloading
_In_opt_ HWND hWndParent, NULL,
_In_opt_ HMENU hMenu, NULL,
Overload
Resolution _In_opt_ HINSTANCE hInstance, hInstance,
Promotion & _In_opt_ LPVOID lpParam NULL
Conversion
); );
Default
Parameters in
Overloading
• There are 11 parameters in CreateWindow()
Summary • Of these 11, 8 parameters (4 are CWUSEDEFAULT, 3 are NULL, and 1 is hInstance) usually get same
values in most calls
• Instead of using these 8 fixed valued Parameters at call, we may assign the values in formal parameter
• C++ allows us to do so through the mechanism called Default parameters
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 4
Program 08.01: Function with a default parameter

Module 08

Intructors: Abir
#include <iostream>
Das and using namespace std;
Sourangshu
Bhattacharya
int IdentityFunction(int a = 10) { // Default value for parameter a
Objectives & return (a);
Outline
}
Default
Parameter
Highlights int main() {
Function int x = 5, y;
Overloading

Overload y = IdentityFunction(x); // Usual function call. Actual parameter taken as x = 5


Resolution
cout << "y = " << y << endl;
Promotion &
Conversion

Default y = IdentityFunction(); // Uses default parameter. Actual parameter taken as 10


Parameters in
Overloading
cout << "y = " << y << endl;
}
Summary
----------
y = 5
y = 10
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 5
Program 08.02: Function with 2 default parameters
#include<iostream>
Module 08
using namespace std;
Intructors: Abir
Das and
Sourangshu int Add(int a = 10, int b = 20) {
Bhattacharya return (a + b);
Objectives &
}
Outline int main() { int x = 5, y = 6, z;
Default
Parameter z = Add(x, y); // Usual function call -- a = x = 5 & b = y = 6
Highlights
cout << "Sum = " << z << endl;
Function
Overloading
z = Add(x); // One parameter defaulted -- a = x = 5 & b = 20
Overload
Resolution cout << "Sum = " << z << endl;
Promotion &
Conversion
z = Add(); // Both parameter defaulted -- a = 10 & b = 20
Default
Parameters in cout << "Sum = " << z << endl;
Overloading }
Summary ----------
Sum = 11
Sum = 25
Sum = 30
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 6
Default Parameter: Highlighted Points

Module 08

Intructors: Abir • C++ allows programmer to assign default values to the function parameters
Das and
Sourangshu
Bhattacharya
• Default values are specified while prototyping the function
Objectives &
• Default parameters are required while calling functions with fewer arguments or without
Outline any argument
Default
Parameter • Better to use default value for less used parameters
Highlights

Function
Overloading

Overload
Resolution
Promotion &
Conversion

Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 7
Restrictions on default parameters

Module 08
• All parameters to the right of a parameter with default argument must have default
Intructors: Abir
Das and
arguments (function f violates)
Sourangshu
Bhattacharya
• Default arguments cannot be re-defined (second signature of function g violates)
• All non-defaulted parameters needed in a call (first call of g() violates)
Objectives & #include <iostream>
Outline

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


Parameter
// Error C2548: f: missing default parameter for parameter 3
Highlights

Function void g(int, double = 0, char * = NULL); // OK


Overloading
void g(int, double = 1, char * = NULL);
Overload // Error C2572: g: redefinition of default parameter : parameter 3
Resolution // Error C2572: g: redefinition of default parameter : parameter 2
Promotion &
Conversion
int main() {
Default
Parameters in
int i = 5; double d = 1.2; char c = ’b’;
Overloading
g(); // Error C2660: g: function does not take 0 arguments
Summary
g(i);
g(i, d);
g(i, d, &c);
}
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 8
Restrictions on default parameters

Module 08 • Default parameters to be supplied only in a header file and not in the definition of a
Intructors: Abir
function
Das and
Sourangshu // Header file: myFunc.h
Bhattacharya
void g(int, double, char = ’a’); // Defaults ch
Objectives &
void g(int i, double f = 0.0, char ch); // A new overload. Defaults f & ch
Outline void g(int i = 0, double f, char ch); // A new overload. Defaults i, f & ch
// void g(int i = 0, double f = 0.0, char ch = ’a’); // Alternate signature. Defaults all in one go
Default
Parameter ----------------------------------------------------
Highlights // Source File
#include <iostream>
Function
Overloading using namespace std;
#include "myFunc.h" // Defaults taken from header
Overload
Resolution
void g(int i, double d, char c) { cout << i << ’ ’ << d << ’ ’ << c << endl; } // No defaults here
Promotion &
----------------------------------------------------
Conversion // Application File
Default
#include <iostream>
Parameters in #include "myFunc.h"
Overloading int main() { int i = 5; double d = 1.2; char c = ’b’;
Summary g(); // Prints: 0 0 a
g(i); // Prints: 5 0 a
g(i, d); // Prints: 5 1.2 a
g(i, d, c); // Prints: 5 1.2 b
}
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 9
Function overloads: Matrix Multiplication in C

Module 08
• Similar functions with different data types and algorithms
Intructors: Abir typedef struct { int data[10][10]; } Mat; // 2D Matrix
Das and typedef struct { int data[1][10]; } VecRow; // Row Vector
Sourangshu
Bhattacharya typedef struct { int data[10][1]; } VecCol; // Column Vector

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


Outline void Multiply_M_VC (Mat a, VecCol b, VecCol* c); // c = a * b
Default void Multiply_VR_M (VecRow a, Mat b, VecRow* c); // c = a * b
Parameter void Multiply_VC_VR(VecCol a, VecRow b, Mat* c); // c = a * b
Highlights void Multiply_VR_VC(VecRow a, VecCol b, int* c); // c = a * b
Function
Overloading int main() {
Overload
Mat m1, m2, rm; VecRow rv, rrv; VecCol cv, rcv; int r;
Resolution Multiply_M_M (m1, m2, &rm); // rm <-- m1 * m2
Promotion & Multiply_M_VC (m1, cv, &rcv); // rcv <-- m1 * cv
Conversion Multiply_VR_M (rv, m2, &rrv); // rrv <-- rv * m2
Default Multiply_VC_VR(cv, rv, &rm); // rm <-- cv * rv
Parameters in Multiply_VR_VC(rv, cv, &r); // r <-- rv * cv
Overloading
return 0;
Summary }
• 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
• C++ has an elegant solution
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 10
Function overloads: Matrix Multiplication in C++

Module 08
• Functions having the same name, similar functionality but different algorithms, and identified
Intructors: Abir
Das and
by different interfaces data types
Sourangshu typedef struct { int data[10][10]; } Mat; // 2D Matrix
Bhattacharya typedef struct { int data[1][10]; } VecRow; // Row Vector
typedef struct { int data[10][1]; } VecCol; // Column Vector
Objectives &
Outline
void Multiply(const Mat& a, const Mat& b, Mat& c); // c = a * b
Default void Multiply(const Mat& a, const VecCol& b, VecCol& c); // c = a * b
Parameter
void Multiply(const VecRow& a, const Mat& b, VecRow& c); // c = a * b
Highlights
void Multiply(const VecCol& a, const VecRow& b, Mat& c); // c = a * b
Function void Multiply(const VecRow& a, const VecCol& b, int& c); // c = a * b
Overloading

Overload int main() {


Resolution Mat m1, m2, rm; VecRow rv, rrv; VecCol cv, rcv; int r;
Promotion &
Conversion
Multiply(m1, m2, rm); // rm <-- m1 * m2
Multiply(m1, cv, rcv); // rcv <-- m1 * cv
Default
Parameters in
Multiply(rv, m2, rrv); // rrv <-- rv * m2
Overloading Multiply(cv, rv, rm); // rm <-- cv * rv
Multiply(rv, cv, r); // r <-- rv * cv
Summary
return 0;
}
• These 5 functions having different argument types are represented as one function name (Multiply) in C++
• This is called Function Overloading or Static Polymorphism
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 11
Program 08.03/04: Function Overloading

Module 08
• Define multiple functions having the same name
Intructors: Abir
Das and
• Binding happens at compile time
Sourangshu Same # of Parameters Different # of Parameters
Bhattacharya

#include <iostream> #include <iostream>


Objectives &
Outline
using namespace std; using namespace std;
int Add(int a, int b) { return (a + b); } int Area(int a, int b) return (a * b);
Default double Add(double c, double d) { return (c + d); } int Area(int c) { return (c * c); }
Parameter
Highlights
int main() { int main() {
int x = 5, y = 6, z; int x = 10, y = 12, z = 5, t;
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;
Overload
Resolution
double s = 3.5, t = 4.25, u; int z = 5, u;
Promotion &
Conversion u = Add(s, t); // double Add(double, double) u = Area(z); // int Area(int)
cout << "double sum = " << u << endl; cout << " Area of Square = " << u << endl;
Default
Parameters in } }
Overloading

Summary
int sum = 11 double sum = 7.75 Area of Rectangle = 12 Area of Square = 25

• Same Add function to add two ints or two doubles • Same Area function for rectangles and for squares
• Same # of parameters but different types • Different number of parameters

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 12
Program 08.05: Restrictions in Function Overloading

Module 08 • Two functions having the same signature but different return types cannot be overloaded
Intructors: Abir #include <iostream>
Das and using namespace std;
Sourangshu
Bhattacharya
int Area(int a, int b) { return (a * b); }
Objectives & double Area(int a, int b) { return (a * b); }
Outline // Error C2556: double Area(int,int): overloaded function differs only by return type
Default
// from int Area(int,int)
Parameter // Error C2371: Area: redefinition; different basic types
Highlights

Function
int main() {
Overloading int x = 10, y = 12, z = 5, t;
double f;
Overload
Resolution
Promotion & t = Area(x, y);
Conversion // Error C2568: =: unable to resolve function overload
Default // Error C3861: Area: identifier not found
Parameters in
Overloading
cout << "Multiplication = " << t << endl;
Summary
f = Area(y, z); // Errors C2568 and C3861 as above
cout << "Multiplication = " << f << endl;
}

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 13
Function Overloading – Summary of Rules

Module 08 • The same function name may be used in several definitions


Intructors: Abir
Das and
• Functions with the same name must have different number of formal parameters and/or
Sourangshu
Bhattacharya
different types of formal parameters
Objectives &
• Function selection (Overload Resolution) is performed by the compiler
Outline
• Two functions having the same signature but differing only in the return types will
Default
Parameter result in a compilation error. The main reason is caller does not have to use the return
Highlights
value, the compiler does not know which return type is the best match
Function
Overloading
• Two functions having same parameter list but differing only in their default arguments
Overload
Resolution will not compile. Changing the value of a default parameter does not change the type
Promotion &
Conversion
of the parameter
Default
Parameters in
• Overloading allows Static Polymorphism
Overloading
• Overload resolution is considered to be one of the areas of the language that is both
Summary
complex and important. Two good resources:
◦ (Intermediate) Overload Resolution Video by CopperSpice
◦ (Elaborate) MSDN Article on Function Overloading
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 14
Overload Resolution

Module 08

Intructors: Abir • To resolve overloaded functions with one parameter


Das and
Sourangshu
Bhattacharya
◦ Identify the set of Candidate Functions
◦ From the set of candidate functions identify the set of Viable Functions
Objectives &
Outline
◦ Select the Best viable function through (Order is important)
Default ▷ Exact Match
Parameter
Highlights ▷ Promotion
Function ▷ Standard type conversion
Overloading

Overload
▷ User defined type conversion
Resolution
Promotion &
Conversion

Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 15
Overload Resolution: Exact Match

Module 08

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


Das and
Sourangshu
Bhattacharya
◦ Most common
◦ Read more about lvalue and rvalue – internalpointers.com Article
Objectives &
Outline • Array-to-pointer conversion
Default Definitions: int ar[10];
Parameter
Highlights void f(int *a);
Function Call: f(ar)
Overloading

Overload • Qualification conversion


Resolution
Promotion & ◦ Converting pointer (only) to const pointer
Conversion
◦ Converting pointer (only) to volatile pointer
Default
Parameters in ◦ Converting reference (only) to const reference
Overloading

Summary
◦ Converting reference (only) to volatile reference

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 16
Overload Resolution: Promotion & Conversion

Module 08

Intructors: Abir • Promotion


Das and
Sourangshu
Bhattacharya
◦ Objects of an integral type can be converted to another wider integral type, that is,
a type that can represent a larger set of values. This widening type of conversion is
Objectives &
Outline
called integral promotion
Default ◦ C++ promotions are value-preserving, as the value after the promotion is
Parameter
Highlights
guaranteed to be the same as the value before the promotion
Function ◦ Examples
Overloading

Overload
▷ char to int; float to double
Resolution ▷ enum to int / short / unsigned int / ...
Promotion &
Conversion ▷ bool to int
Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 17
Overload Resolution: Promotion & Conversion

Module 08

Intructors: Abir • Standard Conversions


Das and
Sourangshu
Bhattacharya
◦ Integral conversions between integral types – char, short, int, and long with or without
qualifiers signed or unsigned
Objectives &
Outline
◦ Floating point Conversions from less precise floating type to a more precise floating type
Default
like float to double or double to long double. Conversion can happen to a less precise
Parameter type, if it is in a range representable by that type
Highlights
◦ Conversions between integral and floating point types: Certain expressions can cause
Function
Overloading objects of floating type to be converted to integral types, or vice versa. May be dangerous!
Overload ◦ Pointer Conversions: Pointers can be converted during assignment, initialization,
Resolution
Promotion &
comparison, and other expressions
Conversion
◦ Bool Conversion: int to bool or vice versa based on the context
Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 18
Example: Overload Resolution with one parameter

Module 08

Intructors: Abir
• In the context of a list of function prototypes:
Das and int g(double); // F1
Sourangshu
Bhattacharya void f(); // F2
void f(int); // F3
Objectives & double h(void); // F4
Outline int g(char, int); // F5
Default
void f(double, double = 3.4); // F6
Parameter void h(int, double); // F7
Highlights void f(char, char *); // F8
Function The call site to resolve is:
Overloading
f(5.6);
Overload
Resolution • Resolution:
Promotion &
Conversion ◦ Candidate functions (by name): F2, F3, F6, F8
Default ◦ Viable functions (by # of parameters): F3, F6
Parameters in
Overloading
◦ Best viable function (by type double – Exact Match): F6

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 19
Example: Overload Resolution fails

Module 08
• Consider the overloaded function signatures:
Intructors: Abir
Das and int fun(float a) {...} // Function 1
Sourangshu
Bhattacharya int fun(float a, int b) {...} // Function 2
int fun(float x, int y = 5) {...} // Function 3
Objectives &
Outline
int main() {
Default
Parameter
float p = 4.5, t = 10.5;
Highlights int s = 30;
Function
Overloading fun(p, s); // CALL - 1
Overload fun(t); // CALL - 2
Resolution
return 0;
Promotion &
Conversion }
Default
Parameters in • CALL - 1: Matches Function 2 & Function 3
Overloading
• CALL - 2: Matches Function 1 & Function 3
Summary
• Results in ambiguity for both calls

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 20
Program 08.06/07:
Default Parameter & Function Overload
Module 08
• Compilers deal with default parameters as a special case of function overloading
Intructors: Abir
Das and
• These need to be mixed carefully
Sourangshu
Bhattacharya
Default Parameters Function Overload
Objectives &
Outline
#include <iostream> #include <iostream>
using namespace std; using namespace std;
Default int f(int a = 1, int b = 2); int f();
Parameter
Highlights
int f(int);
int f(int, int);
Function
Overloading
int main() { int main() {
Overload int x = 5, y = 6; int x = 5, y = 6;
Resolution
Promotion &
Conversion f(); // a = 1, b = 2 f(); // int f();
f(x); // a = x = 5, b = 2 f(x); // int f(int);
Default
Parameters in f(x, y); // a = x = 5, b = y = 6 f(x, y); // int f(int, int);
Overloading } }
Summary

• f can have 3 possible forms of call • f can have 3 possible forms of call
• No overload here use default parameters.

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 21
Program 08.08:
Default Parameter & Function Overload
Module 08 • Function overloading can use default parameter
Intructors: Abir • However, with default parameters, the overloaded functions should still be resolvable
Das and
Sourangshu #include <iostream>
Bhattacharya using namespace std;
// Overloaded Area functions
Objectives & int Area(int a, int b = 10) { return (a * b); }
Outline
double Area(double c, double d) { return (c * d); }
Default int main() { int x = 10, y = 12, t; double z = 20.5, u = 5.0, f;
Parameter t = Area(x); // Binds int Area(int, int = 10)
Highlights
cout << "Area = " << t << endl; // Area = 100
Function
Overloading t = Area(x, y); // Binds int Area(int, int = 10)
Overload cout << "Area = " << t << endl; // Area = 120
Resolution
Promotion & f = Area(z, u); // Binds double Area(double, double)
Conversion
cout << "Area = " << f << endl; // Area = 102.5
Default
Parameters in
Overloading f = Area(z); // Binds int Area(int, int = 10)
cout << "Area = " << f << endl; // Area = 200
Summary

// 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
}
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 22
Program 08.09:
Default Parameter & Function Overload
Module 08
• Function overloading with default parameters may fail
Intructors: Abir
Das and
Sourangshu #include <iostream>
Bhattacharya using namespace std;
int f();
Objectives & int f(int = 0);
Outline int f(int, int);
Default
Parameter int main() {
Highlights int x = 5, y = 6;
Function
Overloading f(); // Error C2668: f: ambiguous call to overloaded function
Overload // More than one instance of overloaded function f
Resolution // matches the argument list:
Promotion & // function f()
Conversion
// function f(int = 0)
Default
Parameters in
Overloading
f(x); // int f(int);
f(x, y); // int f(int, int);
Summary
return 0;
}

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 23
Module Summary

Module 08

Intructors: Abir • Introduced the notion of Default parameters and discussed several examples
Das and
Sourangshu
Bhattacharya
• Identified the necessity of function overloading
Objectives &
• Introduced static Polymorphism and discussed examples and restrictions
Outline
• Discussed an outline for Overload resolution
Default
Parameter • Discussed the mix of default Parameters and function overloading
Highlights

Function
Overloading

Overload
Resolution
Promotion &
Conversion

Default
Parameters in
Overloading

Summary

NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 24

You might also like