Skip to main content

Lesson 7 of 8

Mini Project Part 2 - Scientific Calculator with Overloading

Extend the Module 1 calculator with: function overloading for integer vs floating-point operations, default arguments for unit selection (degrees/radians), recursive power implementation, memoized fibonacci, and interactive I/O with input validation. Everything from this module in one project.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Scientific Calculator - Overloading, Defaults & Recursion

Scientific Calculator - Overloading, Defaults & Recursion
#include <iostream>
#include <iomanip>
#include <cmath>
#include <unordered_map>
#include <string>
#include <limits>

// ── Overloaded arithmetic ─────────────────────────────────────
int    add(int a, int b)       { return a + b; }
double add(double a, double b) { return a + b; }
int    add(int a, int b, int c){ return a + b + c; }

// ── Default unit: degrees ─────────────────────────────────────
double sine(double x, bool degrees = true) {
    constexpr double PI = 3.14159265358979;
    return std::sin(degrees ? x * PI / 180.0 : x);
}
double cosine(double x, bool degrees = true) {
    constexpr double PI = 3.14159265358979;
    return std::cos(degrees ? x * PI / 180.0 : x);
}

// ── Recursive power with integer exponent ─────────────────────
double power(double base, int exp) {
    if (exp == 0) return 1.0;
    if (exp < 0)  return 1.0 / power(base, -exp);
    if (exp % 2 == 0) {
        double half = power(base, exp / 2);   // O(log n) recursion
        return half * half;
    }
    return base * power(base, exp - 1);
}

// ── Memoized fibonacci ────────────────────────────────────────
std::unordered_map<int, long long> fib_cache;
long long fib(int n) {
    if (n <= 1) return n;
    auto it = fib_cache.find(n);
    if (it != fib_cache.end()) return it->second;
    return fib_cache[n] = fib(n-1) + fib(n-2);
}

// ── Safe integer input ────────────────────────────────────────
int read_int(const std::string& prompt) {
    int val;
    while (true) {
        std::cout << prompt;
        if (std::cin >> val) return val;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
        std::cout << "Invalid integer, try again.
";
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(6);

    // Overloading demo
    std::cout << "add(3,4)       = " << add(3, 4)          << "
";
    std::cout << "add(3.1,4.5)   = " << add(3.1, 4.5)      << "
";
    std::cout << "add(1,2,3)     = " << add(1, 2, 3)        << "
";

    // Default args
    std::cout << "sin(90°)       = " << sine(90.0)          << "
";  // 1.0
    std::cout << "cos(0°)        = " << cosine(0.0)         << "
";  // 1.0
    std::cout << "sin(pi rad)    = " << sine(3.14159, false) << "
"; // ~0

    // Recursive power
    std::cout << "2^10           = " << power(2.0, 10)      << "
";  // 1024
    std::cout << "2^-3           = " << power(2.0, -3)      << "
";  // 0.125

    // Memoized fibonacci
    for (int i = 1; i <= 10; ++i) {
        std::cout << "fib(" << i << ") = " << fib(i) << "
";
    }

    // Interactive
    std::cout << "
--- Interactive fibonacci ---
";
    int n = read_int("Enter n for fibonacci (0-50): ");
    if (n >= 0 && n <= 50) {
        std::cout << "fib(" << n << ") = " << fib(n) << "
";
    } else {
        std::cout << "Out of range
";
    }

    return 0;
}

Common Mistakes

  • Overloading `add(int,int)` and `add(double,double)` but calling `add(3, 4.0)` - this is ambiguous (int→double, double→int both have same cost). Add a third overload `add(int, double)` or cast explicitly.
  • Memoization cache as a global - works for single tests but the cache persists across calls if the program calls `fib` multiple times from different test runs. In tests, clear the cache between runs.
  • Recursive power not handling `exp == 0` as base case - `power(0,0)` is mathematically undefined but the recursive split approach hits base case correctly. Always test edge cases: `power(x,0)`, `power(0,n)`, `power(x,-n)`.

Tip

Tip

Practice Mini Project Part 2 Scientific Calculator with Overloading in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Overloaded Operations, Mathematical Domain Validation & Error State Propagation

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Part 2 Scientific Calculator with Overloading from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Part 2 Scientific Calculator with Overloading is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Key Takeaways

  • Extend the Module 1 calculator with: function overloading for integer vs floating-point operations, default arguments for unit selection (degrees/radians), recursive power implementation, memoized fibonacci, and interactive I/O with input validation.
  • Overloading `add(int,int)` and `add(double,double)` but calling `add(3, 4.0)` - this is ambiguous (int→double, double→int both have same cost). Add a third overload `add(int, double)` or cast explicitly.
  • Memoization cache as a global - works for single tests but the cache persists across calls if the program calls `fib` multiple times from different test runs. In tests, clear the cache between runs.
  • Recursive power not handling `exp == 0` as base case - `power(0,0)` is mathematically undefined but the recursive split approach hits base case correctly. Always test edge cases: `power(x,0)`, `power(0,n)`, `power(x,-n)`.

Topics in This Module

0/8 done