Write a C++ program to illustrate a function overloading. W rite two Overloading Functions for adding two numbers.
AIM:
Write a C++ program to illustrate a function overloading. W rite two
Overloading Functions for adding two
numbers.
THEORY:
Since functions' names are in this case the
same, we must preserve uniqueness of signatures, by changing something from
the parameter list (last three alienees).
If the functions' signatures are sufficiently different, the compiler
can distinguish which function was intended to be used at each occurrence. This
process of searching for the appropriate function is called function
resolution and can be quite an intensive one, especially if there
are a lot of equally named functions.
Programming languages supporting implicit type conventions usually use
promotion of arguments (i.e. type casting of integer to floating-point) when
there is no exact function match. The demotion of arguments is rarely used.
When two or more functions match the criteria in function resolution
process, an ambiguity error is reported by compiler.
Adding more information for the compiler by editing the source code (using for
example type casting), can address such doubts.
The example code shows how function overloading can be used. As functions
do practically the same thing, it makes sense to use function overloading.
function int generateNumber(int
MaxValue) {
return rand * MaxValue
}
function int generateNumber(int
MinValue, int MaxValue) {
return MinValue + rand * (MaxValue -
MinValue)
}
SOURCE
CODE:
#include<iostream>
using namespace std;
class MFO{
public:
int
sum(int,int);
float
sum(float a ,float b){
return
a+b;
}
};
int MFO::sum(int a ,int b){
return
a+b;
}
int main(){
MFO
ob;
cout<<"\n
Sum of two integer numbers:"<<ob.sum(10,20);
cout<<"\n
Sum of two float numbers:"<<ob.sum(2.4f,7.1f);
}
Labels: oop through c++ lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home