Here's a program with an overloaded function calc_area() that calculates and returns the area of a circle and rectangle. The program assumes a radius and length/width are given as arguments for the circle and rectangle, respectively. The return type for both function is a "double".
#include <iostream>
using namespace std;
const double PI= 3.14;
double calc_area(double radius) {
return PI*radius*radius;
}
double calc_area(double length, double width) {
return length * width;
}
int main() {
double radius, length, width;
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
double circle_area = calc_area(radius);
double rectangle_area = calc_area(length, width);
cout << "Area of the circle is: " << circle_area << endl;
cout << "Area of the rectangle is: " << rectangle_area << endl;
return 0;
}
The program uses the constant PI = 3.14 as the value of Pi, and it asks the user to input the radius, length and width of the shapes. The program then calculates the area of the circle and rectangle using the overloaded "calc_area()" function, and displays the result to the user.
No comments:
Post a Comment
If you have any doubts, please let me know