C++ examples for Class:Member Access
Make them private and provide public accessors to class members
#include <cinttypes> #include <iostream> using namespace std; class Vehicle/* w w w .j av a2 s . c o m*/ { private: int m_NumberOfWheels{}; public: Vehicle(int numberOfWheels) : m_NumberOfWheels{ numberOfWheels } { } int GetNumberOfWheels() const { return m_NumberOfWheels; } }; class Car : public Vehicle { public: Car() : Vehicle(4) { } }; class Motorcycle : public Vehicle { public: Motorcycle() : Vehicle(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; }