Home >>C++ Tutorial >C++ method Overloading
If the members that are two or more than two in numbers possesses the very same name but are different in terms of type of parameters or different in number is generally called as overloading in C++.
These following members that are displayed below can be overloaded in C++:
The reason that these members can only be overloaded is that they only have the parameters in them.
Here are the types of the overloading in C++:
The procedure that has two or more than two functions with the same name and is different in parameters is generally known as the function overloading in C++. The functions gets redefined in the function overloading generally by two ways, either by using different types of arguments or a different number of arguments. These are the differences that generally help the compiler in differentiating between various functions.
There is a main advantage of the function overloading and that is, enhanced readability of the program that gives freedom to the programmers from using different names for the same action.
Here are a couple of the example of the function overloading in C++ that will help you understand the subject more clearly:
#include <iostream> using namespace std; class Calculator { public: static int sum(int x,int y) { return x + y; } static int sum(int x, int y, int z) { return x + y + z; } }; int main(void) { Calculator obj; cout<<"Sum of two number="<<obj.sum(10, 11)<<endl; cout<<"Sum of three number="<<obj.sum(10, 11, 12); return 0; }
A compile-time polymorphism that consists of the overloaded operators, in order to deliver the special meaning to the user-defined data type. This method is generally used to overload or redefine the most of the operators that are available in the C++ programming language. In order to perform the operation on the user-defined data type this method is used. Just for instance, C++ provides the ability to add the variables of the user-defined data type that are basically applied to the data types that are built-in.
Different operations can be performed on the same operand by the operators overloading and it is known as the main advantage of it.
Here are the operators that cannot be overloaded are mentioned below:
Here is the syntax for the operator overloading:
return_type class_name : : operator op(argument_list) { // body of the function. }
Here are the example of the operator overloading in the C++ that will explain the concept better and help you understand the core of the method:
#includeusing namespace std; class Demo { private: int number; public: Demo(): number(10) { } void operator ++() { number = number+5; } void Print() { cout<<"The Numebr value is = "< Output :The Numebr value is = 15