Question

In: Computer Science

C++ Give an example of overloading by implementing a function square(x), which calculates the square of...

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.

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in C++ with code and output shown.
  • For better understading please read the comments mentioned in the code.
  • In the code shown below function overloading is shown by the use of square() function where we use two functions of same name and same return type i.e, void square() but we pass different type of parameter to them i.e, int and double. So this is a kind of polymorphism which is using of functions(in this case) of same name as different kinds.
  • So function is overloaded here, if you call square(x) where x is integer then the first function will be called but if x is of double type second function will be called.
  • CODE:

#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;
}

  • OUTPUT:

Area of square with side 2 is 4
Area of square with side 2.3 is 5.29

  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

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.


Related Solutions

c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
What does it mean when a method is overloaded? Give an example of overloading the method...
What does it mean when a method is overloaded? Give an example of overloading the method isNotInRange so that can be called like isNotInRange(100, 97, 122);
Give an example of a function F which is the joint probability distribution (not density) function...
Give an example of a function F which is the joint probability distribution (not density) function of a pair of random variables X and Y such that (a) X and Y are independent and discrete (b) X and Y are dependent and discrete (c) X and Y are independent and continuous (d) X and Y are dependent and continuous
Write a function double mysqrt( double x ) which computes the value of the square root...
Write a function double mysqrt( double x ) which computes the value of the square root of x using the bisection method. First you need to set the left and right bounds for x. If 0<x<1 then lt = x and rt = 1 and the sqrt(x) is somewhere in between. If x > 1 then lt = 1 and rt = x and sqrt(x) is somewhere in between. In a loop you need to compute the mid value between...
Give an example of a function whose Taylor polynomial of degree 1 about x = 0...
Give an example of a function whose Taylor polynomial of degree 1 about x = 0 is closer to the values of the function for some values of x than its Taylor polynomial of degree 2 about that point.
Give an example to discuss the X-linked human phenotypes. Please discuss the structure and function of...
Give an example to discuss the X-linked human phenotypes. Please discuss the structure and function of telomeres and their involvement in cellular aging. Explain how PCR amplifies a particular sequence of DNA.
Give an example of a square matrix A such that all the diagonal entries are positive...
Give an example of a square matrix A such that all the diagonal entries are positive but A is not positive definite
Explain the Chi – Square test and when it is appropriate and give an example.
Explain the Chi – Square test and when it is appropriate and give an example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT