Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 9 of 10
Mini Project Part 1 - Multi-File Calculator Build
Build a multi-file scientific calculator that demonstrates the complete compilation pipeline, namespace organisation, `constexpr`, `enum class` for operations, formatted output with `<iomanip>`, and a proper CMake build. Everything from this module applied in one coherent project.
Multi-File Scientific Calculator - Complete Project
// === include/calculator.h ===
#pragma once // modern include guard (non-standard but universal)
#include <stdexcept>
#include <cmath>
namespace calc {
enum class Operation {
Add, Subtract, Multiply, Divide,
Power, SquareRoot, Log, Sin, Cos
};
constexpr double PI = 3.14159265358979323846;
constexpr double E = 2.71828182845904523536;
// Declarations
double evaluate(double a, double b, Operation op);
double evaluate_unary(double a, Operation op);
const char* op_name(Operation op);
} // namespace calc
// === src/calculator.cpp ===
#include "calculator.h"
#include <cmath>
#include <stdexcept>
namespace calc {
double evaluate(double a, double b, Operation op) {
switch (op) {
case Operation::Add: return a + b;
case Operation::Subtract: return a - b;
case Operation::Multiply: return a * b;
case Operation::Divide:
if (b == 0.0) throw std::domain_error("Division by zero");
return a / b;
case Operation::Power: return std::pow(a, b);
default:
throw std::invalid_argument("Not a binary operation");
}
}
double evaluate_unary(double a, Operation op) {
switch (op) {
case Operation::SquareRoot:
if (a < 0) throw std::domain_error("sqrt of negative");
return std::sqrt(a);
case Operation::Log:
if (a <= 0) throw std::domain_error("log of non-positive");
return std::log(a);
case Operation::Sin: return std::sin(a * PI / 180.0); // degrees
case Operation::Cos: return std::cos(a * PI / 180.0);
default:
throw std::invalid_argument("Not a unary operation");
}
}
const char* op_name(Operation op) {
switch (op) {
case Operation::Add: return "+";
case Operation::Subtract: return "-";
case Operation::Multiply: return "*";
case Operation::Divide: return "/";
case Operation::Power: return "^";
case Operation::SquareRoot: return "sqrt";
case Operation::Log: return "log";
case Operation::Sin: return "sin";
case Operation::Cos: return "cos";
}
return "?";
}
} // namespace calc
// === src/main.cpp ===
#include <iostream>
#include <iomanip>
#include "calculator.h"
int main() {
using namespace std;
using calc::Operation;
cout << fixed << setprecision(6);
// Binary operations
struct { double a, b; Operation op; } tests[] = {
{10.0, 3.0, Operation::Add},
{10.0, 3.0, Operation::Divide},
{ 2.0, 8.0, Operation::Power},
};
cout << "=== Binary Operations ===
";
for (auto& t : tests) {
cout << setw(8) << t.a << " " << calc::op_name(t.op)
<< " " << setw(8) << t.b
<< " = " << calc::evaluate(t.a, t.b, t.op) << "
";
}
cout << "
=== Unary Operations ===
";
cout << "sqrt(2) = " << calc::evaluate_unary(2, Operation::SquareRoot) << "
";
cout << "sin(90°) = " << calc::evaluate_unary(90, Operation::Sin) << "
";
cout << "log(e) = " << calc::evaluate_unary(calc::E, Operation::Log) << "
";
// Exception demonstration
try {
calc::evaluate(5.0, 0.0, Operation::Divide);
} catch (const std::domain_error& e) {
cerr << "Caught: " << e.what() << "
";
}
return 0;
}
// Build: cmake -B build && cmake --build build && ./build/myappCommon Mistakes
- Putting all code in main.cpp - this prevents reuse and makes testing hard. Split by responsibility: separate header + implementation for each logical module.
- Using #define for constants instead of constexpr - `#define PI 3.14` has no type, no scope, no namespace. `constexpr double PI = 3.14159` is type-safe and namespace-aware.
- Not handling exceptions from math functions - `std::sqrt(-1)` doesn't throw - it returns `NaN`. Always validate inputs before calling math functions if you want meaningful errors.
Tip
Tip
Practice Mini Project Part 1 MultiFile Calculator Build in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Header Guards, Translation Units, Object Compilation, and Final Linker Resolution
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Part 1 MultiFile Calculator Build 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 1 MultiFile Calculator Build 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
- Build a multi-file scientific calculator that demonstrates the complete compilation pipeline, namespace organisation, `constexpr`, `enum class` for operations, formatted output with `<iomanip>`, and a proper CMake build.
- Putting all code in main.cpp - this prevents reuse and makes testing hard. Split by responsibility: separate header + implementation for each logical module.
- Using #define for constants instead of constexpr - `#define PI 3.14` has no type, no scope, no namespace. `constexpr double PI = 3.14159` is type-safe and namespace-aware.
- Not handling exceptions from math functions - `std::sqrt(-1)` doesn't throw - it returns `NaN`. Always validate inputs before calling math functions if you want meaningful errors.
Finished this lesson?