C++ examples for Class:Member Access
protected access specifier acts like a public specifier for classes that derive from the current class, and it acts like a private specifier for external code.
#include <cinttypes> #include <iostream> using namespace std; class Vehicle/*from w ww .jav a 2 s . co m*/ { protected: int m_NumberOfWheels{}; public: Vehicle() = default; int GetNumberOfWheels() const { return m_NumberOfWheels; } }; 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.GetNumberOfWheels() << " wheels." << endl; Motorcycle myMotorcycle; cout << "A motorcycle has " << myMotorcycle.GetNumberOfWheels() << " wheels." << endl; return 0; }