Abstract Data Types

Often, you will create a hierarchy of classes together. For example, you might create a Shape class as a base class to derive a Rectangle and a Circle. From Rectangle, you might derive Square as a special case of Rectangle.

Each of the derived classes will override the Draw() method, the GetArea() method, and so forth. Listing 18.3 illustrates a bare-bones implementation of the Shape class and its derived Circle and Rectangle classes.

Listing 18.3. Shape Classes
 0: //Listing 18.3. Shape classes. 1: #include <iostream> 2: 3: class Shape 4: { 5: public: 6: Shape(){} 7: virtual ~Shape(){} 8: virtual long GetArea() { return -1; } // error 9: virtual long GetPerim() { return -1; } 10: virtual void Draw() {} 11: }; 12: 13: class ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.