Module 08: Programming C++: Default Parameters & Function Overloading
Module 08: Programming C++: Default Parameters & Function Overloading
Module 08: Programming C++: Default Parameters & Function Overloading
Partha Pratim
Das Module 08: Programming C++
Objectives & Default Parameters & Function Overloading
Outline
Default
Parameter
Overload
Resolution Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Default
Parameters in ppd@cse.iitkgp.ernet.in
Overloading
Summary
Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan
Module 08
Default
Parameter
Function
Overloading
Overload
Resolution
Default
Parameters in
Overloading
Summary
Module 08
Module 08
Overload
Resolution
Default
Parameters in
Overloading
Summary
Module 08
Module 08 #include<iostream>
using namespace std;
Partha Pratim
Das
int Add(int a = 10, int b = 20) {
Objectives & return (a + b);
Outline }
Default int main(){
Parameter int x = 5, y = 6, z;
Function
Overloading z = Add(x, y); // Usual function call -- a = x = 5 & b = y = 6
Overload
cout << "Sum = " << z << endl;
Resolution
Default
z = Add(x); // One parameter defaulted -- a = x = 5 & b = 20
Parameters in cout << "Sum = " << z << endl;
Overloading
Module 08
Summary
Summary g(); // Error C2660: ’g’: function does not take 0 arguments
g(i);
g(i, d);
g(i, d, &c);
return 0;
}
Module 08
Overload
Resolution
Default
Parameters in
Overloading
Summary
Module 08
Overload
Resolution
Default
Parameters in
Overloading
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
return 0;
}
NPTEL MOOCs Programming in C++ Partha Pratim Das 12
Function overloads: Matrix Multiplication in C
Similar functions with different data types & algorithms
Module 08
typedef struct { int data[10][10]; } Mat; // 2D Matrix
Partha Pratim typedef struct { int data[1][10]; } VecRow; // Row Vector
Das typedef struct { int data[10][1]; } VecCol; // Column Vector
Module 08
Functions having similar functionality but different in details.
Partha Pratim
Das typedef struct { int data[10][10]; } Mat; // 2D Matrix
typedef struct { int data[1][10]; } VecRow; // Row Vector
Objectives & typedef struct { int data[10][1]; } VecCol; // Column Vector
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 */ };
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
int main() {
Overload
Mat m1, m2, rm; VecRow rv, rrv; VecCol cv, rcv; int r;
Resolution
Multiply(m1, m2, rm); // rm <-- m1 * m2
Default Multiply(m1, cv, rcv); // rcv <-- m1 * cv
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;
}
return 0; return 0;
} }
return 0;
}
Module 08
Overload
Resolution
Default
Parameters in
Overloading
Summary
Module 08
Overload
Resolution
Default
Parameters in
Overloading
Summary
Module 08
Summary
Module 08
Module 08
Summary Resolution:
Candidate functions (by name): F2, F3, F6, F8
Viable functions (by # of parameters): F3, F6
Best viable function (by type double – Exact Match): F6
Module 08
• Consider the overloaded function signatures:
Partha Pratim
Das int fun(float a) {...} // Function 1
int fun(float a, int b) {...} // Function 2
Objectives & int fun(float x, int y = 5) {...} // Function 3
Outline
Summary
• CALL - 1: Matches Function 2 & Function 3
• CALL - 2: Matches Function 1 & Function 3
• Results in ambiguity
return 0;
}
NPTEL MOOCs Programming in C++ Partha Pratim Das 26
Program 08.09:
Default Parameter & Function Overload
Module 08 Function overloading with default parameters may fail
Partha Pratim
Das #include <iostream>
using namespace std;
Objectives & int f();
Outline int f(int = 0);
Default int f(int, int);
Parameter
return 0;
}
NPTEL MOOCs Programming in C++ Partha Pratim Das 27
Module Summary
Module 08
Summary
Module 08
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor ppd@cse.iitkgp.ernet.in 9830030880
Outline Tanwi Mallick, TA tanwimallick@gmail.com 9674277774
Default
Srijoni Majumdar, TA majumdarsrijoni@gmail.com 9674474267
Parameter Himadri B G S Bhuyan, TA himadribhuyan@gmail.com 9438911655
Function
Overloading
Overload
Resolution
Default
Parameters in
Overloading
Summary