Inheritance Background and Design
- See page 130 in the book.
- As we have seen, inheritance is a mechanism for deriving new classes from existing classes.
- Starting from a base class
- We can produce derived classes
- We can
- Add new members (data and methods)
- Overwrite the implementation of methods.
- Extend existing methods.
- The relationship between objects is often described as an is-a relationship.
- In our example, A farm is a building.
- We will do this for multiple different buildings.
- In UML
- An arrow from the derived class to the base class.
- The arrow "point" is a hollow triangle.
-
- Right now, our example just hides the implementation of methods.
- This is not the best.
- And you can see at the end where it sort of "fails"
- We can make a class abstract by declaring at least one method pure virtual.
- Virtual methods are methods that are intended to be replaced.
- Pure virtual methods are methods without an implementation.
- Polymorphism is the ability to adopt different forms.
- We have seen a primitive form of this.
- Polymophism allows us to employ hierarchies very efficiently.