$\require{cancel}$
Chapter 5
- Back to the book, chapter 5.
- Again, "What are the (real world) objects I am modeling?"
- I don't believe real world is part of this.
- Classes:
- Stroustrap thinks class = data type.
- An instance of a class is a specific item of a class. Frequently referred to as objects.
- "A class is simply the encapsulation of what defines a classification of objects"
- Classes can be formed from component classes.
- Composition is another principle of oo design.
- An airplane class has an engine object, a wing object, ....
- More on this in a bit.
- A class has properties
- The values of the member data.
- What makes this object unique
- Behaviors
- What does this object do? or What can I do with this object?
- In general, we can build three types of things.
- If something only has properties, or values it is data.
- If something only has behavior, it is an algorithm.
- If something has both, it is a class.
- The trick is deciding what are our objects, our data, our functions.
- Class Relationships or composition
- There are multiple ways you can combine classes.
- has-a relationship is composition.
- is-a relationship is inheritance.
- A frog has-a heart ie the frog is composed of several things, including a heart. (true Composition)
- In this case, a heart must have a frog, or it can not exist.
- A pond has-a frog, ie a pond can hold a frog, but might have several or none. (Aggregation)
- In this case a frog can exist without a pond.
- A frog is-a animal, ie the frog has characteristics common to all animals (inheritance)
- The animal contains the behaviors and properties common to all animals.
- A frog is less general, it contains behaviors and properties unique to frogs, but inherits the behaviors and properties of an animal.
- In inheritance we can
- Add or replace properties and functionality of the base class.
- More on this in chapter 10.