< Previous | Contents | Next >

Introducing the Triple Program

The Triple program triples the value 5, and “gamer”. The program triples these values using a single function that’s been overloaded to work with an argument of two different types: int and string. Figure 5.6 shows a sample run of the program.

Overloading Functions 175


image

Figure 5.6

Function overloading allows you to triple the values of two different types using the same function name.


You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 5 folder; the filename is triple.cpp.


// Triple

// Demonstrates function overloading


#include <iostream> #include <string>

using namespace std; int triple(int number);

string triple(string text);


int main()

{

176 Chapter 5 n Functions: Mad Lib‌‌


cout << "Tripling 5: " << triple(5) << "\n\n"; cout << "Tripling ’gamer’: " << triple("gamer");


return 0;

}


int triple(int number)

{

return (number * 3);

}


string triple(string text)

{

return (text + text + text);

}