< previous page page_290 next page >

Page 290
be an abstract data type. To save space, the rest of Listing 18.3 is not reproduced here. Replace the declaration of Shape in Listing 18.3, lines 514, with the declaration of Shape in Listing 18.4 and run the program again.
LISTING 18.4 ABSTRACT DATA TYPES

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:  class Shape
2:  {
3:  public:
4:       Shape(){}
5:       virtual ~Shape(){}
6:       virtual long GetArea() = 0;
7:       virtual long GetPerim()= 0;
8:       virtual void Draw() = 0;
9:  private:
10: };

Output:
(1)Circle (2)Rectangle (3)Square (0)Quit:2
× × × × × ×
× × × × × ×
× × × × × ×
× × × × × ×
(1)Circle (2)Rectangle (3)Square (0)Quit:3
× × × × ×
× × × × ×
× × × × ×
× × × × ×
× × × × ×
(1)Circle (2)Rectangle (3)Square (0)Quit:0
Analysis: As you can see, the workings of the program are totally unaffected. The only difference is that it is now impossible to make an object of class Shape.
ABSTRACT DATA TYPES
You declare a class to be an abstract data type by including one or more pure virtual functions in the class declaration. Declare a pure virtual function by writing = 0 after the function declaration.
For example:
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
class Shape
{
   virtual void Draw() = 0;   // pure virtual
};

 
< previous page page_290 next page >

If you like this book, buy it!