Overloading Functions

A powerful feature of C++ is the ability to use the same name for several functions. This sounds like a confusing thing to do, but when people draw water and draw a card the meaning of 'draw' is usually obvious from the context. In the same way, C++ uses the argument types to distinguish between the various overloaded functions.

sqr()

The sqr(x) function squares its argument. This operation makes sense for any argument that you can multiply by itself (for instance, both floating-point and integer numbers). Bear in mind that floating-point and integer arithmetic are very different on a machine level, and using a double sqr(double) function to do integer squaring can be very inefficient. So you need to define two functions, ...

Get C++ By Example: UnderC Learning 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.