CPP_Segment_03
CPP_Segment_03
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
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:
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.