You are on page 1of 22

Overview of C++ Overloading

 Same operator or function name used for different


definitions
 Both operators and functions can be overloaded
 Signature consists of operator/function name and
the number and types of its arguments in order
 E.g., add(int, long) and add(long, int) have
different signatures
 Overriding a base class method is also a form of
overloading
 But definitions are separated by scopes
 Compiler can distinguish (dynamically or statically,
depending on whether virtual or not)
The ‘C’ swap() function

 In ‘C’ we need a “swap” function for


each different data type
 void swapInt (int* a, int* b);
 void swapDbl (double* a, double* b);
 void swapBob (struct bob* a, struct bob*
b)
 etc., etc., etc.
 Why??
C++ swap( ) function

 We still need separate functions, but


they can all have the same name
 void swap (int& a, int& b);
 void swap (double& a, double& b);
 void swap (struct bob& a, struct bob& b);
 Is this an improvement?
 Why can I do this in C++ and not C?
Function signatures

 A function signature is what the


compiler/linker uses to identify a
function
 In C, functions are identified only by
their name
 In C++, function signatures include
name, parameters and, (for member
functions) const --- NOT return type
Constructors are functions too

We can create (declare) a file as an


ifstream two ways ---
1. ifstream fileA;
2. ifstream fileB (“filename”);

 These use different constructors


 ifstream ( );
 ifstream (char * filename);
Function Overloading
Overloading Constructor Functions
#include<iostream.h>
class myclass{
int x;
public:
/*overloaded constructor functions two ways*/
myclass(){x=0;}//no initializer
myclass(int n){x=n;}//initializer
int getx(){return x;}
}; (contd..)
Overloading Constructor functions(contd..)
int main()
{
myclass o1(10);//declare with initial
value
myclass o2;//declare without initializer
cout<<o1.getx()<<“\n”;
cout<<o2.getx()<<“\n”;
}
class A {
Function Overloading
public:
int add(int i, int j);

// not allowed, would be  Overloading is resolved by


// ambiguous with above
// long add(int m, int n);
 Finding all possible matches
based on passed arguments
// Ok, different signature  Finding the “best match”
long add(long m, long n);
}; among those
 Signature does not
int
main (int argc, char **argv) { depend on return type
int a = 7;  So, overloading can’t be
int b = 8; resolved by return type
int c = add(a, b); alone
return 0;
}
Function Overloading
 Function overloading
 Having functions with same name and different parameters

 Should perform similar tasks ( i.e., a function to square


ints, and function to square floats).
int square( int x) {return x * x;}
float square(float x) { return x * x; }
 Program chooses function by signature
 signature determined by function name and parameter types
 Can have the same return types
 Rules: functions can differ in parameters and return type,
but not in return type only!
1
2 // Using overloaded functions
3 #include <iostream>
4 Functions have same name but

Example
5 using std::cout; different parameters
6 using std::endl;
7
8 int square( int x ) { return x * x; }
9
10 double square( double y ) { return y * y; }
11
12 int main()
13 {
14 cout << "The square of integer 7 is " << square( 7 )
15 << "\nThe square of double 7.5 is " << square( 7.5 )
16 << endl;
17
18 return 0;
19 }

The square of integer 7 is 49


The square of double 7.5 is 56.25
Function Overloading
 C++ supports writing more than one function
with the same name but different argument
lists. This could include:
 different data types
 different number of arguments

 The advantage is that the same apparent


function can be called to perform similar but
different tasks. The following will show an
example of this.
Function Overloading
void swap (int *a, int *b) ;
void swap (float *c, float *d) ;
void swap (char *p, char *q) ;
int main ( )
{
int a = 4, b = 6 ;
float c = 16.7, d = -7.89 ;
char p = 'M' , q = 'n' ;
swap (&a, &b) ;
swap (&c, &d) ;
swap (&p, &q) ;
Function Overloading
void swap (int *a, int *b)
{ int temp; temp = *a; *a = *b; *b =
temp; }

void swap (float *c, float *d)


{ float temp; temp = *c; *c = *d; *d =
temp; }

void swap (char *p, char *q)


{ char temp; temp = *p; *p = *q; *q =
Default Arguments
This is a feature in C++ that is related to function
overloading.
It allows you to give a parameter a default value when no
corresponding argument is specified when the function is
called.
Example:
void f(int a=0, int b=0)
This function can now be called in three ways:
1.f(); // a and b default set to 0
2. f(10); // a is 10, b default to 0
3. f(10,99); // a is 10, b is 99

Note: In this example, there is no way to default a and


specify b.
Overloading and Ambiguity

Overloading-caused ambiguity can be


introduced through type conversions, reference parameters and
default arguments.
For a program to compile without error, ambiguity must be
removed.

Example
#include<iostream.h>

float f(float i)
{ return i/2.0; }
double f(double i)
{ return i/3.0; } (contd..)
Example(contd..)

int main()
{
float x=4.0;
double y=5.0;

cout<< f(x); //unambiguous –use f(float)


cout<<f(y); //unambiguous- use f(double)
cout<<f(10); //ambiguous,convert 10 to
double or float
}
Copy Constructor
•This is one of the important forms of an overloaded
constructor.
•When an object is passed to a function, a bitwise(i.e.,exact)
copy of that object is made and given to the function
parameter that receives the object. However,there are cases in
which this identical copy is not desirable.
•By creating a copy constructor, we can avoid the problems
created by the bitwise copy of an object .The most common
form of a copy constructor is:
•classname (const classname &obj){
//body of constructor }
Function Templates
 We have discussed overloaded functions as a
way to perform similar operations on data of
different types. The swap functions were an
example.

 We wrote three functions with the same


name but different data types to perform the
swap operations. Then we could call swap
(&a, &b), for example, and C++ would select
which function to use by matching the data
type of a and b to one of the functions.
Function Templates

 Another way to perform this task would be to


create a function template definition.

 With a function template defined, when we


call swap (&a, &b), C++ will generate the
object code functions for us. The program on
the following slides is an example.
Function Templates
template <typename T> void swap (T *a, T *b)
{ T is a “dummy” type that will be
filled in by the compiler as
T temp; needed
temp = *a;
*a = *b; a and b are of “type” T
temp is of “type” T
*b = temp;
} swap is a function template,
NOT a function
Function Templates
int main ( )
{
int a = 5, b = 6;
float c = 7.6, d = 9.8;
char e = 'M', f = 'Z';
swap (&a, &b); // compiler puts int in for T
swap (&c, &d); // compiler puts float in for T
swap (&e, &f); // compiler puts char in for T
cout << "a=" << a << " and b=" << b << endl;
cout << "c=" << c << " and d=" << d << endl;
cout << "e=" << e << " and f=” << f << endl;
}

You might also like