Each class defines its own scope (§7.4, p. 282) within which its members are defined. Under inheritance, the scope of a derived class is nested (§2.2.4, p. 48) inside the scope of its base classes. If a name is unresolved within the scope of the derived class, the enclosing base-class scopes are searched for a definition of that name.
The fact that the scope of a derived class nests inside the scope of its base classes can be surprising. After all, the base and derived classes are defined in separate parts of our program’s text. However, it is this hierarchical nesting of class scopes that allows the members of a derived class to use members of its base class as if those members were part of the derived class. For example, when we write
Bulk_quote bulk;
cout << bulk.isbn();
the use of the name isbn
is resolved as follows:
• Because we called isbn
on an object of type Bulk_quote
, the search starts in the Bulk_quote
class. The name isbn
is not found in that class.
• Because Bulk_quote
is derived from Disc_quote
, the Disc_quote
class is searched next. The name is still not found.
• Because Disc_quote
is derived from Quote
, the Quote
class is searched next. The name isbn
is found in that class; the use of isbn
is resolved to the isbn
in Quote
.