Write a C++ program to illustrate function overloading. Write 2 overloading functions for power.
AIM:
Write a C++ program to illustrate function overloading. Write 2 overloading
functions for power.
THEORY:
C++ allows specification of more than
one function of the same name in the same scope. These are called overloaded
functions and are described in detail in Overloading. Overloaded functions
enable programmers to supply different semantics for a function, depending on
the types and number of arguments.
For example, a print function
that takes a string (or char *) argument performs very different
tasks than one that takes an argument of type double. Overloading
permits uniform naming and prevents programmers from having to invent names
such as print_sz or print_d. The following table shows what
parts of a function declaration C++ uses to differentiate between groups of
functions with the same name in the same scope.
Although functions can be
distinguished on the basis of return type, they cannot be overloaded on this
basis. Const or volatile are only used as a basis for
overloading if they are used in a class to apply to the this pointer
for the class, not the function's return type. In other words, overloading
applies only if the const or volatile keyword
follows the function's argument list in the declaration.
SOURCE
CODE:
#include<iostream>
#include<math.h>
using namespace std;
inline float power(float x,float y)
{
return
power(x,y);
}
inline int power(int x,int y)
{
return
power(x,y);
}
int main()
{
int
a=2,b=3,x;
float
c=2.1,d=3,y;
x=power(a,b);
y=power(c,d);
cout<<"\n
x= "<<x<<endl<<"\n y= "<<y;
}
Labels: oop through c++ lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home