3.5 Software Engineering with Set and Get Member Functions

As you’ll see in the next section, set and get member functions can validate attempts to modify private data and control how that data is presented to the caller, respectively. These are compelling software engineering benefits.

If a data member were public, any client of the class—that is, any other code that calls the class’s member functions—could see the data and do whatever it wanted with it, including setting it to an invalid value.

You might think that even though a client of the class cannot directly access a private data member, the client can nevertheless do whatever it wants with the variable through public set and get functions. You’d think that you could peek at the private ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.