|
|
|
|
|
|
|
(1)Circle (2)Rectangle (3)Square (0)Quit: 2
× × × × × ×
× × × × × ×
× × × × × ×
× × × × × ×
Abstract drawing mechanism!
(1)Circle (2)Rectangle (3)Square (0)Quit:3
× × × × ×
× × × × ×
× × × × ×
× × × × ×
× × × × ×
Abstract drawing mechanism!
(1)Circle (2)Rectangle (3)Square (0)Quit:0 |
|
|
|
|
|
|
|
|
Analysis: On lines 514, the abstract data type Shape is declared, with all three of its accessor methods declared to be pure virtual. Note that this is not necessary. If any one were declared pure virtual, the class would have been an ADT. |
|
|
|
|
|
|
|
|
The GetArea() and GetPerim() methods are not implemented, but Draw() is. Circle and Rectangle both override Draw(); and both chain up to the base method, taking advantage of shared functionality in the base class. |
|
|
|
|
|
|
|
|
Complex Hierarchies of Abstraction |
|
|
|
|
|
|
|
|
At times, you will derive ADTs from other ADTs. It may be that you want to make some of the derived pure virtual functions non-pure and leave others pure. |
|
|
|
|
|
|
|
|
If you create the Animal class, you can make Eat(), Sleep(), Move(), and Reproduce() pure virtual functions. Perhaps you derive Mammal and Fish from Animal. |
|
|
|
|
|
|
|
|
On examination, you decide that every Mammal will reproduce in the same way, and so you make Mammal::Reproduce() be non-pure, but you leave Eat(), Sleep(), and Move() as pure virtual functions. |
|
|
|
|
|
|
|
|
From Mammal you derive Dog, and Dog must override and implement the three remaining pure virtual functions so that you can make objects of type Dog. |
|
|
|
|
|
|
|
|
What you say, as class designer, is that no Animals or Mammals can be instantiated, but that all Mammals can inherit the provided Reproduce() method without overriding it. |
|
|
|
|
|
|
|
|
Listing 18.6 illustrates this technique with a bare-bones implementation of these classes. |
|
|
|
|
|