- The Takeaway
- Document Management
- The Base Class: AnyOldDocument
- Economy in Coding
- The First Document Type: A Memo
- The Second Document Typean Invoice
- The Third and Last Document Typea Contract
- Leveraging Base Class Generality
- Complete Program Output
- Make Your Destructors Virtual
- Additional Reading
Less code means less expense in maintenance and upgrades. I often wonder how much code is needlessly duplicated nowadays. As we’ve seen, the inheritance and polymorphism features supported by C++ can be leveraged to great advantage to produce very functional and yet quite generic base classes. These base classes then serve many of the needs of the derived classes.
When you’re defining base classes, ask yourself this question: "Have I made this base class as generic as possible?" Before adding code to derived classes, see whether you can add it to the base class in some generalized form. It’s entirely possible that you might be simply duplicating code unnecessarily by placing it in the derived classes. The same or similar placed in derived classes has to be maintained throughout the lifecycle. If it resides in the base class, only one version of the code has to be maintained.
Polymorphism and virtual functions are inextricably interwoven. In many ways, polymorphism is close to magic! However, it does come with a price tag in terms of performance and very possibly ease of understanding. As I always say, if it facilitates good clean code and clearly expresses your algorithmic requirements, then go for it!