---
title: "Cube of int, float, double by overloading"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:cube-overloaded-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/cube-overloaded-program
site: BCT Engineering
---

# Cube of int, float, double by overloading

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

- Asked in: 2072 Kartik, 3 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/cube-overloaded-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Write a program to calculate and display the cube of integer, float and double number using function overloading (passing single argument to function).

## Short answer

Overload cube() for int, float, and double, each returning x*x*x. Call with appropriate types; e.g., cube(3) → 27, cube(2.5f) → 15.625, cube(1.5) → 3.375. Use f suffix for float literal to call float version.

## Answer

Function overloading: three functions named `cube` with the same single parameter position but different types. The compiler selects the one matching the argument type.

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

int cube(int x)
{
    return x * x * x;
}
float cube(float x)
{
    return x * x * x;
}
double cube(double x)
{
    return x * x * x;
}

int main()
{
    int i = 3;
    float f = 2.5f;
    double d = 1.5;

    cout << "Cube of int    " << i << " = " << cube(i) << endl;
    cout << "Cube of float  " << f << " = " << cube(f) << endl;
    cout << "Cube of double " << d << " = " << cube(d) << endl;
    return 0;
}
```

#### Output

```text
Cube of int    3 = 27
Cube of float  2.5 = 15.625
Cube of double 1.5 = 3.375
```

Note: a literal such as `2.5` is a `double`, so use the `f` suffix (`2.5f`) or a float variable to call the float version.

## Check yourself

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

1. Overloaded cubes differ by?
   A. Name
   B. Return only
   C. Nothing
   D. Parameter type
   Answer: D. Type. (Key point: Overloading)

2. Why use 2.5f?
   A. 2.5 is double; f selects float version
   B. Faster
   C. Required syntax
   D. Makes int
   Answer: A. Literal double. (Key point: Literal)

3. cube(1.5) gives?
   A. 2.25
   B. 3.375
   C. 4.5
   D. 3
   Answer: B. 1.5^3. (Key point: Output)

## 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
- [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
- [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×
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Flowchart to reverse integer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aflowchart-reverse-number) — Computer Programming, chapter 2
- [Single, double, multiple timber roofs; king post truss](https://www.bctengineer.com/question.md?key=semester_6%2Fbuilding_technology%3Atimber-roofs-king-post) — Building Technology, chapter 4, asked 2×
- [Single and double handshaking](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Ahandshaking-single-double) — Microprocessor, chapter 4
- [Single vs double earth wire selection](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Asingle-double-earth-wire-criteria) — Transmission and Distribution System Design, chapter 3
