In: Computer Science
C++
Give an example of overloading by implementing a function
square(x), which calculates the square of x, (also known as x*x or
x 2 ) where the input can be int or double.
#include<iostream>
using namespace std;
//function which will calculate and print the area of square
with side of type int
void square(int x){
int result=x*x;
cout<<"Area of square with side "<<x<<" is
"<<result<<endl;
return;
}
//function which will calculate and print the area of square with
side of type double
void square(double x){
double result=x*x;
cout<<"Area of square with side "<<x<<" is
"<<result<<endl;
return;
}
//driver function
int main(){
//calling square with data type as int
square(2);
//calling square with data type as double
square(2.3);
return 0;
}
Area of square with side 2 is 4
Area of square with side 2.3 is 5.29
CODE
INPUT/OUTPUT
So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.