C++ Overloading
The return page ended with forty-five lines on the screen, and the last one was 4, the value returned from double_count. This page starts from that screen, in the same file types.cpp, and the first box still holds the word pine. The announce and show_label functions stay above main. This page continues the Functions module. The new work replaces the single double_count name with two functions that share one name, scale. Each version has a different parameter list. The calls print 4, then 6. The screen grows to forty-six lines, and the last one is 6.
Overloading means one function name can hold more than one definition, as long as the parameter lists are not the same. The compiler looks at the call and picks the version whose parameters match the arguments. You do not invent a second name when the work is the same idea with a different number of inputs.
Programs overload names so the reader sees one verb for related work. Scaling by a fixed factor and scaling by a chosen factor are both scale. The parameter list tells them apart. This page keeps both versions returning int, so the teaching point is the parameter list, not a new return style. The older double_count name goes away in the checked file, because the one-argument scale already covers that doubling job with a name that can grow a second shape.
Above main, after show_label, sit two definitions. The first is int scale(int n) with the body return n * 2;. The second is int scale(int n, int factor) with the body return n * factor;. Same name. Different parentheses. The first version takes one int. The second version takes two.
Inside main, after show_label("tag");, the file stores and prints the one-argument call: int scaled = scale(count); then std::cout << scaled << std::endl;. Because count holds 2, that version returns 4. Next comes the two-argument call printed directly: std::cout << scale(count, 3) << std::endl;. That version returns 2 times 3, which is 6.
Walk the new ending. announce(); prints call. show_label("tag"); prints tag. scale(count) returns 4 and the print shows 4. scale(count, 3) returns 6 and the print shows 6. The first forty-four lines still match the path through tag. The last two lines are 4, then 6. There are forty-six lines in all. Reading the call is enough to know which body runs: one argument picks the first scale, two arguments pick the second.
The parameter lists must differ. Two functions named scale that both take only one int cannot sit in the same file. Changing only the parameter name, such as int scale(int x) next to int scale(int n), does not count as a new list. The types and the count of parameters are what the compiler compares. The return type alone is not enough to overload on this page’s rule of thumb: keep the difference in the parentheses.
A common mistake is to call scale(count, 3) when only the one-argument version exists, so the build fails. Another common mistake is to expect scale(count) to use the factor version somehow; with one argument, only the one-parameter scale runs. The empty announce() and the string show_label stay as earlier tools; they are not overloads of scale.
This page stops before the next tool that explains where a name is visible. The next page names that tool once at the end. Here both scale versions are visible to main because they sit above it in the same file.
Type the file in this order, or add only the new lines if types.cpp is already on disk. Above int main(), keep announce and show_label, remove double_count if it is still there, and add both scale definitions. Near the end of main, after show_label("tag"); and before return 0;, store int scaled = scale(count);, print scaled, then print scale(count, 3). The file name on disk stays types.cpp. The checked file is below, and the new lines are the two scale definitions and the two uses in main.
#include <iostream>
#include <string>
void announce()
{
std::cout << "call" << std::endl;
}
void show_label(std::string label)
{
std::cout << label << std::endl;
}
int scale(int n)
{
return n * 2;
}
int scale(int n, int factor)
{
return n * factor;
}
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
double price = 7.9;
const int limit = 10;
int scores[3] = {4, 5, 6};
std::string other = "cone";
std::string joined = word + " " + other;
int total = 17;
int step = 3;
step += 4;
int score = 72;
int goal = 50;
bool wet = false;
std::string result;
int day = 3;
std::string name;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
std::cout << word.size() << std::endl;
std::cout << joined << std::endl;
std::cout << word[0] << std::endl;
std::cout << joined.find("cone") << std::endl;
std::cout << joined.substr(5, 4) << std::endl;
std::cout << count * 4 << std::endl;
std::cout << 7 / 2 << std::endl;
std::cout << 7.0 / 2 << std::endl;
std::cout << (count > 1) << std::endl;
std::cout << (count == 3) << std::endl;
std::cout << !ready << std::endl;
std::cout << static_cast<int>(price) << std::endl;
std::cout << static_cast<double>(count) / 4 << std::endl;
std::cout << static_cast<int>(ratio * 3) << std::endl;
std::cout << limit << std::endl;
std::cout << limit * count << std::endl;
std::cout << scores[0] << std::endl;
std::cout << scores[2] << std::endl;
std::cout << total % 5 << std::endl;
std::cout << step << std::endl;
std::cout << (score >= goal) << std::endl;
std::cout << (score < goal) << std::endl;
std::cout << ((score >= goal) && (count == 2)) << std::endl;
std::cout << ((score < goal) || wet) << std::endl;
if (score >= goal) {
result = "pass";
} else {
result = "retry";
}
std::cout << result << std::endl;
if (wet) {
std::cout << "wet" << std::endl;
}
switch (day) {
case 1:
name = "mon";
break;
case 2:
name = "tue";
break;
case 3:
name = "wed";
break;
default:
name = "other";
break;
}
std::cout << name << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << i << std::endl;
}
int n = 3;
while (n > 0) {
std::cout << n << std::endl;
n--;
}
for (int k = 0; k < 5; k++) {
if (k == 2) {
continue;
}
if (k == 4) {
break;
}
std::cout << k << std::endl;
}
for (int value : scores) {
std::cout << value << std::endl;
}
announce();
show_label("tag");
int scaled = scale(count);
std::cout << scaled << std::endl;
std::cout << scale(count, 3) << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types, then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows forty-six lines. The lines through tag match the earlier path. The last two lines are 4, then 6, the results of the two scale calls.
Check the last two lines first. scale(count) used the one-parameter version and printed 4. scale(count, 3) used the two-parameter version and printed 6. The table gives each row its piece, so compare your run with it line by line. If only one new number appears, one of the calls is missing. If the build fails about ambiguous or missing scale, the parameter lists do not match the calls. If double_count is still in the file and scale is missing, replace that old name with the two scale definitions.
📊 The scale overloads and what they show
| Piece | What the screen shows |
|---|---|
| First | call, because announce still runs first among the function calls |
| Last | 6, because scale(count, 3) returns 2 * 3 and that print is last |
| Rule | one name can hold two functions when the parameter lists differ |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0, 7, 0.5, 1, 10, 20, 4, 6, 2, 7, 1, 0, 1, 0, pass, wed, 0, 1, 2, 3, 2, 1, 0, 1, 3, 4, 5, 6, call, tag, 4, 6 |
Try one change at a time. Change scale(count, 3) to scale(count, 4), leave both definitions as they are, and rebuild. The last line becomes 8, because 2 times 4 is 8. Put 3 back. Next, change the one-parameter body from return n * 2; to return n * 5;, leave the calls as they are, and rebuild. The line that was 4 becomes 10, and the last line stays 6, because only the one-argument version changed. Put n * 2 back. Each experiment changes one thing, so the screen shows exactly which overload caused the difference. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the overloading tool and stops before the next one. The next page is scope, which explains where a name is visible inside a function or inside main. The two scale versions you have just written are the right shape when one verb needs more than one parameter list. You leave with forty-six lines on the screen, and the last one is 6. Count the lines before you leave. There are forty-six, and the last one is 6.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top