Strengths and Weaknesses of Templates

Picking the data type or class that will be at the core of your program or class can be difficult.

For instance, the calculator used ints until it was revealed that there were arithmetic problems as a result of that choice. You might recall from Lesson 4, “Numeric Input,” that changing the calculator to use floats instead of ints forced modifications affecting almost every part of the code.

Changing the object-oriented calculator to use long double instead of float for greater accuracy would be no different: It would require changes in every class. And if you wanted to have an int calculator, a float calculator, and a long double calculator all at the same time, you might have to reimplement the classes used ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND 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.