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 of a class, it can access all of the public member data and functions of that class. The member functions of a class, however, can access all the private data members and functions of any class from which they derive.
Therefore, the function Dog::WagTail() can access the private data itsBreed and can access the protected data in the Mammal class.
Even if other classes are layered between Mammal and Dog (for example, DomesticAnimals), the Dog class will still be able to access the protected members of Mammal, assuming that these other classes all use public inheritance.
Listing 16.2 demonstrates how to create objects of type Dog and access the data and functions of that type.
Mammal sound! Tail wagging... Fido is 2 years old
On line 49 a Dog is declared, Fido. Fido inherits all the attributes of a Mammal, as well as all the attributes of a Dog. Thus Fido knows how to WagTail(), but also knows how to Speak() and Sleep().
18.118.198.81