Online Book Reader

Home Category

Professional C__ - Marc Gregoire [108]

By Root 1427 0
the compiler can only think of it as type Super which does not have someOtherMethod() defined. This problem will be addressed later with the discussion of virtual methods.

A Subclass’s View of Inheritance

To the subclass itself, nothing much has changed in terms of how it is written or how it behaves. You can still define methods and data members on a subclass just as you would on a regular class. The previous definition of Sub declares a method called someOtherMethod(). Thus, the Sub class augments the Super class by adding an additional method.

A subclass can access public and protected methods and data members declared in its superclass as though they were its own, because technically, they are. For example, the implementation of someOtherMethod() on Sub could make use of the data member mProtectedInt, which was declared as part of Super. The following code shows this implementation. Accessing a superclass data member or method is no different than if the data member or method were declared as part of the subclass.

void Sub::someOtherMethod()

{

cout << "I can access superclass data member mProtectedInt." << endl;

cout << "Its value is " << mProtectedInt << endl;

}

When we introduced access specifiers (public, private, and protected) in Chapter 6, the difference between private and protected may have been confusing. Now that you understand subclasses, the difference should be clear. If a class declares methods or data members as protected, subclasses have access to them. If they are declared as private, subclasses do not have access.

The following implementation of someOtherMethod() will not compile because the subclass attempts to access a private data member from the superclass.

void Sub::someOtherMethod()

{

cout << "I can access superclass data member mProtectedInt." << endl;

cout << "Its value is " << mProtectedInt << endl;

cout << "The value of mPrivateInt is " << mPrivateInt << endl; // BUG!

}

The private access specifier gives you control over how a potential subclass could interact with your class. In practice, most data members are declared as protected, and most methods are either public or protected. The reason is that most of the time, you or someone you work with will be extending the class so you don’t want to shut out any potential uses by making methods or members private. Occasionally, the private specifier is useful to block subclasses from accessing potentially dangerous methods. It is also useful when writing classes that external or unknown parties will extend because you can block access to prevent misuse. For example, in your superclass you can declare private data members to prevent anyone, including subclasses from accessing them directly. To allow only subclasses to change those values, you can provide protected setter and getter methods.

From the perspective of a subclass, all public and protected data members and methods from the superclass are available for use.

Preventing Inheritance

C++11 allows you to mark a class as final, which means trying to inherit from it will result in a compiler error. A class can be marked as final with the final keyword right behind the name of the class. For example, the following Super class is marked as final:

class Super final

{

// Omitted for brevity

};

The following Sub class tries to inherit from the Super class, but this will result in a compiler error because Super is marked as final.

class Sub : public Super

{

// Omitted for brevity

};

Overriding Methods

The main reasons to inherit from a class are to add or replace functionality. The definition of Sub adds functionality to its parent class by providing an additional method, someOtherMethod(). The other method, someMethod(), is inherited from Super and behaves in the subclass exactly as it does in the superclass. In many cases, you will want to modify the behavior of a class by replacing, or overriding, a method.

How I Learned to Stop Worrying and Make Everything virtual

There is one small twist to overriding methods in C++ and it has to do with the keyword virtual.

Return Main Page Previous Page Next Page

®Online Book Reader