answersLogoWhite

0


Best Answer

#include

// overloads:

char square(char& c){return(c*c);}

double square(double& d){return(d*d);}

int square(int& i){return(i*i);}

int main()

{

double a=3;

double b=square(a); // b=9

int c=5;

int d=square(c); // d=25

char e=12;

char f=square(f); // f=144

return(0);

}

Note that since all the overloads will have the exact same implementation and will only differ by type, we can take advantage of C++ templates to generate the overloads for us, automatically, as and when they are required. Thus the following code is functionally equivalent to the previous code:

#include

template

T square(T& t){return(t*t);}

int main()

{

// force compiler to generate double square(double&) overload

double a=3;

double b=square(a); // b=9

// force compiler to generate int square(int&) overload

int c=5;

int d=square(c); // d=25

// force compiler to generate char square(char&) overload

char e=12;

char f=square(f); // f=144

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Program to find the square of a given number using Function Overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is function overloading?

FUNCTION OVERLOADING:- when we define two functions with same name,in same class(may be) distinguished by their signatures- resolved at compile time- same method bt different parameters in each of themFUNCTION OVERRIDING:- when we redifine a function which is already defined in parent class- resolved at run time- changing the existing method


What is an example program in c plus plus to square a number using the concept of an inline function?

... double squareOf_Number(double Number){return (Number*Number);}...int main(){...double Number = 0;...printf("Enter a number: ");cin >> Number;...printf("Square of %f is %f\n", Number, squareOf_Number(Number));...}Or you can include #include and use the function pow(double a, double b) which returns a^b.


How do you write a vbscript for converting alphanumeric to numeric?

num = InputBox("Enter a number: ","PROGRAM: Square") sumSquare = CInt(num) * CInt(num) MsgBox("The square of " & num & " = " & sumSquare) ===== *NOTE*: The function CInt() is what we use to convert a text string to become a numeric integer value.


How do you write Square program using vb?

write a vb program to find the magic square


What is implied by the argument of the function?

A function (also known as procedure, subroutine, and - in object-oriented languages - as a method) lets you do repetitive calculations in a single place, without having to repeat lots of commands over and over. For example, you might have a function that calculates the square root of a number. An argument (also known as a parameter) is any variable information you pass to your function. For example, in the case of calculating a square root, the argument tells the function what number you want to calculate the square root of. For calculating powers, you might have two arguments: the base, and the exponent. In general, a function can have zero or more arguments - it really depends what it is used for.

Related questions

How does inverse function work?

Let's illustrate with an example. The square function takes a number as its input, and returns the square of a number. The opposite (inverse) function is the square root (input: any non-negative number; output: the square root). For example, the square of 3 is 9; the square root of 9 is 3. The idea, then, is that if you apply first a function, then its inverse, you get the original number back.


Which math function is used to find the square root of a number using java?

Math.sqrt(number) function is used to find the square root of a number.. try it


Program to find square and cube of a number?

square and cube caculator


How does functions work?

Let's illustrate with an example. The square function takes a number as its input, and returns the square of a number. The opposite (inverse) function is the square root (input: any non-negative number; output: the square root). For example, the square of 3 is 9; the square root of 9 is 3. The idea, then, is that if you apply first a function, then its inverse, you get the original number back.


What is function overloading?

FUNCTION OVERLOADING:- when we define two functions with same name,in same class(may be) distinguished by their signatures- resolved at compile time- same method bt different parameters in each of themFUNCTION OVERRIDING:- when we redifine a function which is already defined in parent class- resolved at run time- changing the existing method


What is an inverse of a function?

The opposite of another function - if you apply a function and then its inverse, you should get the original number back. For example, the inverse of squaring a positive number is taking the square root.


How do I explain square root?

The square root of a number is that number which, when squared, gives you the given number. For example, the square root of 25 is 5, since if you square 5, you get 25. It is the "inverse function" (that is, in a way it's the opposite) of squaring.


What is an example program in c plus plus to square a number using the concept of an inline function?

... double squareOf_Number(double Number){return (Number*Number);}...int main(){...double Number = 0;...printf("Enter a number: ");cin >> Number;...printf("Square of %f is %f\n", Number, squareOf_Number(Number));...}Or you can include #include and use the function pow(double a, double b) which returns a^b.


How do you write a vbscript for converting alphanumeric to numeric?

num = InputBox("Enter a number: ","PROGRAM: Square") sumSquare = CInt(num) * CInt(num) MsgBox("The square of " & num & " = " & sumSquare) ===== *NOTE*: The function CInt() is what we use to convert a text string to become a numeric integer value.


How do you write a java program to find the square root of a number?

You can use the Math.sqrt() method.


What is the key word for square root in abap?

sqrt is inbuilt function available in ABAP to calculate square root of any number.


Write a c program to accept a numbers and generate square root cube and exponential values?

write a c program to accept a number and generate a square root cube and exponential values