---
title: "Area of triangle and rectangle program"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:area-triangle-rectangle-overload"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/area-triangle-rectangle-overload
site: BCT Engineering
---

# Area of triangle and rectangle program

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2068 Baisakh, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/area-triangle-rectangle-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Write a program to find the area of triangle (when three sides are given) and area of rectangle using function overloading and default argument.

## Short answer

Overload a function area() for triangle using Heron’s formula s=(a+b+c)/2 and area= sqrt(s(s-a)(s-b)(s-c)). For rectangle, define area(length, breadth=0) that returns length*breadth; if breadth is 0, treat as square. Three arguments call triangle version, one or two call rectangle version.

## Answer

Plan: overload `area()`. The triangle version takes three sides and uses Heron's formula, s = (a+b+c)/2 and area = √(s(s-a)(s-b)(s-c)). The rectangle version takes length and breadth, where breadth has a default value (making a square when only one value is given).

```cpp
#include <iostream>
#include <cmath>
using namespace std;

// Area of triangle: three sides (Heron's formula)
float area(float a, float b, float c)
{
    float s = (a + b + c) / 2;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

// Area of rectangle: breadth has a default (= length -> square)
float area(float l, float b = 0)
{
    if (b == 0)
        b = l;
    return l * b;
}

int main()
{
    float a, b, c, l, w;
    cout << "Enter three sides of triangle: ";
    cin >> a >> b >> c;
    if (a + b > c && b + c > a && a + c > b)
        cout << "Area of triangle = " << area(a, b, c) << endl;
    else
        cout << "Not a valid triangle" << endl;

    cout << "Enter length and breadth of rectangle: ";
    cin >> l >> w;
    cout << "Area of rectangle = " << area(l, w) << endl;
    cout << "Area when only length given = " << area(l) << endl;
    return 0;
}
```

#### Sample output

```text
Enter three sides of triangle: 3 4 5
Area of triangle = 6
Enter length and breadth of rectangle: 5 2
Area of rectangle = 10
Area when only length given = 25
```

The compiler chooses the version by the number of arguments (3 arguments gives triangle; 1 or 2 gives rectangle), so there is no ambiguity.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Triangle area formula?
   A. sqrt(s(s-a)(s-b)(s-c)), s=(a+b+c)/2
   B. 0.5*a*b
   C. a*b*c
   D. s*(a+b+c)
   Answer: A. Heron. (Key point: Heron)

2. How is version chosen?
   A. By names
   B. By number of arguments
   C. By return
   D. User
   Answer: B. 3 vs 1/2. (Key point: Selection)

3. Condition for a valid triangle?
   A. a+b+c>0
   B. a=b=c
   C. Sum of any two sides greater than third
   D. a>b>c
   Answer: C. Triangle inequality. (Key point: Validity)

4. area(l) with b=0 default?
   A. 0
   B. l
   C. error
   D. Square l*l
   Answer: D. b=l. (Key point: Default breadth)

## Related in Object Oriented Programming

- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
