Simple Class Methods with Struct

The struct keyword constructs a class without implicit private and public access control. Everything is public. In the following example, we create a simple Calculator class with some methods.

#include <cstdio>

struct Calculator
{
    int Sum(int x, int y)
    {
        return x + y;
    }

    int Substract(int x, int y)
    {
        return x - y;
    }
};

int main()
{

    Calculator simple_calc;
    printf("The sum of %d and %d is %d\n", 2, 3, simple_calc.Sum(2, 3));
    printf("The difference of %d and %d is %d\n", 2, 3, simple_calc.Substract(2, 3));
}

We can now compile and run the program.

$ g++ simple_class_methods.cpp -std=c++17 -o simple_class_methods
$ ./simple_class_methods
The sum of 2 and 3 is 5
The difference of 2 and 3 is -1