Computer Science, asked by gite, 1 year ago

write a function called power() that takes a double for m and int value for p and returns the result as double value .use of a default argument of 2 for p,so that if this argument is this omitted ,the number will be squared, write a main() function that gets value from the user to test this function.


write a function that performs same operation as that of above exercise but takes an int value for m.both the function should have the same name . write a main() that calls both the functions .use the concept of function overloding.

Answers

Answered by kvnmurty
1
The following is not exactly following the syntax rules of a language.  Please do follow when you write the program.  It is just a pseudo code.

#include <iostream>
#include <string>
#include <sstream>
              // we use string stream function to extract optional exponent values
#include <cmath>
using namespace std;

// function overloading
double power(double m, int p);
int power (int m, int p) ;
double power(double m);
int power (int m);

int  main (char * argv[], int argc)
{
      double   d_result, d_base, fraction ;
       int     i_result, i_base, exponent;
      string myExp;

               cout << "\nEnter the value of base: ";   cin >> d_base;
               i_base = (int) d_base ;   // integer part of base
               fraction = d_base - i_base;      // fraction part. check for 0 for an integer.
              // another way of checking is reading base as a string and checking for a "." in it.

               cout << "\n Enter the value of exponent: " ;  cin >> myExp;
               if (myExp.empty())     // default exponent   {
                        if (fraction == 0)    power (i_base);
                        else   power (d_base);
              }
               else     {  /// non default integer exponent
                         stringstream(myLine) >> exponent;
                         if (fraction == 0)    i_result =  power (i_base, exponent);
                        else        d_result = power(d_base, exponent);
            }
            cout <<  "\n Answer:  "  <<   (fraction) ?  d_result : i_result  ;
}

double power(double m, int p)
{
       return  pow(m, p);           // math
}

int power (int m, int p)
{
      return pow(m, p);       // math
}
double power(double m)
{
        return pow(m, 2);
}

int power (int m)
{
         return pow(m, 2);
}



kvnmurty: click on thanks button above please
Similar questions