Lesson 2 of 45 · C++
Lesson 10: Function Overloading
Duration: 8 min
Lesson 2 of 45 · C++
Duration: 8 min
cpp\nvoid print(int i) {\n std::cout << \"Integer: \" << i << '\\n';\n}\n\nvoid print(double d) {\n std::cout << \"Double: \" << d << '\\n';\n}\n\nvoid print(const std::string& s) {\n std::cout << \"String: \" << s << '\\n';\n}\n\nint main() {\n print(42); // calls int overload\n print(3.14); // calls double overload\n print(std::string(\"hi\")); // calls string overload\n return 0;\n}\n\n\nPitfall: If you add void print(long l); and call print(42);, the compiler may be ambiguous because both int and long are viable.\n\n> Tip: Use the explicit keyword on single‑argument constructors to avoid unintended implicit conversions.\n\n---\n\n<Alert type="warning">You cannot overload on return type alone; the return type is not considered in overload resolution.