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

CPP_Segment_03

The document provides an overview of function overloading in C++, including concepts such as function overloading, constructor overloading, copy constructors, default arguments, and overloading ambiguity. It explains how multiple functions can share the same name but differ in parameters, enhancing program readability and demonstrating polymorphism. Additionally, it covers rules for default arguments and scenarios that may lead to ambiguity in function calls.

Uploaded by

ET213004 Joy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CPP_Segment_03

The document provides an overview of function overloading in C++, including concepts such as function overloading, constructor overloading, copy constructors, default arguments, and overloading ambiguity. It explains how multiple functions can share the same name but differ in parameters, enhancing program readability and demonstrating polymorphism. Additionally, it covers rules for default arguments and scenarios that may lead to ambiguity in function calls.

Uploaded by

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

Computer Programming II(CSE-1205)

Engr Md.Eftekhar Alam


Assistant Professor(CSE)
Electrical and Electronic Engineering (EEE) Department,
International Islamic University Chittagong (IIUC).
Computer Programming II [C++]
Segment 03
Function Overloading
Topics:
1. Overloading Function
2. Constructor Overloading
3. Copy Constructor
4. Default Arguments
5. Overloading Ambiguity

2
Function overloading
If any class have multiple functions with same names but different parameters then
they are said to be overloaded.
 Function overloading allows you to use the same name for different functions, to
perform, either same or different functions in the same class.
 Function overloading can be considered as an example of polymorphism feature in
C++.
 Function overloading is usually used to enhance the readability of the program.
 If you have to perform one single operation but with different number or types of
arguments, then you can simply overload the function.

3
Function overloading cont.
Ways to overload a function
 By changing number of Arguments.
 By having different types of argument.

4
Function overloading cont.
Different number of Arguments
 In this type of function overloading we define two functions with same names but different number
of parameters of the same type.
 For example, in the below mentioned program we have made two sum() functions to return sum of
two and three integers.
 Example:
void sum (int x, int y){
cout << x+y;
}
void sum(int x, int y, int z){
cout << x+y+z;
}
int main(){
sum (10,20); // sum() with 2 parameter will be called
sum(10,20,30); //sum() with 3 parameter will be called
}
5
Function overloading cont.
Different datatype of arguments
 In this type of overloading we define two or more functions with same name and same number of
parameters, but the type of parameter is different.
 For example in this program, we have two sum() function, first one gets two integer arguments and
second one gets two double arguments.
 Example:
void sum(int x,int y){
cout<< x+y;
}
void sum(double x,double y){
cout << x+y;
}
int main(){
sum (10,20);
sum(10.5,20.5);
}
6
Constructor Overloading
It’s same as function overloading that If any class have multiple constructors (same name as
class) but different parameters then this concept is known as Constructor Overloading.
***Note: Constructor has no type.
class Rectangle{ A
int main(){
public:
float area; //Calls Rectangle()
Rectangle(){ // Constructor with no parameters Rectangle object01;
area = 0;
} //Calls Rectangle(int a, int b)
// Constructor with two parameters Rectangle object02(10, 20);
Rectangle(int a, int b){
area = a * b; object01.display();
} object02.display();
void display(){ }
cout<< area<< endl;
}
7 };
A
Copy Constructor
 Its similar to the passing object to function.
 The copy constructor is a constructor which creates an object by initializing it with an
object of the same class, which has been created previously. The copy constructor is used to

 Initialize one object from another of the same type.
 Copy an object to pass it as an argument to a function.
 Copy an object to return it from a function.
 Why needed: If a class has pointer variables and has some dynamic memory allocations,
then it is a must to have a copy constructor.
 A copy constructor has the following general function prototype:
ClassName (const ClassName &old_obj){
//Body of constructor
};

8
Copy Constructor Example
class Point{ int main() A
private: {
int x, y; Point p1(10, 15); //Calls normal constructor
public: Point p2(p1); //Calls Copy constructor
Point(int x1, int y1) { x = x1; y = y1; } //point p2 = p1; //Another way to call Copy constructor

// Calling & Accessing private variables


// Copy constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
Point(const Point &p1) {
cout << "\np2.x = " << p2.getX() << ", p2.y = " <<
x = p1.x+2; y = p1.y; p2.getY();
}
int getX(){ return x; } return 0;
int getY(){ return y; } }
}; A

9
Default Arguments
When we mention a default value for a parameter while declaring the function, it is said to be as default argument.
In this case, even if we make a call to the function without passing any value for that parameter, the function will take the
default value specified.
Example:

void sum(int x,int y=0){ //Default value for y is 0


cout << x+y<<“ ”;
}
int main(){
sum(10); • For the first function call, x=10 & y=0(Default)
sum(10,10); • For the second function call, x=10 & y=10
}
Output : 10 20

• By setting default argument, we are also overloading the function.


• Default arguments also allow you to use the same function in different situations just like function
overloading.
10
Default Arguments Cont.
Rules for using Default Arguments
 Only the last argument must be given default value.You cannot have a default argument
followed by non-default argument.
sum (int x,int y);
sum (int x,int y=0);
sum (int x=0,int y); // Incorrect
 If you default an argument, then you will have to default all the subsequent arguments after
that.
sum (int x,int y=0);
sum (int x,int y=0,int z); // Incorrect
sum (int x,int y=10,int z=10); // Correct
 You can give any value a default value to argument, compatible with its datatype.

11
Overloading Ambiguity
In Function Overloading, you know that compiler decides which function needs to be
invoked among the overloaded functions.
If the compiler can not choose a function among two or more overloaded functions,
the situation is Ambiguity in Function Overloading.

12
Overloading Ambiguity Cont.
The reason behind the ambiguity in the following code is that the floating variable 3.5 and 5.6 are actually treated
as double by the compiler.
As per C++ standard, floating points are treated as double. Since compiler could not find a function with double
argument and got confused if the value should be converted from double to int or float.

void test(float s) { Removing ambiguity: void test(float s) {


cout << "float "; Added suffix "f" to the cout << "float ";
} value to tell compiler, it's }
void test(int s) { a float value. void test(int s) {
cout << "int"; cout << "int";
} }
int main() { int main() {
test(3.5); //ambiguous calling test(3.5f); //ambiguous removed
return 0; return 0;
} }
13
Overloading Ambiguity Cont.
float f(float a);
int test(int a) { double f(double a);
return a; int main() {
} cout << f(10.1f) << " "; // unambiguous, calls f(double)
int test(int a, int b=10) { //cout << f(10); // Error! ambiguous
return a*b; return 0;
} }
int main() { float f(float a) {
cout << test(4, 5) << " "; // unambiguous return a;
//cout << test(6); // Error, ambiguous! }
return 0; double f(double a) {
} return a+10;
}
Ex 01: Overloading ambiguity in default parameter Ex 02 Overloading ambiguity between float & double
14

You might also like