operator overloading-- c support; c++ also support
function overloading-- c doesn't support; c++ support
c support operator_overloading but not Function_overloading
c++ support operator_overloading as well as Function_overloading
What is operator overloading:
operator overloading is a specific case of polymorphism, where operators have different implementations depending on their arguments.
(The whole idea in operator overloading is 'same operator' and 'different type argument')
(The whole idea in operator overloading is 'same operator' and 'different type argument')
what is Function overloading:
Function overloading or method overloading is a feature in programming language that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function.
(The whole idea in function overloading is 'same function name' and 'different type argument')
(The whole idea in function overloading is 'same function name' and 'different type argument')
As we known 'int' and 'float' are different datatype, instead of using different operator to add 'int' and 'float'; we use a common operator (+), this is known as operator overloading.
Program to shows, C support operator overloading
program to shows, cpp support operator overloading
____________________________________________________________________________
Program to show, c doesn't support Function overloading
#include <stdio.h>
#include <conio.h>
// volume of a cube
int volume(int s) //Name of this function is 'volume'
{
return(s*s*s);
}
// volume of a cylinder
double volume(double r, int h) //Name of this function is also 'volume'
{
return(3.14*r*r*h);
}
// volume of a cuboid
long volume(long l, int b, int h) //Name of this function is also 'volume'
{
return(l*b*h);
}
int main()
{
cout << volume(10) << endl;
cout << volume(2.5,8) << endl;
cout << volume(10,5,15) << endl;
}
- when we compile this program it give error, because c doesn't support 'function overloading.'
- जब c compiler 3 same name (volume) के फंक्शन देखता है तो कंफ्यूज हो जाता है। क्योकि c का compiler Function overloading (polymorphism) नहीं जनता, इसलिए तो कहते है, c Function overloading support नहीं करती।
_________________________________________________________________________________
But c++ Function overloading support करती है
Program to show, cpp support Function overloading
#include <iostream>
using namespace std;
// volume of a cube
int volume(int s) //Name of this function is 'volume'
{
return(s*s*s);
}
// volume of a cylinder
double volume(double r, int h) //Name of this function is also 'volume'
{
return(3.14*r*r*h);
}
// volume of a cuboid
long volume(long l, int b, int h) //Name of this function is also 'volume'
{
return(l*b*h);
}
int main()
{
cout << volume(10) << endl;
cout << volume(2.5,8) << endl;
cout << volume(10,5,15) << endl;
}
Program to show, cpp support Function overloading
#include <iostream>
using namespace std;
// volume of a cube
int volume(int s) //Name of this function is 'volume'
{
return(s*s*s);
}
// volume of a cylinder
double volume(double r, int h) //Name of this function is also 'volume'
{
return(3.14*r*r*h);
}
// volume of a cuboid
long volume(long l, int b, int h) //Name of this function is also 'volume'
{
return(l*b*h);
}
int main()
{
cout << volume(10) << endl;
cout << volume(2.5,8) << endl;
cout << volume(10,5,15) << endl;
}
This program run and
OUTPUT IS:
1000
157
750
_________________________________________________________________
No comments:
Post a Comment