Professional Documents
Culture Documents
Lecture 05
Lecture 05
(Function Overloading)
Rizoan Toufiq1
1 Assistant Professor
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
#include<iostream>
using namespace std;
class myclass{
int a;
public:
myclass(int i){a = i;}
int get_a(){return a;}
};
int main(){
myclass obj;//no matching function error
cout<<obj.get_a();
return 0;
}
#include<iostream>
using namespace std;
class myclass{
int a;
public:
myclass(){a = 0;}
int get_a(){return a;}
};
int main(){
myclass obj(10);//no matching function error
cout<<obj.get_a();
return 0;
}
#include<iostream>
using namespace std;
class myclass{
int a;
public:
myclass(){a=0;}
myclass(int i){a = i;}
int get_a(){return a;}
};
int main(){
myclass obj(10);
myclass obj1;
cout<<obj.get_a()<< " "<< obj1.get_a();
return 0;
}
To support arrays
To support arrays
To support arrays
To gain Flexibility
To gain Flexibility
To gain Flexibility
int i;
// initialize all elements to ob
for (i=0; i <10; i++)
p[i] = ob;
for (i=0; i <10; i++){
cout << "p[" << i << "]: " << p[i]. getx ();
cout << \ n ;
}
return 0;
}
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
1 Assignment
2 Initialization which can occur three ways:
1 When an object is used to initialize another in a declaration statement.
2 When an object is passed as a parameter to a function, and
3 When a temporary object is created for use as a return value of a
function.
Example 01.
Example 01.
~ array () { delete [] p; }
// copy constructor
array ( const array &a);
void put (int i, int j){
if(i >=0 && i< size )
p[i] = j;
}
int get (int i){
return p[i];
}
};
Example 01.
Example 01.
Example 01.
Example 01.
array a (10) ;
array b (10) ;
b = a; // does not call copy constructor
// Copy constructor
strtype :: strtype ( const strtype &o)
{
int l;
l = strlen (o.p)+1;
p = new char [l]; // allocate memory for new copy
if (!p){
cout << " Allocation error \n";
exit (1) ;
}
strcpy (p, o.p); // copy string into copy
}
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
1. Ambiguity Error
1. Ambiguity Error
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
p = zap;
Example01:
Example01:
int main(){
/*Create a pointer to void function with one int
parameter*/
void (* fp1 )(int );
/*Create a pointer to void function with one int
parameter and one character parameter .*/
void (* fp2 )(int , char );
Example01:
1 Function Overloading
Overloading Constructor Function
Creating and Using a Copy Constructor
Using Default Arguments
Overloading and Ambiguity
Finding an address of an overloading function
2 Resources
Herbert Schildt,
Teach Yourself C++
- Chapter 5
Walter Savitch
Problem Solving with C++
Herbert Schildt
Teach Yourself C++
Y. Daniel Liang
Introduction to Java programming
Paul Deitel
Java How to program
Herbert Schildt
Java: the complete reference