476 Day 14
98: break;
99: case 1: sp = new Circle(5);
100: break;
101: case 2: sp = new Rectangle(4,6);
102: break;
103: case 3: sp = new Square(5);
104: break;
105: default:
106: cout <<”Please enter a number between 0 and 3”<<endl;
107: continue;
108: break;
109: }
110: if( !fQuit )
111: sp->Draw();
112: delete sp;
113: sp = 0;
114: cout << endl;
115: }
116: return 0;
117: }
(1)Circle (2)Rectangle (3)Square (0)Quit: 2
x x x x x x
x x x x x x
x x x x x x
x x x x x x
(1)Circle (2)Rectangle (3)Square (0)Quit: 3
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
(1)Circle (2)Rectangle (3)Square (0)Quit: 0
On lines 7–16, the Shapeclass is declared. The GetArea()and GetPerim()
methods return an error value, and Draw()takes no action. After all, what does it
mean to draw a Shape? Only types of shapes (circles, rectangles, and so on) can be
drawn; Shapesas an abstraction cannot be drawn.
Circlederives from Shapein lines 18–29 and overrides the three virtual methods. Note
that no reason exists to add the word “virtual,” because that is part of their inheritance.
But there is no harm in doing so either, as shown in the Rectangleclass on lines 43, 44,
and 47. It is a good idea to include the term virtualas a reminder, a form of
documentation.
OUTPUT
LISTING14.7 continued
ANALYSIS