< Previous | Contents | Next >
Using Access Modifiers When Deriving
When you derive a class from an existing one, you can use an access modifier, such as public, which I used in deriving Boss.
Using public derivation means that public members in the base class become public members in the derived class, protected members in the base class become protected members in the derived class, and private members in the base class are inaccessible in the derived class.
Tric k
Even if base data members are private, you can still use them indirectly through base class member functions. You can even get and set their values if the base class has accessor member functions.
Because Boss inherits from Enemy using the keyword public, Boss inherits Enemy’s public member functions as public member functions. It also means that Boss inherits m_Damage as a protected data member. The class essentially acts as if I simply copied and pasted the code for these two Enemy class members right into the Boss definition. But through the beauty of inheritance, I didn’t have to do this. The upshot is that the Boss class can access Attack() and m_Damage().
Hi n t
You can derive a new class with the protected and private keywords, but they’re rarely used and are beyond the scope of this book.