Calculator C++ API Documentation

This page demonstrates how to use breathe to document C++ code with yardang.

Calculator Class

class Calculator

A class for performing basic arithmetic operations.

The Calculator class provides methods for addition, subtraction, multiplication, and division. It also maintains a history of operations performed.

Example usage:

calc::Calculator calc;
double result = calc.add(5.0, 3.0);
std::cout << "Result: " << result << std::endl;

Note

This is a thread-safe implementation.

Subclassed by calc::ScientificCalculator

Public Functions

Calculator()

Default constructor.

Initializes an empty calculator with no operation history.

virtual ~Calculator()

Destructor.

double add(double a, double b)

Add two numbers.

See also

subtract()

Parameters:
  • a – The first operand.

  • b – The second operand.

Returns:

The sum of a and b.

double subtract(double a, double b)

Subtract two numbers.

See also

add()

Parameters:
  • a – The minuend.

  • b – The subtrahend.

Returns:

The difference (a - b).

double multiply(double a, double b)

Multiply two numbers.

See also

divide()

Parameters:
  • a – The first factor.

  • b – The second factor.

Returns:

The product of a and b.

double divide(double a, double b)

Divide two numbers.

See also

multiply()

Warning

Division by zero will throw an exception.

Parameters:
  • a – The dividend.

  • b – The divisor.

Throws:

std::invalid_argument – if b is zero.

Returns:

The quotient (a / b).

std::vector<OperationResult> getHistory() const

Get the history of all operations.

Returns:

A vector containing all operation results.

void clearHistory()

Clear the operation history.

size_t getOperationCount() const

Get the number of operations performed.

Returns:

The count of operations in history.

Protected Functions

void recordOperation(const OperationResult &result)

Record an operation in the history.

Parameters:

result – The result to record.

ScientificCalculator Class

class ScientificCalculator : public calc::Calculator

An extended calculator with scientific functions.

This class inherits from Calculator and adds advanced mathematical operations like power and square root.

Public Functions

double power(double base, double exponent)

Calculate the power of a number.

Parameters:
  • base – The base number.

  • exponent – The exponent.

Returns:

base raised to the power of exponent.

double squareRoot(double value)

Calculate the square root of a number.

Parameters:

value – The number to find the square root of.

Throws:

std::invalid_argument – if value is negative.

Returns:

The square root of value.

Enumerations

Operation Enum

enum class calc::Operation

Enumeration of supported arithmetic operations.

Values:

enumerator ADD

Addition operation.

enumerator SUBTRACT

Subtraction operation.

enumerator MULTIPLY

Multiplication operation.

enumerator DIVIDE

Division operation.

Structures

OperationResult

struct OperationResult

Structure to hold the result of a calculation.

Public Members

double value

The calculated value.

Operation operation

The operation that was performed.

std::string description

Human-readable description of the operation.

Functions

formatNumber

std::string calc::formatNumber(double value, int precision = 2)

A helper function to format a number as a string.

Parameters:
  • value – The number to format.

  • precision – The number of decimal places.

Returns:

A formatted string representation.

Defines

MAX_HISTORY_SIZE 1000

Maximum number of operations to store in history.

Type Definitions

typedef std::vector<OperationResult> calc::HistoryList

Type alias for the operation history container.