liveBook · Manning

Private member

As explained in Object-Oriented Software Design in C++

A private member is a program element whose direct access is restricted to its defining class, module, file, or other language-defined scope.

A private member is a class member that outside code cannot access directly. In C++, private member variables store hidden state, while private member functions often provide internal helper behavior. The class exposes only the public operations that other code needs. section 5.0 section 1.8

Use access levels to control which code may depend on an implementation:

  • public members form the class interface and can be accessed by any code. section 1.8
  • private members are accessible only by member functions of their defining class. section 5.1 section 1.8
  • protected members are accessible to subclass member functions but remain hidden from other code. section 5.1

Private members support encapsulation and the Principle of Least Knowledge. A Book class can keep title and author private while offering public access without exposing whether those values are stored directly or obtained dynamically. Because clients cannot depend on the hidden implementation, the class can change internally with fewer effects on other code. section 5.0 section 5.1

Mammal state and helper behavior

A Mammal class keeps weight and height private and exposes const getter functions. Its private snore() function supports the protected sleep() function, which subclasses may use without seeing the internal snoring implementation. Human and Cat then keep their own state and helper functions private while implementing public behavior such as eat() and perform(). section 5.8

Figure 4.2 Possible UML class diagrams for class Catalogue that include different amounts of information. A plus sign (+) indicates public, and a minus sign (-) indicates private. How much information to include in a class diagram depends on whether the diagram is in high-level or low-level design documentation of the application.

Use the UML notation to identify access: a minus sign marks private members, a plus sign marks public members, and a hashtag marks protected members. section 4.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains access rules, encapsulation effects, helper functions, protected contrast, inheritance, and UML notation with C++ examples.
Examples
Demonstrates Book attributes, a Mammal hierarchy, getter functions, private helpers, subclass behavior, and UML access symbols.
What it assumes
Assumes basic C++ classes and member functions while introducing encapsulation, access control, and implementation hiding.
What you can do after
Evaluate class designs by separating public interfaces from private state and behavior, then document access levels in UML.
How it connects
Connects private members to encapsulation, abstraction, inheritance, polymorphism, the Principle of Least Knowledge, and the Open-Closed Principle.

Also explained in

Compare how 12 books explain private member →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.