C++ Derived Class
1) Code is reusable, 2)Hierarchy reflects a relationship found, 3)Polymorphic Mechanisms
There are three main benefits to using a derived class:
- First, code is reusable. The grad_student type uses existing, tested code from the student type.
- Second, the hierarchy reflects a relationship found in the problem domain. When speaking of students, the special grouping "graduate student" is an outgrowth of the real world and its treatment of this group.
- Finally, various polymorphic mechanisms will allow client code to treat grad_student as a subtype of student, simplifying
client code while granting it the benefits of maintaining these distinctions among subtypes.
View of Inheritance using a Derived Class
To the derived class 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 derived class 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 derived class can access public and protected methods and data members declared in its base class 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 base class data member or method is no different than if the data member or method were declared as part of the derived
class.
void Sub::someOtherMethod()
{
cout << "I can access base class data member mProtectedInt." << endl;
cout << "Its value is " << mProtectedInt << endl;
}
When access specifiers (public, private, and protected) were introduced, the difference between private and protected may have been confusing.
Now that you understand derived classes, the difference should be clear. If a class declares methods or data members as protected, derived classes have access to them.
If they are declared as private, derived classes do not have╯access.
The following implementation of someOtherMethod() will not compile because the derived class
attempts to access a private data member from the base class.
void Sub::someOtherMethod()
{
cout << "I can access base class data member mProtectedInt." << endl;
cout << "Its value is " << mProtectedInt << endl;
cout << "The value of mPrivateInt is " << mPrivateInt << endl; // Error!
}
C++ private access specifier
The private access specifier gives you control over how a potential derived class could interact with your class. I recommend that you make all your data members private by default. You can provide public getters and setters if you want to allow anyone to access those data members, and you can provide protected getters and setters if you only want derived classes to access them. The reason to
make data members private by default is that this provides the highest level of encapsulation. This means that you can change how you represent your data while keeping the public and protected interface unchanged. Without giving direct access to data members, you can also easily add checks on the input data in your public and protected setters. Methods should also be private by default.
Only make those methods public that are designed to be public, and make methods protected if you only want derived classes to have access to them.