Function overloading for area functions – CSKC
Sign In

Search

Solved Problem #CPP#2990    siteicon   siteicon  

Problem Statement - Function overloading for area functions

Write a c++ program using function overloading using the following prototype –
int area(int); //For area of square
float area(float); //For area of circle
int area(int,int); //For area of rectangle
show working of this program by calling this function into the main routine using some assumed valued.

Solution

TC++ #2990

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

int area(int x) {return x*x;}
This function returns area of square.

float area(float x) {return 3.14*x*x;}
This function returns area of a circle

float area(int x,int y) {return x*y;}
This function returns area of a rectangle

int x=5,y=6; 
These values have been assumed.

cout<<“Area of square with side ” <<x<<” = “<<area(x)<<endl;
Area of a square is returned when int is passed as parameter x.

cout<<“Area of circle with radius “<<x<<” = “<<area((float) x)<<endl;
Area of circle is returned when float is passed as parameter x

cout<<“Area of rectangle with sides “<<x<<“,”<<y<<” = “<<area(x,y)<<endl;
Area of rectangle is passed when two integer parameters are passed as x and y

Notes

  • Function overloading or function polymorphism depends on type of parameter and number of parameters. It does not differentiate on the basis of returned data type of a function.

Common Errors

  • If in this program while calculating the area of circle if the value passed inside is not in float form the relevant function will not be called so it is important to typecast as float or initialise a separate variable of type float. So students will have to be careful while assuming the values.


Share

Created: 24-Jan-2018 22:05 | Updated: 29-Nov-2018 17:26|