Polymorphism
- having many forms.
- But most likely for us
- A base class
- Several derived classes.
- Let's think about a simple unit for the stompy robot game.
- We probably have different classes of .
- Robots
- Tanks/trucks/APC
- Infantry/civilians/robot-pilots
- Airplanes
- Ships
- Buildings (Forts/walls/...)
- Mines/traps/...
- There are a set of common methods we will want each to implement
- Given the problems with casting, we should have the same methods in everyting.
- These probably include
- Move
- Attack
- Defend
- TakeDamage
- Plus a set of observers like
- Name
- Description
- Facton
- Reset
- CanAttack
- ...
- Much of the actual logic of attack will be part of the weapon class.
- It will need to determine cover and range.
- It will determine damage
- But these will be called by the unit class.
- I am not the happiest with this example.
- What should the move function do?
- Move:
- It most likely takes a destination.
- And returns a bool if the move were successful
- How would the move function work?
- In a robot without jump jets
- Ask the map for a path from source to destination.
- Make sure that the path is traversable, or not blocked.
- This is specialized by unit type.
- Compute the total cost of moving along the path.
- If everything is ok,
- For each sector along the path
- Move robot to new location.
- Check for encounter in new location.
- In a robot with jump jets
- Ask the map for a path from source to destination.
- No obsticles.
- Compute the total cost of moving along the path.
- If everything is ok, move driectly to the new location.
- Check for encounter in the new location.
- An airpane:
- Basically the same, but it would not land
- A ship:
- Would only be able to move in water sectors.
- What should we do about the move function in the base class?
- We could just return true
- But that would cause a purity of design problem.
- This assums everything was successful.
- We could return false
- But this has the same problem.
- Pure virtual method/Abstract classes.
- When there is no clear implementation of a method in a base class.
- But you want to have it present to specify the interface for polymophism
-
vitrual type function(params) specifiers = 0
- This is a pure virtual method.
- It is explicitly undefined in the base class.
- A class that contains a pure virtual methods
- Is called an abstract class
- An abstract class can not be constructed.
- By declaring a pure virtual method we are
- refusing to commit to how it will be implmented in any derived class.
- But forcing it to be implemented in derived classes.