Build a calculator with C++

Add a Calculator Class

It’s time to add some math logic.

Go to the Project menu, and choose Add Class. Under “Class Name”, write Calculator. Click OK.



Create a class

Automatically, .h and .cpp files with the same name as the class will be created. You can see a full list of your project files in the Solution Explorer window (it should be visible on the side of the IDE). If the window isn’t visible, you can open it by going to the View menu and choosing Solution Explorer.



Solution Explorer

You should now have three tabs open in the editor: CalculatorTutorial.cpp, Calculator.h, and Calculator.cpp. If you accidentally close one of them, you can re-open it by double clicking it in the Solution Explorer.

Remove the Calculator(); and ~Calculator(); lines and add the following line to Calculator.h:

#pragma once 
class Calculator
{
public: 
double Calculate(double x, char oper, double y); 
};

This line declares a new function called Calculate, which we’ll use to run the math operations for the calculator. You’ll see a green squiggle appear under “Calculate”. Hover over the word, click the lightbulb that pops up, and choose Create definition of ‘Calculate’ in Calculator.cpp. A pop-up will appear, giving you a peek of the code change that was made. You will see the function defined in Calculator.cpp.



CreateDefinition

Currently, it just returns 0.0. Let’s change that. Press Esc to close the pop-up.

Switch to the Calculator.cpp file in the editor window. Remove the Calculator() and ~Calculator() sections and add the following code to the Calculate function:

double Calculator::Calculate(double x, char oper, double y)
{
    switch(oper)
    {
    case '+':
        return x + y;
    case '-':
        return x - y;
    case '*':
        return x * y;
    case '/':
        return x / y;
    default:
        return 0; 
    }
}

The function Calculate will consume a number, an operator, and a second number, then perform the requested operation on the numbers.

Now let’s update the main function in CalculatorTutorial.cpp:

// CalculatorTutorial.cpp : Defines the entry point for the console application. 
//

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

using namespace std;  
  
int main()  
{  
    double x = 0.0;
    double y = 0.0; 
    double result = 0.0;
    char oper = '+';

    cout << "Calculator Console Application" << endl << endl;   
    cout << "Please enter the operation to perform. Format: a+b | a-b | a*b | a/b" << endl;
    
    Calculator c;
    while (true)
    {
        cin  >> x  >> oper  >> y;
        result = c.Calculate(x, oper, y);
        cout << "Result is: " << result << endl;
    }

    return 0;  
}

We will use the Calculate member function of the Calculator class to execute the operations provided by the user, which we store via two variables (x, y) and operator (oper). The result of the operation is stored in result.

Now it’s time to test the program again. Hit Ctrl + F5 and type 5 + 5, and press Enter. Verify that the result brought back is 10.

FivePlusFive

Close the terminal window when you are done.


I ran into an issue Continue tutorial