Challenge 3: Implement a Calculator Class

In this exercise, you have to implement a calculator which can perform addition, subtraction, multiplication, and division.

Problem Statement

Write a C++ class called Calculator with

  • private member variables:

    • num1 and num2 (float type)

And member functions:

  • add(float n1, float n2), a function which returns the addition of two numbers

  • Subtract(float n1, float n2), a function which returns the subtraction of n1 from n2

  • multiply(float n1, float n2), a function which returns the multiplication of numbers

  • divide(float n1, float n2), a function which returns the division of n2 by n1

  • Define a default constructor which initializes both numbers by zeros

Input

Pass floating point numbers in the member functions

Output

Addition, Subtraction, division, and multiplication

Sample Input

calculator obj;
obj.add(10, 94)
obj.subtract(10, 94)
obj.multiply(10, 94)
obj.divide(10, 94)

Sample output

104
84
940
9.4

Get hands-on with 1200+ tech skills courses.