We can't just assume that all variables go in the private section and that all functions go in the public section. Attributes/Member data are typically private and operations/member functions are typically public, but it is possible to have public data (which is rare) and private functions. So you must look at the visibility symbol on the left-hand side of each feature: "+" indicates a public feature while "-" indicates a private feature. You have the option of putting the private features together, and the public features together. Alternatively, you can create multiple public
and private
sections as needed to keep the variables and the functions separate.
class Car { private: string model; int wheels; double engine_size; public: Car(string a_name, int a_wheels, double a_engine_size); string get_model(); bool is_running(); void set_wheels(int w); double accelerate(int fuel_rate); public: string vin; private: int diagnostics(char system); };
class Car { private: string model; int wheels; double engine_size; int diagnostics(char system); public: Car(string a_name, int a_wheels, double a_engine_size); string get_model(); bool is_running(); void set_wheels(int w); double accelerate(int fuel_rate); string vin; };
public
and private
keywords are not applied to the class or to individual features. In C++, public
and private
label sections of a class and every feature that appears after a label exhibits the labeled accessibility. A class typically has two labeled sections but may have more if needed.
class ________ { private: feature1; feature2; public: feature3; feature4; };