private inheritance
private
is used in the line where the derived class declares its inheritance from a base class.
1 | class Base |
e.g. a class Car
related to class Motor
via private inheritance. demolist10_8
1 |
|
line 23-28 demonstrates that inside the class and derived class, private heritance, class Car
has access to the base class Motor
, on the other hand, in main()
a object of derived class Car
via private heritance can't invoke methods(members) of base class Motor
directly. For instance, if method SwitchIgnition
was invoked like DreamCar.SwitchIgnition()
, compared with line 33-34, error will appear as cannot access base's public member
.
additionally, inside main()
error happens when using DreamCar.Car::SwitchIgnition
.
protected inheritance
Q:does a derived class can be derived from two or more base class?
A:you can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. e.g.:
1 | class A { /* ... */ }; |
protected inheritance is similar to private inheritance in the following ways:
- it also signifies a has-a relationship.
- derived class accesses all public and protected members of base class
- those outside the inheritance hierarchy with an instance of derived can't access public member of base, like implementation in
main()
.
the differences lies in following:
1 | class Derived2 : protected Derived |
protected inheritance hierarchy allow the subclass of the subclass access to protected and public members of base, but that can't happen for private inheritance hierarchy.
using protected inheritance demolist10_9
1 |
|
Note that: using protected
or public
inheritance only when you have to.
having an instance of Motor
as a private member instead of inheriting from it (indicate Motor
) is called aggregation
or composition
.
e.g.
1 | class Car |
This can be good design as it enables you to easily add more motors as members attributes to an existing class Car
.
The problem of slicing
Slicing happens when code like:
1 | Derived objDerived; |
or alternatively,
1 | void UseBase(Base Input); |
In both case, the object of type Derived
is being copied into another of type Base
, either explicitly via assignment or by passing as an argument. That will cause unwanted reduction of data.
To avoid slicing problem, don't pass parameter by value. Pass it as pointers to the Base class or reference to the same.
multiple inheritance
A class derives from two or more base classes.
Syntax:
1 | class Derived: access-specifier Base1, access-specifier Base2 |
A manifestation of Platypus that demonstrates multiple inheritance is demonstrated by the following.demolist10_10
1 |
|
: class DerivedClass
is able to invoke the three characteristics of the individual base classes using an object of DerivedClass
which is named objDerived1
.
avoiding inheritance using final
It's used to ensure that a class declared as final
can't be used as a base class. A version of class DerivedClass
taken from demolist10_10 and declared as final
would look like this:
1 | class DerivedClass final: public BaseClass1, public BaseClass2, public BaseClass3 |
The public inheritance is an is-a relationship between base class and derived class, whereas private and protected inheritance create has-a relationship