You can extend almost any class you want. So once you’ve defined a base class, you just need to know enough about it, and how you want to derive from it.
Considering our GenericGeometric class from before, let’s look at deriving a Circle class from it.
In our header file we will have:
#include "GenericGeometric.h"
class DerivedCircle : public GenericGeometric
{
};
Notice two things that are different in this example from our initial Circle class. First, we need to include the header file for the base class. Second, after our class name, we have : public GenericGeometric
.
If we didn’t have public, we couldn’t access the public properties and methods of the base class. The colon is how C++ knows that we’re deriving from a base class.
However, private properties will still be hidden to us. Instead, we can access them via their public methods, just as any other developer. We’ll see an example of this in a few minutes.
To create a constructor, we need to build on. This is because constructors return the data type, and we will need to create a DerivedCircle, not a GenericGeometric.
DerivedCircle();
DerivedCircle(double size);
DerivedCircle::DerivedCircle()
{
}
DerivedCircle::DerivedCircle(double size)
{
setSize(size);
}
Derived Classes was originally found on Access 2 Learn