Although it's a common practice, and a good starting point, to put data members in a private section and member functions in a public section, we can't assume all members follow this pattern. Instead, it's crucial to check the accessibility symbols at the left of each member and translate them appropriately: "+" translates to a public member and "-" to a private one. Public data are rare, but private "helper" functions are sometimes helpful. You can put all private members in one section and all public members in another. Alternatively, you can create multiple public and private sections, separating data and functions into different groups.
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; };
Unlike Java, the public
and private
keywords are not applied to the class or individual members. In C++, the keywords label sections of a class: every feature appearing after a label exhibits the labeled accessibility until changed by another label. A class typically has two labeled sections but may have more if needed.
class ________ { private: feature1; feature2; public: feature3; feature4; };
UML class diagrams do not specify how a function performs its tasks (although other UML diagrams may). So, when we translate UML class diagrams into C++, the translation process only specifies the functions' prototype or signature and does not specify the contents of the functions' bodies.