Method Overloading - Kamills-12/2143-OOP GitHub Wiki
Method overloading is when you use the same function name, but change up the parameters: type, number, or order. C++ figures out which one to call based on the input.
It’s super useful when you want a function to handle different types of data, without creating a whole new function name every time.
#include <iostream>
using namespace std;
class Logger {
public:
void log(string message) {
cout << "[LOG]: " << message << endl;
}
void log(int code) {
cout << "[ERROR CODE]: " << code << endl;
}
void log(string message, int code) {
cout << "[LOG]: " << message << " (Code: " << code << ")" << endl;
}
};
int main() {
Logger logger;
logger.log("System OK"); // calls first one
logger.log(404); // calls second one
logger.log("Not Found", 404); // calls third one
return 0;
}