Chapter 24
Do Not Disturb: Protected Members
In This Chapter
Protecting members of a class
Asking, “Why do that?”
Declaring friends of the class
My goal with this part of the book, starting with Chapter 21, is to model real-world objects in C++ by using the class structure. In Chapter 22, I introduce the concept of member functions in order to assign active properties to the classes. Returning to the microwave oven example in Chapter 21, assigning active properties allows me to give my Oven class properties like cook() and defrost().
However, that’s only part of the story. I still haven’t put a box around the insides of my classes to ward off meddling. I can’t very well hold someone responsible if the microwave catches on fire so long as the insides are exposed to anyone who wants to mess with them.
This chapter “puts a box” around the classes by declaring certain members off-limits to user functions.
Protecting Members
Members of a class can be flagged as inaccessible from outside the class with the keyword protected. This is in direct opposition to the public keyword, which designates those members that are accessible to all functions. The public members of a class form the interface ...
Get Beginning Programming with C++ For Dummies, 2nd Edition 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.