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

Function Overloading

The document discusses function overloading in C++. It covers overloading constructor functions, using default arguments, and issues that can cause ambiguity like automatic type conversions. Examples are provided to illustrate different ways of overloading functions and constructors.

Uploaded by

Fazle Rafsani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Function Overloading

The document discusses function overloading in C++. It covers overloading constructor functions, using default arguments, and issues that can cause ambiguity like automatic type conversions. Examples are provided to illustrate different ways of overloading functions and constructors.

Uploaded by

Fazle Rafsani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture Five

Function Overloading

Ref: Herbert Schildt, Teach Yourself C++, Third Edn (Chapter 5)

Dr. M. Mahfuzul Islam


Professor, Dept. of CSE, BUET
Overloading Constructor Functions

It is common to overload a classs constructor function.

It is not possible to overload a destructor function.

Three main reasons to overload constructor function:

- to gain flexibility,
- to support arrays and
- to create copy constructors.
If a program attempts to create an object for which no matching
constructor is found, a compile-time error occurs.
Overloading Constructor Functions

Example for gaining flexibility

#include <iostream> date::date(char *str){


#include <cstdio> sscanf(str, %d%*c%d%*c%d, &day,
using namespace std; &month, &year);
}
class date {
int month, day, year; int main() {
public: date sdate(31/12/99);
date(char *str); date idate(31, 12, 99);
date( int d, int m, int y) ){
day = d; sdate.show();
month = m; idate.show();
year = y; return 0;
} }
void show(){
cout << day << / << month << / ;
cout << year << \n;
}
};
Overloading Constructor Functions

Example for supporting array

#include <iostream> int main() {


using namespace std; myclass o1[10];
myclass o2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9,
class myclass {
10};
int x;
public:
for(int i = 0; i < 10; i++){
myclass() { x = 0;}
cout << o1[ << i << ]: << o1[i].getx();
myclass(int n) { x = n;}
cout << \n;
int getx() {return x;}
cout << o2[ << i << ]: << o2[i].getx();
};
cout << \n;
}
return 0;
}
Creating and Using copy constructor

Problems can occur when an object is passed to or returned from a function.


copy constructor is one of the solutions.
There are two distinct situations for assigning one object to another-
assignment and initialization. The copy constructor only applies to
initializations. It does not apply to assignments.
Common form of assignment:
classname (const classname &object){}
Creating and Using copy constructor

#include <iostream> void show(strtype x){


#include <cstring> char *s;
#include <cstdlib>
using namespace std; s = x.get();
class strtype{ cout << s << \n;
char *p; }
public: int main(){
strtype(char *s); strtype a(Hello), b(There);
~strtype() { delete [] p;}
char *get() { return p; } show(a);
}; show(b);
return 0;
strtype:: strtype(char *s){ }
int l;

l = strlen(s) + 1;
p = new char[l]; What is the problem of the
if (!p){ program?
cout << Allocation error\n;
exit(1);
}
strcopy( p, s );
}
Creating and Using copy constructor
Solving the problem using COPY CONSTRUCTOR" strtype:: strtype(const strtype &o){
#include <iostream> int l;
#include <cstring> l = strlen(o.p) + 1;
#include <cstdlib> p = new char[l];
using namespace std; if (!p){
cout << Allocation error\n;
class strtype{
exit(1);
char *p;
}
public:
strcopy( p, o.p );
strtype(char *s);
}
strtype(const strtype &o);
~strtype() { delete [] p;}
void show(strtype x){
char *get() { return p; }
char *s;
};
strtype:: strtype(char *s){
int l; s = x.get();
cout << s << \n;
l = strlen(s) + 1; }
p = new char[l]; int main(){
if (!p){ strtype a(Hello), b(There);
cout << Allocation error\n;
exit(1); show(a);
} show(b);
strcopy( p, s ); return 0;
} }
Creating and Using copy constructor

The copy constructor is invoked when a function generates the


temporary object.
#include <iostream> myclass myclass::f(){
using namespace std; myclass temp;
return temp;
class myclass {
}
public:
myclass(); int main(){
myclass(const myclass &o); myclass obj;
myclass f();
}; obj = obj.f();
return 0;
myclass:: myclass(){
}
cout << Constructing normally\n;
}
OUTPUT:
myclass:: myclass(const myclass &o){ Constructing normally
cout << Constructing copy\n; Constructing normally
} Constructing copy
The Overload Anachronism

When C++ was first invented, the keyword overload was used to create an
overloaded function.
Overload is obsolete now and no longer supported by modern C++
compilers.

The general form of overload


overload func-name;

Overloading a function called timer():


overload timer;
Using Default Arguments

The defaults can be specified either in function prototype or in its definition


if the definition precedes the functions first use.
The defaults cannot be specified in both the prototype and the definition.
All default parameters must be to the right of any parameters that do not
have defaults.
Default arguments must be constants or global variables. They cannot be
local variables or other parameters.

#include <iostream> Output:


using namespace std; 0 0
10 0
void f(int a = 0, int b = 0){ 10 99
cout << a << << b << \n\;
} void f(int a = 0, int b){
cout << a << << b <<\n;
int main(){ }
f();
f(10);
f(10, 99); Wrong! b must have default, too
}
Using Default Arguments
Default argument can be used instead of function overload
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class myclass {
double rect_area( double length, double width = 0){ int x;
if (!width) width = length; public:
return length*width; myclass(int n = 0) { x = n; }
} int getx() { return x; }
};
int main(){
cout << rect_area(10.0, 5.8) << \n; int main(){
cout << rect_area(10.0) <<\n; myclass o1(10);
return 0; myclass o2;
}
cout << o1.getx() << \n;
cout << o2.getx() << \n;
return 0;
}

It is possible to create copy constructors that take additional arguments, as long as


the additional arguments have default values.
myclass( const myclass &obj, int x = 0){
//body of constructor
}
Overloading and Ambiguity
Automatic type conversion rule cause an ambiguous situation.
#include <iostream> int main(){
using namespace std; float x = 10.09;
double y = 10.09;
float f(float i){
return i / 2.0; cout << f(x)<<\n;
} cout << f(y)<<\n;
cout << f(10)<<\n; // ambiguous
double f(double i){ return 0;
return i / 3.0; }
}

Wrong type of arguments causes an ambiguous situation.


#include <iostream> int main(){
using namespace std; f(c);
f(86); // which f() is called?
void f(unsigned char c){ return 0;
cout << c; }
}

void f(char c){


cout << c;
}
Overloading and Ambiguity
Call by value and call by reference cause an ambiguous situation.
#include <iostream>
using namespace std; int main(){
int x = 1, y = 2;
int f(int a, int b){
return a+b; cout << f(x, y); // which f() is called?
} return 0;
}
int f(int a, int &b){
return a-b;
}

Default argument causes an ambiguous situation.


#include <iostream>
using namespace std; int main(){
cout << f(10, 2);
int f(int a){ cout << f(10); // which f() is called?
return a*a; return 0;
} }

int f(int a, int b = 0){


return a*b;
}
Finding address of an Overloaded Function
A function address is obtained by putting its name on the right side of an
assignment statement without any parenthesis or arguments.
To assign p the address of zap(),
p = zap;
What about overloaded function????

#include <iostream> int main(){


using namespace std; void (*fp1)(int);
void (*fp2)(int, char);
void space(int count){
for( ; count; count--) cout << ; fp1 = space;
} fp2 = space;

void space(int count, char ch){ fp1(22);


for( ; count; count--) cout << ch; cout << \n;
}
fp2(30, x);
cout << \n;

return 0;
}

You might also like