Coding a simple class

I’d recommend you read up on basic C++ syntax before digging too deep into classes. You have some fundamental problems with not just the functions you’ve written but, also in how you use them.

Functions need to have a return type

Both your add and subtract functions do not have a return type; if you want a function that returns nothing, you specify the return type as void

When calling a function with parameters, pass them some

When you call AddNumbers, you aren’t passing it any numbers to add. What are you trying to do there? My guess is you’re looking for something more like this:

class CalculateNumbers
        // Constructor
        // Pass the two numbers you want calculations to be done on
        CalculateNumbers(int a, int b); 

        int AddNumbers();
        int SubtractNumbers();

        int m_firstNumber;
        int m_secondNumber;

CalculateNumbers::CalculateNumbers(int a, int b)
    : m_firstNumber(a)
    , m_secondNumber(b)


int CalculateNumbers::AddNumbers()
    return m_firstNumber + m_secondNumber;

int CalculateNumbers::SubtractNumbers()
    return m_firstNumber - m_secondNumber;

CalculateNumbers instance(10, 5);

instance.AddNumbers(); // returns 15
instance.SubtractNumbers(); // returns 5

In the future it would be helpful if you included more details about what you’re attempting to accomplish, and even better is if you have a specific question you’d like answered.

Browse More Popular Posts

Leave a Comment