Search This Blog

Saturday 23 January 2016

Function Overloading In C++

#include<iostream>

using namespace std;

void power(double m,int n=2);                //default value of n is set to 2
void power(int m,int n=2);                      
//default value of n is set to 2
 
int main()
{
    int m,n;
    double dm;
    cout << "Enter the integer value of m"<<endl;
    cin >> m;
    cout << "Enter the double value of m"<<endl;
    cin >> dm;
    cout << "Enter the integer value of n"<<endl;
    cin >> n;
    power(dm,n);
    power(m,n);
    power(dm);                                            //default value from prototype is used
    power(m);   
                                           //default value from prototype is used
 }

void power(double m,int n)
{
    double r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

void power(int m,int n)
{
    int r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

No comments:

Post a Comment