---
title: "Default argument and overloading program"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:default-arg-overloading-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/default-arg-overloading-program
site: BCT Engineering
---

# Default argument and overloading program

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

- Asked in: 2072 Chaitra, 4 marks
- Video: [Function overloading and default arguments in C++ (Episode 2)](https://www.youtube.com/watch?v=ElKSqvP43ts) — BBarters
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/default-arg-overloading-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Write a program to show the relation between default argument and function overloading.

## Short answer

The program shows default arguments, as in float volume(float l, float b=2, float h=3), replace multiple overloaded functions by accepting 1–3 arguments. Overloaded area functions handle circle and rectangle separately. Mixing defaults with overloading can create ambiguous calls.

## Answer

Both default arguments and function overloading let one function name be called with different numbers of arguments. They can conflict: if an overloaded function can be matched with or without the defaulted argument, the call is **ambiguous** and the compiler reports an error. So overloads and defaults must be designed so that each call has exactly one best match.

#### Program

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

// overloaded versions of area
float area(float r)                  // circle
{
    return 3.14159 * r * r;
}
float area(float l, float b)         // rectangle
{
    return l * b;
}
// function with default arguments (box volume)
float volume(float l, float b = 2, float h = 3)
{
    return l * b * h;
}
// ambiguous if declared along with volume(float):
// float volume(float l);

int main()
{
    cout << "Circle area = " << area(2.0f) << endl;
    cout << "Rectangle area = " << area(4.0f, 5.0f) << endl;
    cout << "Volume(1 arg) = " << volume(1) << endl;
    cout << "Volume(2 args) = " << volume(1, 4) << endl;
    cout << "Volume(3 args) = " << volume(1, 4, 5) << endl;
    return 0;
}
```

#### Output

```text
Circle area = 12.5664
Rectangle area = 20
Volume(1 arg) = 6
Volume(2 args) = 12
Volume(3 args) = 20
```

#### Relation

- Overloading: different functions, same name, different parameter lists.
- Default argument: one function, usable with fewer arguments.
- If we also wrote `float volume(float l);`, the call `volume(1)` would match both versions, giving an ambiguity error.

## Check yourself

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

1. Adding float volume(float l) beside volume(l, b=2, h=3)?
   A. volume(1) becomes ambiguous
   B. Nothing
   C. Faster
   D. Compiles ok
   Answer: A. Matches both. (Key point: Ambiguity)

2. Overloading means?
   A. Same params different names
   B. Same name, different parameter lists
   C. Default values
   D. Inline
   Answer: B. Definition. (Key point: Overloading def)

3. volume(1,4) prints?
   A. 6
   B. 20
   C. 12
   D. 4
   Answer: C. 1*4*3. (Key point: Output)

4. area(2.0f) prints?
   A. 6.28
   B. 4
   C. 25
   D. 12.5664
   Answer: D. pi r^2. (Key point: Circle)

## Related in Object Oriented Programming

- [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
- [Area of triangle and rectangle program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aarea-triangle-rectangle-overload) — Object Oriented Programming, chapter 3
- [Function overriding versus overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overriding-vs-overloading) — Object Oriented Programming, chapter 6, asked 2×

## Related in other subjects

- [White noise: relation between PSDF and AC function](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Awhite-noise-psdf-ac-relation) — Communication Systems, chapter 9
