Hiding Methods in Derived Classes - C++ Class

C++ examples for Class:Inheritance

Description

Hiding Methods in Derived Classes

Demo Code

#include <cinttypes>
#include <iostream>

using namespace std;

class Vehicle//from   ww  w  .ja  v  a  2 s.c  o m
{
public:
    Vehicle() = default;

    int GetNumberOfWheels() const
    {
        return 0;
    }
};

class Car : public Vehicle
{
public:
    Car() = default;

    int GetNumberOfWheels() const
    {
        return 4;
    }
};

class Motorcycle : public Vehicle
{
public:
    Motorcycle() = default;
    int GetNumberOfWheels() const
    {
        return 2;
    }
};

int main(int argc, char* argv[])
{
    Vehicle myVehicle{};
    cout << "A vehicle has " << myVehicle.GetNumberOfWheels() << " wheels." << endl;

    Car myCar{};
    cout << "A car has " << myCar.GetNumberOfWheels() << " wheels." << endl;

    Motorcycle myMotorcycle;
    cout << "A motorcycle has " << myMotorcycle.GetNumberOfWheels() << " wheels." << endl;

    return 0;
}

Result


Related Tutorials