Skip to main content

Posts

Showing posts with the label Write a program to compute the area of a triangle and a circle by overloading the area() function

Write a program to compute the area of a triangle and a circle by overloading the area() function.

 Here's an example program that overloads the area() function to calculate the area of a triangle and a circle: #include <iostream> using namespace std; // Function to calculate area of triangle float area(float base, float height) {     return 0.5 * base * height; } // Function to calculate area of circle float area(float radius) {     return 3.14 * radius * radius; } int main() {     float base, height, radius;     // Read input for triangle     cout << "Enter base and height of the triangle: ";     cin >> base >> height;     // Calculate and display area of triangle     float tri_area = area(base, height);     cout << "Area of triangle: " << tri_area << endl;     // Read input for circle     cout << "Enter radius of the circle: ";     cin >> radius;     // Calculate and display area of circle ...