Answer to Question #173446 in C++ for praveenkumar.b

Question #173446

Write a C++ code to overload the function sqr() . In the first call of the function sqr(), an integer 15 is passed. The compiler executes the integer version of the function and returns result 225. In the second call a float value 2.5 is passed to function sqr(). The compiler executes the float value of the function and returns the result 6.25. The selection of which function to execute is decided at the run time by the compiler according to the datatype of variable passed.


1
Expert's answer
2021-03-23T13:50:46-0400

// Program to compute sqr value

// Works for both int and double

 

#include <iostream>

using namespace std;

 

// function with double type parameter

double sqr(double n){

    return n*n;

}

 

// function with int type parameter

int sqr(int n) {

   return n*n;

}

 

int main() {

   

   // call function with int type parameter

   cout << "sqr value of 15 = " << sqr(15) << endl;

 

   // call function with double type parameter

   cout << "sqr value of 2.5 = " << sqr(2.5) << endl;

   return 0;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS