---
title: "Function overloading; new/delete average program"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:function-overloading-new-delete-average"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-overloading-new-delete-average
site: BCT Engineering
---

# Function overloading; new/delete average program

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

- Asked in: 2068 Chaitra, 2+5+3 marks
- Video: [Function Overloading | C++ Tutorial](https://www.youtube.com/watch?v=A9COxBajDng) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-overloading-new-delete-average
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

What is function overloading? Use new and delete operators to store n numbers dynamically and find their average using casting operator. What are the things we should remember while using default argument? Explain with an example.

## Short answer

Function overloading: same name, different parameters. For average: int *a = new int[n]; sum values, cast to float: avg = (float)sum/n; delete[] a. Default arguments: assign right to left, specify once, no skipping middle; e.g., void show(int a, int b=5, int c=10); show(1) uses b=5, c=10.

## Answer

#### Function overloading

**Function overloading** means using the same function name for several functions that differ in the number, type or order of their parameters. The compiler selects the correct one at compile time from the arguments of the call (compile-time polymorphism). The return type alone cannot distinguish overloads.

```cpp
int add(int a, int b);
float add(float a, float b);
int add(int a, int b, int c);
```

#### Program: n numbers with new and delete, average with cast

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

int main()
{
    int n;
    cout << "How many numbers? ";
    cin >> n;

    int *a = new int[n];           // allocate dynamically
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        cout << "Enter number " << i + 1 << ": ";
        cin >> a[i];
        sum += a[i];
    }

    // cast sum to float so the division is not integer division
    float avg = (float)sum / n;
    cout << "Average = " << avg << endl;

    delete[] a;                    // free memory
    return 0;
}
```

Without the cast, `sum / n` of two ints would drop the fractional part (e.g. 7/2 = 3). `(float)sum` makes the division floating point (3.5).

#### Things to remember with default arguments

1. Defaults are assigned from right to left; a parameter after a defaulted one must also have a default.
2. Specify the default only once (in the prototype, or the definition if no prototype).
3. Defaults can be constants or expressions but not local variables.
4. Avoid ambiguity with overloaded functions.
5. Arguments are matched left to right; we cannot skip a middle argument.

```cpp
void show(int a, int b = 5, int c = 10);
show(1);        // b = 5, c = 10
show(1, 2);     // c = 10
show(1, 2, 3);  // no defaults
```

## Check yourself

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

1. Which change alone does NOT allow two functions with the same name to be overloaded?
   A. Different number of parameters
   B. Different order of parameter types
   C. Different return type only
   D. Different parameter types
   Answer: C. Overloads are told apart by their parameter lists; the return type alone cannot distinguish them. (Key point: Overload distinction)

2. Why is sum cast to float in (float)sum / n?
   A. To make the division floating point so the fraction is kept
   B. To make n a float permanently
   C. To allocate memory for floats
   D. To avoid using delete
   Answer: A. Without the cast, int/int drops the fraction (7/2 = 3); casting gives 3.5. (Key point: Cast in average)

3. How is the array created by new int[n] released?
   A. delete a;
   B. free(n);
   C. delete[] a;
   D. a = NULL;
   Answer: C. Memory allocated with new[] is freed with delete[]. (Key point: Freeing array memory)

4. Which declaration follows the default-argument rule?
   A. void f(int a = 1, int b);
   B. void f(int a, int b = 5, int c = 10);
   C. void f(int a = 1, int b, int c = 3);
   D. void f(int a, int b = x) where x is a local variable
   Answer: B. Defaults are assigned right to left; a parameter after a defaulted one must also have a default, and defaults cannot be local variables. (Key point: Default argument rule)

## 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×
- [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
- [Rectangle to Polar via casting operator](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Arectangle-to-polar-cast) — Object Oriented Programming, chapter 5
- [Operator overloading: need and definition](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-why-define) — Object Oriented Programming, chapter 5, asked 3×
- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Operators; conditional operator](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoperator-conditional) — Computer Programming, chapter 3
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
