Home > Articles > Programming > C/C++

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Conclusion

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!

  • + Share This
  • 🔖 Save To Your Account