Adding Methods to class - C++ Class

C++ examples for Class:Member Function

Description

Adding Methods to class

Demo Code

#include <cinttypes>
#include <iostream>

using namespace std;

class Car/*from www . j  a  v a2  s . c o  m*/
{
private:
    int m_NumberOfWheels;

public:
    void SetNumberOfWheels(int numberOfWheels)
    {
        m_NumberOfWheels = numberOfWheels;
    }

    int GetNumberOfWheels()
    {
        return m_NumberOfWheels;
    }
};

int main(int argc, char* argv[])
{
    Car myCar;
    myCar.SetNumberOfWheels(4);

    cout << "Number of wheels: " << myCar.GetNumberOfWheels() << endl;

    Car myMotorcycle;
    myMotorcycle.SetNumberOfWheels(2);

    cout << "Number of wheels: " << myMotorcycle.GetNumberOfWheels() << endl;

    return 0;
}

Result


Related Tutorials