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.
#include<iostream>
using namespace std;
int sqr(int value);
float sqr(float value);
//The start point of the program
int main (){
cout<< "sqr(15) = "<<sqr(15);
cout<< "\nsqr(2.5) = "<<sqr(2.5f)<<"\n\n";
system("pause");
return 0;
}
// overload the function sqr()
int sqr(int value){
return value*value;
}
// overload the function sqr()
float sqr(float value){
return value*value;
}
Comments
Leave a comment