Menu
Coddy logo textTech

Recap - Simple Calculator

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 11 of 104.

challenge icon

Challenge

Hard

Build a complete Calculator class that combines all the concepts you've learned:

  • Header/Source files — Separate declaration and implementation
  • Namespace — Place the class inside CalcApp
  • Private attributesname (string), memory (int), operationCount (int)
  • Constructors — Parameterized (name) and default ("Standard")
  • this pointer — Use this-> for all member access
  • Methodsadd, subtract, multiply, divide (each stores result in memory and increments operation count)
  • GettersgetName(), getMemory(), getOperationCount()

divide should return 0 when dividing by zero.

Try it yourself

#include <iostream>
#include "Calculator.h"

int main() {
    std::string name;
    int a, b;
    std::getline(std::cin, name);
    std::cin >> a >> b;
    
    CalcApp::Calculator calc(name);
    
    std::cout << "Calculator: " << calc.getName() << std::endl;
    std::cout << "Memory: " << calc.getMemory() << std::endl;
    std::cout << "Add: " << calc.add(a, b) << std::endl;
    std::cout << "Subtract: " << calc.subtract(a, b) << std::endl;
    std::cout << "Multiply: " << calc.multiply(a, b) << std::endl;
    std::cout << "Divide: " << calc.divide(a, b) << std::endl;
    std::cout << "Divide by zero: " << calc.divide(a, 0) << std::endl;
    std::cout << "Memory: " << calc.getMemory() << std::endl;
    std::cout << "Operations: " << calc.getOperationCount() << std::endl;
    return 0;
}

All lessons in Object Oriented Programming