Private Versus Protected

You may have noticed that a new access keyword, protected, has been introduced on lines 23 and 43 of Listing 16.1. Previously, class data had been declared private. However, private members are not available to derived classes. You could make itsAge and itsWeight public, but that is not desirable. You don't want other classes accessing these data members directly.

What you want is a designation that says, “Make these visible to this class and to classes that derive from this class.” That designation is protected. Protected data members and functions are fully visible to derived classes, but are otherwise private.

There are, in total, three access specifiers: public, protected, and private. If a function has an instance ...

Get Sams Teach Yourself C++ in 24 Hours, Third 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.