📌Highlights: Hello Friends! In this post we discuss about Overloading concept in C++. We cover following topics such as Function Overloading, Operator Overloading, concept of polymorphism, Examples.
OVERLOADING: When we use same thing for different purpose than it is known as overloading.
In 'C" only predefined overloading is takes place such as:
'&' this symbol is used in two ways:
1. For as a address
2. For as a logical AND.
But in ' C ' there is not user defined overloading is possible.
But C++ allow user define overloading for function and operator.
FUNCTION OVERLOADING:
In ' C ' identifier of block are unique so we can not make any function twice.
But in C++ we can do. C++ permit overloading of function. This means that we can use same function name to create function that perform variety of different task. This is known as function polymorphism in OOPs.
By using the concept of function overloading we can design a family of function with one function name but with different argument list. The function would perform different operation depending upon argument list in the function call.
EXAMPLE:
FOR DECLARATION
int add(int a,int b);
int add(int a, int b,int c)
double add(double x,double q);
double add(int p,double q);
double add (double p, int q);
FOR FUNCTION CALL
count << add( 5,10 );
count << add( 5,10,0 );
count << add ( 12.5,7.5 );
count << add ( 5,10,15 );
count << add ( 0,75,5 );
In above a function call first match the prototype having the same number of argument and type of argument and call the appropriate function for the execution.
Now we will discuss about Selection.
1. The compiler first tries to find an same match in which the type of actual argument are the same.
2. If an same matching is not found the compiler uses the compatibility method by which lower is caste into upper in the sequence of waterfall model. such as.
char to int
float to double.
Compatibility means if char is casting into int than we can say that char is compatible to int.
But not convert int into char or double into float.
By waterfall model sequence of casting is:
char → int → float → double
3. When above two conditions are fail and if a function call match with two function definition then this situation is known as ambiguous function cal and give an error.
EXAMPLE:
long square ( long n )
double square ( double x )
function call such as
square ( 10 );
Here in this situation int is converted into both long as well as double it a situation of ambiguous function call and give error.
COMPLETE PROGRAM OF AMBIGUITY FUNCTION:
# include < conio.h >
# include < iostream.h >
long square (long);
double square (double);
void main ( )
{
clrscr ( );
count << square ( 10 );
getch ( );
}
logn square ( long a )
{
cout << " This is long ";
return ( a * a );
}
double square ( double b )
{
count << " This is double:" ;
return ( b * b) ;
}
This program give ambiguity error.
EXAMPLE OF FUNCTION OVERLOADING
# include < iostream.h >
overload volume;
int volume ( int );
double volume ( double, int );
long volume ( long, int, int );
main ( )
{
count << volume ( 10 ) << " \n";
count << volume (2.5, 8 ) << " \n";
count << volume (1001, 75, 15 ) ;
}
int volume ( int s)
{
return ( s * s *s ) ;
}
double volume ( double r, int h )
{
return ( 3.14 * r * r * h );
}
long volume ( long l, int b, int h )
{
return ( l * b * h );
}
NOTE:
→ We should not overload unrelated function.
→ Some time default argument is used instead of overloading. It reduce the number of function to be define.
→ Overloading function are exactly extensively used for handling class objects.
OPERATOR OVERLOADING
Operator overloading is one of the many feature of C++ language. It is an important technique that has enhanced the power of C++. C++ permit as to add two variables of user define types with the same syntax that is applied to the basic type or predefined data type.
This means that C++ has the ability to provide the operator with a special meaning of data type. The mechanism of giving such special meanings to an operator is known as operator overloading.
Operator overloading provide a flexible option for the creation of new definition for most of the C++ operators.
We may almost create new language by own by this overloading technique.
NOTE:
We can overload all the C++ operator except the following.
→ class member acess operator ( .... *) [redirection operator]
→ scope resolution operator ( : : )
REMEMBER:
When operator is overloaded its original meaning is not lost for instance the operator + , which has been overloaded to add two vectors, can still be used to add two integers.
The general form of operator function is
→ returntype classname : : operator op (argument list )
{
function body;
}
EXAMPLE OF OVERLOADING OPERATOR FOR COMPARING TWO STRING.
int operator > (char s1[30], char s2[30] )
{
int i = 0;
while ( s1[ i ] ! ='\0' && s2[ i ] ! ='\0' && s1[ i ] = = s2 [ i ] )
{
i ++ ;
}
if ( s1[ i ] - s2[ i ] > 0 )
{
return ( 1 );
}
else
{
return ( 0 );
}
void main ( )
{
char s1[ 20 ];
char s2[ 20 ];
cin >> s1 >> s2 ;
if ( s1 > s2 )
{
count << s1 ;
}
else
{
cout << s2 ;
}
}
Friends I hope you understand the concept of FUNCTION OVERLOADING AND OPERATOR OVERLOADING IN C++.By the help of examples it is clear how we implement Overloading in C++.
No comments:
Post a Comment