0% found this document useful (0 votes)
9 views15 pages

Function Overloading

Uploaded by

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

Function Overloading

Uploaded by

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

CSE 107: OBJECT ORIENTED

PROGRAMMING LANGUAGE

Made by : Dr. Tanzima Hashem


FUNCTION OVERLOADING
🞆Two or more functions can share the same name as long as either
⚫ The type of their arguments differs, or
⚫ The number of their arguments differs, or
⚫ Both of the above

🞆The compiler will automatically select the correct version

🞆The return type alone is not a sufficient difference to allow function


overloading

2
FUNCTION OVERLOADING
#include <iostream>
using namespace std;

class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
3
}
};
FUNCTION OVERLOADING
int main(void) {
printData pd;

// Call print to print integer


pd.print(5); Printing int: 5
Printing float: 500.263
// Call print to print float Printing character: Hello C++
pd.print(500.263);

// Call print to print character


pd.print("Hello C++");

4
return 0;
}
FUNCTION OVERLOADING
int sum (int x, int y)
{
cout << x+y;
}

double sum(double x, double y)


{
cout << x+y;
}
5
FUNCTION OVERLOADING
int sum (int x, int y)
{
cout << x+y;
}

int sum(int x, int y, int z)


{
cout << x+y+z;
}
6
FUNCTION OVERLOADING
// This is incorrect and will not compile.
int f1 (int a);
double f1 (int a);
.
.
.
f1(10) //which function does the computer call???

7
OVERLOADING CONSTRUCTOR FUNCTIONS
🞆It is possible to overload constructors, but destructors cannot be overloaded

🞆Three main reasons to overload a constructor function


⚫ To gain flexibility
⚫ To support arrays
⚫ To create copy constructors

8
OVERLOADING CONSTRUCTOR FUNCTIONS
🞆Overloading constructor functions also allows the programmer to select the
most convenient method to create objects
⚫ Date d1(22, 9, 2007); // uses Date( int d, int m, int y )
⚫ Date d2(“22-Sep-2007”); // uses Date( char* str )

🞆There must be a constructor function for each way that an object of a class will
be created, otherwise compile-time error occurs
🞆Let, we want to write
⚫ MyClass ob1, ob2(10);
🞆Then MyClass should have the following two constructors (it may have more)
⚫ MyClass ( ) { … }
⚫ MyClass ( int n ) { … }
9
🞆Whenever we write a constructor in a class, the compiler does not supply the
default no argument constructor automatically
OVERLOADING CONSTRUCTOR FUNCTIONS
🞆No argument constructor is also necessary for declaring arrays of objects
without any initialization
⚫ MyClass array1[5];
// uses MyClass () { … } for each element

🞆But with the help of an overloaded constructor, we can also initialize the
elements of an array while declaring it
⚫ MyClass array2[3] = {1, 2, 3}
// uses MyClass ( int n ) { … } for each element

🞆As dynamic arrays of objects cannot be initialized, the class must have a no
argument constructor to avoid compiler error while creating dynamic arrays
using “new”. 10
USING DEFAULT ARGUMENTS
🞆 Related to function overloading
⚫ Essentially a shorthand form of function overloading

🞆 Allows to give a parameter a default value when no corresponding argument is


specified when the function is called
⚫ void f1(int a = 0, int b = 0) { … }
⚫ It can now be called in three different ways
🞆f1(); // inside f1() ‘a’ is ‘0’ and b is ‘0’
🞆f1(10); // inside f1() ‘a’ is ‘10’ and b is ‘0’
🞆f1(10, 99); // inside f1() ‘a’ is ‘10’ and b is ‘99’
⚫ We cannot give ‘b’ a new (non-default) value without specifying a new value for ‘a’
⚫ While specifying non-default values, we have to start from the leftmost parameter
and move to the right one by one
11
USING DEFAULT ARGUMENTS
🞆Default arguments must be specified only once: either in the function’s
prototype or in its definition

🞆All default parameters must be to the right of any parameters that don’t have
defaults
⚫ void f2(int a, int b = 0); // no problem
⚫ void f3(int a, int b = 0, int c = 5); // no problem
⚫ void f4(int a = 1, int b); // compiler error

🞆Default arguments must be constants or global variables.


🞆Default arguments cannot be local variables or other parameters
12
USING DEFAULT ARGUMENTS
🞆Relation between default arguments and function overloading
⚫ void f1( int a = 0, int b = 0 ) { … }
⚫ It acts as the same way as the following overloaded functions –
🞆void f2( ) { int a = 0, b = 0; … }
🞆void f2( int a ) { int b = 0; … }
🞆void f2( int a, int b ) { … }
🞆Constructor functions can also have default arguments

13
OVERLOADING AND AMBIGUITY
🞆Due to automatic type conversion rules
🞆Example 1:
⚫ void f1( float f ) { … }
⚫ void f1( double d ) { … }
⚫ float x = 10.09;
⚫ double y = 10.09;
⚫ f1(x); // unambiguous – use f1(float)
⚫ f1(y); // unambiguous – use f1(double)
⚫ f1(10); // ambiguous, compiler error
🞆Because integer ‘10’ can be promoted to both “float” and “double”.

14
OVERLOADING AND AMBIGUITY
🞆Due to the use of default arguments
🞆Example 3:
⚫ void f3( int a ) { … }
⚫ void f3(int a, int b = 0){…}
⚫ f3(10, 20); // unambiguous – calls f3(int, int)
⚫ f3(10); // ambiguous, compiler error

15

You might also like