C++ allows us to overload methods, constructor and indexed properties. The function or operator define with the same name but with different types of parameters s called overloading. C++ has two types of overloading and they are:
- Function overloading
- Operator overloading
C++ Function overloading
Function overloading is a feature of object-oriented programming where multiple functions having the same name differs by their parameters, then these functions are said to be overloaded. When we call those functions, we need to make sure to enter the proper number of arguments along with the proper data type.
Example: Below are the sample of functions with same name but different arguments.
1 2 3 4 5 | // same name different arguments int func() { } int func(int n) { } float func(double d) { } int func(int n, double d) { } |
Error example: Now see the example below, the functions defined below have the same name, same number type, and the same number of arguments. In such cases, the compiler will through an error.
1 2 3 | // Compiler throws error int func(float num1) { } double func(float num2){ } |
The advantage of function overloading is that we do not need to write different names for the functions that perform the same task, increasing the readability of the program. Polymorphism is achieved by function overloading.
Let us go through an example in C++ for function overloading.
C++ program for function overloading with different types and different number of parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // C++ program for function overloading #include <iostream> using namespace std; class OverloadProgram { public: int total(int num1, int num2) { return num1 + num2; } int total(int num1, int num2, float num3) { return num1 + num2 + num3; } }; int main() { OverloadProgram obj; //calling first display function cout << "Result 1: " << obj.total(50, 40) << endl; //calling second display function cout << "Result 2: " << obj.total(7, 15, 5) << endl; return 0; } |
Output:
1 2 | Result 1: 90 Result 2: 27 |
C++ Operator Overloading
Operator overloading means performing different operations with the same operator. We can overload most of the built-in operators that are available in C++. We can also change the user-defined type as well such as objects and structures.
The keyword ‘operator‘ is used to overload an operator. Compile-time polymorphism is achieved through operator overloading. It has a return type and parameter list.
Example: Consider an operator ‘+‘, it is used to add the two numbers but it is also used with string to concatenate two strings. So the ‘+‘ operator when placed between number operands, addition operation is done but when placed between string operands, it concatenates them.
Syntax: syntax for operator overloading in C++.
1 2 3 4 5 6 7 8 | class class_name { ... .. ... public return_type operator symbol (arguments_list) { //code } }; |
In the above syntax,
- return_type is the type that is returned by the function
- symbol refers to the operator that is to be overloaded such as
+, <, -, >,
etc.
However there are few operator that cannot be overloaded and they are:
- sizeof operator
- Scope operator (::)
- ternary operator(?:)
- member pointer selector(*)
Let us go through an example in C++ for operator overloading.
C++ Program for operator overloading
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // Overload ++ program #include <iostream> using namespace std; class TestOperator { private: int number; public: // Constructor TestOperator(): number(9) {} // ++ overloaded void operator++() { ++number; // pre increment by 1 } void display() { cout << "The Count: " << number << endl; } }; int main() { TestOperator op; // Calling the void operator++ ()function ++op; //calling display function op.display(); return 0; } |
Output:
1 | The Count: 10 |
Another example for operator overloading in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // C++ program for operator overloading #include <iostream> using namespace std; class Message { string str; public_colon Message(string i) { str = i; } void operator+(Message); void display(); }; void Message::operator+(Message a) { string msg = str + a.str; cout << "The result: " << msg; } int main() { Message m1("Hi, Welcome "); Message m2("Home"); m1 + m2; return 0; } |
Output:
1 2 | //Output: The result: Hi, Welcome Home |
The advantage of operator overloading is that we can perform different operations on the same operand.