Search

11.6 — Adding new functionality to a derived class

In the introduction to inheritance lesson, we mentioned that one of the biggest benefits of using derived classes is the ability to reuse already written code. You can inherit the base class functionality and then add new functionality, modify existing functionality, or hide functionality you don’t want. In this and the next few lessons, we’ll take a closer look at how each of these things is done.

First, let’s start with a simple base class:

Now, let’s create a derived class that inherits from Base. Because we want the derived class to be able to set the value of m_value when derived objects are instantiated, we’ll make the Derived constructor call the Base constructor in the initialization list.

Adding new functionality to a derived class

In the above example, because we have access to the source code of the Base class, we can add functionality directly to Base if we desire.

There may be times when we have access to a base class but do not want to modify it. Consider the case where you have just purchased a library of code from a 3rd party vendor, but need some extra functionality. You could add to the original code, but this isn’t the best solution. What if the vendor sends you an update? Either your additions will be overwritten, or you’ll have to manually migrate them into the update, which is time-consuming and risky.

Alternatively, there may be times when it’s not even possible to modify the base class. Consider the code in the standard library. We aren’t able to modify the code that’s part of the standard library. But we are able to inherit from those classes, and then add our own functionality into our derived classes. The same goes for 3rd party libraries where you are provided with headers but the code comes precompiled.

In either case, the best answer is to derive your own class, and add the functionality you want to the derived class.

One obvious omission from the Base class is a way for the public to access m_value. We could remedy this by adding an access function in the Base class -- but for the sake of example we’re going to add it to the derived class instead. Because m_value has been declared as protected in the Base class, Derived has direct access to it.

To add new functionality to a derived class, simply declare that functionality in the derived class like normal:

Now the public will be able to call getValue() on an object of type Derived to access the value of m_value.

This produces the result:

derived has value 5

Although it may be obvious, objects of type Base have no access to the getValue() function in Derived. The following does not work:

This is because there is no getValue() function in Base. Function getValue() belongs to Derived. Because Derived is a Base, Derived has access to stuff in Base. However, Base does not have access to anything in Derived.

11.6a -- Calling inherited functions and overriding behavior
Index
11.5 -- Inheritance and access specifiers

16 comments to 11.6 — Adding new functionality to a derived class

  • Omri

    Right, preecompiled can be both ( contradictory!) understood as:
    a. not yet compiled
    b. already compiled
    Actually, I think there is no way to decide which is intended other than knowing beforehand… Or perhaps "precompiled" is a jargon shorthand for "previously compiled"…
    This is quite different from ordinary use of pre ( like prehistory) which means before (as before history).
    No end to confusion possibilities…

    As per:
    "Yes, by precompiled, I mean compiled before being distributed to the user of the library."
    Do youvmean:
    "Yes, by precompiled, I mean compiled and added as such to the library before distribution of the library to users."
    Thanks.

    • Alex

      Yeah, I guess it is a little weird. Precompiled is a term in computer science (usually used in the context of precompiled headers) meaning that something was already compiled. So rather than “before being compiled”, it means “compiled before”.

      And by precompiled I mean, “the library code files were compiled by the library creator, and distributed in compiled form to customers”.

  • Omri

    Regarding:
    "The same goes for 3rd party libraries where you are provided with headers but the code comes precompiled."
    My understanding was that the code in a library comes ***compiled*** and is only linked to the user project when called.
    I will happily be advised.
    Thanks.

  • Carlos

    I am following your tutorials and I feel the need to write you just thanking this awesome content and the effort you did to do it. Thank you thousand times! hope existed more people like you 🙂

  • roy

    hi,good teacher..
    cplus is very difficult, but by reading a lot of things become clear,
    but huge contents need to remember,
    so i think cplus need a revolution to make it less errorprone! i used c 12years.thks

  • dazedbrain

    Because Derived is a Base, Derived has access to stuff in Base.

    I thought "Because Derived is derived from Base" would make more sense.

  • Reaversword

    I’ve been thinking about this, trying to reach a "logical-intuitive" example.

    This is my example, but it includes some questions that get this topic a little bit "out of the limits", I think.

    4 classes:

    Grandfather (60 year old man, owner of a shop)
    Father (32 years old man, co-owner of a shop)
    Son1 (8 years old boy, still doesn’t know exactly what he does, no dental caries diagnosed).
    Son2 (10 years old boy, still doesn’t know exactly what he does, dental caries diagnosed).

    Grandfather has:
    dentures (private), safeDepositBox (protected), tv (protected), candies (public).

    Father has access to all except dentures.

    But, I confess I’m unable to get this working (cause I’m looking for the limits):

    *This lesson answers question 1.

    1*) How can Father "private" the son to get access to safeDepositBox, but let him use tv?
    (This would be get 2 base protected members, and get one private, and other still protected, but this would be per-member, and not per-class).

    2) How can Father "private" the Son2 to get access to candies (because dental caries), but still be able to offer it to Son1?
    (This would be a member-private for one derived class, but available for the other derived one).

    3) How can Father "private" the Sons to get access to candies (because dental caries), but still be able to offer it to public?
    (This would be a member-private for derived classes, but available for the public).

    Are any of this two last cases possible?. Or this is go to much out of the limits / rules?.

    • Alex

      > How can Father “private” the son

      I don’t know what this means. Are you asking how Son can derived from Father, keeping safeDepositBox private but keeping tv protected? If Father doesn’t want anybody to inherit access to the safeDepositBox by default, he could redefine it as private. But the Son could still override it.

      Essentially, access controls are there to help enforce good encapsulation, not to strictly keep derived classes in or out.

      • Reaversword

        I was asking how Father prevent Son access to safeDepositBox, but still leaves him use tv. But this is perfectly answered here.

        Father just redeclares safeDepositBox as private , and do nothing with tv (because is already declared as "protected" by Grandfather).

        So, interesting ones (and I think a little bit out of limits) are 2) and 3).

  • Thanks again for this absolutely wonderful tutorials!

  • Kinten

    ok, the only diference in private and protected inheritance is while making multiple inheritances? say yo got:

    base -> derived1 -> derived2 (diagram)

    if derived1 does Private inheritance then derived2 can’t acces anything from base class, otherwise if I make derived1 Protected derived2 will have acces, but not the program.
    I am right?

    • Kinten

      Multiple inheritance was’t the right word, it shold be “multiple times derived”

    • If derived1 does private inheritance, than derived2 can’t access anything from the base class, as you say.

      If derived1 uses protected inheritance, then public and protected members in the base class become protected in derived1. This means that they can be accessed by derived2. Note that neither derived1 nor derived2 can access private members in base.

      In reality, it is pretty uncommon to see private and protected inheritance. Almost all of the inheritance you see is done via public inheritance.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter