C++ Tutorial #14 – Overloading Functions & Function Templates




Overloading functions are functions that have the same name, but have different parameters. You can have different variable types in each function, or you can set multiple parameters. Overloading functions can be really cool, they can be useful for when you’re making a game. Let’s say you’re making a function places a button on the screen. You would probably make parameters like int positionXint positionY, int width, int height. These parameters would be required, and you can set optional parameters like string Text, if you want the button to have text; string FontName, if you want a specific font; or int R, int G, int B, if you want options to change color; or string imgSrc if you want to load an image file for your button.


Lets begin coding:




Your program should look like this:






This section of code below is our overloading function. It uses different variable types (int & double), and it has multiple parameters.





Inside of our main function we created three variables for each type (int & double), and set a value to each of them. We used cout to print each value of each function.





That’s pretty much it to overloading functions. Now let’s move on to Function Templates.






Function Templates work like overloading functions, they follow the same syntax, but they’re written a little differently.


Here’s the syntax for declaring a Function Template:



template <template-parameter> function-declaration



Here’s an example of a declared function template:





<class varType> is our template parameter. Unlike a regular function, function template parameters are enclosed in angle-brackets <> instead of parenthesis (). After the template parameter, we declare our function. We use the variable name (varType) inside of our template parameter in place of the variable types we normally use in a function (ex. int, double, string, void).





To use this function we simply just write the name of the function, then write the variable type we want to use inside the angled brackets. You can use any variable type as long as the compiler doesn’t throw out an error. Obviously you can’t do stuff like multiply a string by an int. After you specify the variable type, you use the parameters in your function. The above examples will return 7, 6.5, and “John Smith “, but of course you won’t see it without using cout, so let’s write a program.



Begin coding:





Your program should look like this:



Willie Nelson



This section of code below is our Function Template Declaration with T for our template parameter. We placed our template parameter T for our function type. We named our function combine, and placed T x, and T y for our function parameters. Then we placed our function argument return x + y.





We declared two variables for each variable type (int, double, & string), and set a value to each of them. Then we used cout and used our function template. We specified the variable type in the template parameter and and placed our variables we declared inside of our function parameters.





On our next tutorial, we will continue using function templates, now that you know the basics of it. Don’t worry, it’s not to hard, there’s just a little more I need to cover on that subject. Until then, keep practicing!


About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *