Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Area of square with side 5 = 25
Area of circle with radius 5 = 78.5
Area of rectangle with sides 5,6 = 30
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.