Controlling Access to Member Variables and Methods in Derived Classes, public Access Specifier - C++ Class

C++ examples for Class:Inheritance

Description

Controlling Access to Member Variables and Methods in Derived Classes, public Access Specifier

Demo Code

#include <cinttypes>
#include <iostream>

using namespace std;

class Vehicle//from w ww  .j  a v a 2s  .c  o  m
{
public:
    int m_NumberOfWheels{};

    Vehicle() = default;
};

class Car : public Vehicle
{
public:
    Car()
    {
        m_NumberOfWheels = 4;
    }
};

class Motorcycle : public Vehicle
{
public:
    Motorcycle()
    {
        m_NumberOfWheels = 2;
    }
};

int main(int argc, char* argv[])
{
    Car myCar{};
    cout << "A car has " << myCar.m_NumberOfWheels << " wheels." << endl;
    myCar.m_NumberOfWheels = 3;
    cout << "A car has " << myCar.m_NumberOfWheels << " wheels." << endl;

    Motorcycle myMotorcycle;
    cout << "A motorcycle has " << myMotorcycle.m_NumberOfWheels << " wheels." << endl;
    myMotorcycle.m_NumberOfWheels = 3;
    cout << "A motorcycle has " << myMotorcycle.m_NumberOfWheels << " wheels." << endl;

    return 0;
}

Result


Related Tutorials