Overloading in plain terms
Overloading means using one name for more than one piece of code. The version that runs depends on what you pass in, such as the arguments in a function call or the operands on either side of an operator. The name stays the same, and the inputs decide which version is used.
It’s one way to get polymorphism in C++, where the same name behaves differently depending on the situation.
The main types of overloading in C++
C++ has three common types of overloading.
Function overloading
Several functions share a name, and their parameter lists differ. A send function might take a message on its own or a message plus a retry count:
void send(const std::string& message);
void send(const std::string& message, int retries);
Member functions inside a class can be overloaded the same way. C doesn’t allow this for ordinary functions, so the question does c have function overloading gets a no for C and a yes for C++. The rules are covered in what is function overloading.
Operator overloading
Operator overloading in C++ lets operators such as +, ==, and << work with your own class types. An overloaded operator is written as a function named operator followed by the symbol:
struct Money {
long cents;
};
Money operator+(const Money& a, const Money& b) {
return Money{a.cents + b.cents};
}
With that function in place, total = price + tax works for Money values.
Constructor overloading
A class can have several constructors with different parameter lists, so objects can be created in more than one way:
class Point {
public:
Point() : x(0), y(0) {}
Point(int x_value, int y_value) : x(x_value), y(y_value) {}
private:
int x;
int y;
};
Here Point origin; uses the first constructor, and Point p{3, 4}; uses the second.
How the three types compare
| Type | What shares the name | How the right version is chosen | Typical use |
|---|---|---|---|
| Function overloading | Free functions or member functions | Number and types of arguments | The same action on different inputs |
| Operator overloading | Operator symbols such as + |
Types of the operands | Natural syntax for your own types |
| Constructor overloading | Constructors of one class | Arguments passed when the object is created | Creating objects in more than one way |
Which types you’ll use most
Function overloading and constructor overloading show up in many everyday programs. Operator overloading is less common. It fits best with types that represent values, such as money, vectors, or fractions. Overloading operators just to be clever tends to make code harder to read.
Overloading is not overriding
Overloading is resolved at compile time. Overriding is a different idea: a derived class replaces a virtual function from its base class, and the version that runs is chosen at runtime. Readers who ask what is function overriding with an example can compare the two side by side to see where they differ.
Key takeaways
- Overloading means one name with several versions, and the inputs decide which version runs.
- C++ has function overloading, operator overloading, and constructor overloading.
- Overloads are chosen at compile time, based on the types of the arguments or operands.
- Overriding is related but different, and it depends on
virtualfunctions.