Arithmetic operators are typically used for arithmetic operations. Is it not nice to have the +, join two strings? Enabling that is said to be overloading the arithmetic addition operator, for strings.
The increment operator, ++ adds 1 to an int or a float. When dealing with pointers, it does not add 1 to the pointer. It makes the pointer point to the next consecutive object in memory. An iterator points to the next object in a linked-list, but the linked-list objects are in different places in memory (not in consecutive regions). Would it not be nice to overload the increment operator for an iterator, to increment but point to the following element, in the linked-list?
This article explains overloading in C++. It is divided into two parts: function overloading and operator overloading. Having already basic knowledge in C++ is necessary to understand the rest of the article.
Article Content
Function Overloading
The following function adds two ints and returns an int:
{
int sum = no1 + no2;
return sum;
}
The prototype of this function is:
int add(int no1, int no2);
The prototype of a function in the header of the function, ending with a semicolon. The following function with the same name, but with a different prototype, would add three floats and return a float:
float add(float no1, float no2, float no3)
{
float sum = no1 + no2 + no3;
return sum;
}
How does the compiler differentiate which function to call, since two or more functions have the same name? The compiler uses the number of arguments and argument types to determine which function to call. The parameter list of overloaded functions should differ in their number and/or parameter types. So, the function call,
would call the integer function, while the function call,
float sme = add(2.3, 3.4, 2.0);
would call the float function. Note: there are situations where the compiler will reject an overloaded function when the number of arguments is the same but of different types! – Reason: – see later.
The following program puts the above code segments into action:
using namespace std;
int add(int no1, int no2)
{
int sum = no1 + no2;
return sum;
}
float add(float no1, float no2, float no3)
{
float sum = no1 + no2 + no3;
return sum;
}
int main()
{
int sm = add(2, 3);
cout<<sm<<‘n‘;
float sme = add(2.3, 3.4, 2.0);
cout<<sme<<‘n‘;
return 0;
}
The output is:
5
7.7
Operator Overloading
Arithmetic operators are used to overload operations in class types. An iterator is a class type. The increment and decrement operators are used to overload operations for an iterator.
Example String Class Operator Overloading
This section provides an example, where + is overloaded for a simply designed string class, called a spring class. + concatenates the literals of two string objects, returning a new object with the concatenated literals. Concatenating two literals means joining the second literal to the end of the first literal.
Now, C++ has a special member function for all classes, called the operator. The programmer can use this special function to overload operators, such as +. The following program shows the overloading of the + operator for two strings.
using namespace std;
class spring
{
public:
//data members
char val[100];
int n;
char concat[100];
//member functions
spring (char arr[])
{
for (int i=0; i<100; ++i) {
val[i] = arr[i];
if (arr[i] == ‘