Answer to Question #212704 in C++ for moiz

Question #212704

Write two generic functions (overloaded) named area(). Create function template for the first area() function that it is able to receive the radius of a circle as its parameter and returns half of the calculated area of circle as of same data type. Similarly, create function template for the second area() function that it is able to receive two parameters as length and width of a rectangle and returns the triple of the calculated area of rectangle as of same data type. 


1
Expert's answer
2021-07-02T04:11:21-0400
/******************************************************************************


Write two generic functions (overloaded) named area().
Create function template for the first area() function that it is able to 
receive the radius of a circle as its parameter and returns half of the calculated 
area of circle as of same data type. Similarly, create function template for the second 
area() function that it is able to receive two parameters as length and width of a rectangle 
and returns the triple of the calculated area of rectangle as of same data type. 


*******************************************************************************/


#include <iostream>


using namespace std;
template <class T>
T area (T a, T b)
{
  return (3*(a*b));
}
double area (double r)
{
  return (0.5*(3.142*r*r));
}
int main()
{
    cout<<area<int>(15,7)<<endl;
    cout<<area(14);
    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