Use function overloading to reuse functions
Function overloading is an important concept in C++. Sometimes, we want to use the same function name but have different functions to work on different data types or a different number of types. This is useful as the client can choose the correct function based on its needs. C++ allows us to do this by using function overloading.
Getting ready
For this recipe, you will need a Windows machine with a working copy of Visual Studio.
How to do it…
In this recipe, we will learn how to overload a function:
- Open Visual Studio.
- Create a new C++ project.
- Select a Win32 Console Application.
- Add source files called
main.cpp
,Cspeed.h
, andCspeed.cpp
. - Add the following lines of code to
main.cpp
:#include <iostream> #include <conio.h> #include "CSpeed.h" using namespace std; //This is not overloading as the function differs only //in return type /*int Add(float x, float y) { return x + y; }*/ int main() { CSpeed speed; cout<...