C++ Operator Overloading
overloading in function refers to the use of the same thing for different purpose. C++ language also permits overloading of functions. This means that we can use the same functions names to create functions that performs a variety of different tasks. This is known as function polymorphism in Object oriented programming.
using the concept of function overloading, we can design a family of function of functions with one function name but with different argument (parameters) list. The function would perform different operations depending on the argument list. The function would perform different operations depending on the argument(parameters) list in the function call. The correct function to be invoked is determined by checking the number of arguments and type of the arguments but not on the function type.
for example an overloaded add() function handles different types of data as shown below
// declarations
add( a, b);
add( a, b,c);
add( x, y)
add( p, q);
add( p, q);
// FUNCTION calls
std::cout<<add(7,10);
std::cout<<add(67,34,23);
std::cout<<add(34.5,23.6);
std::cout<<add(12,34.7);
std::cout<<add(34.7,12);
|
C++ Operators Overloading
Overloading means assigning different meanings to an operation, depending on context. C++ language permits overloading of operators, thus allowing us to assign multiple meanings to operators. Actually we have used the concept of overloading in c language also.
example the operator *(Astrik) whenever applied to a pointer variable, gives the value pointed to by the pointer. but this is also commonly used for multiplying two numbers. so the number and type of operands decide the nature of operation to follow
The input/output operators << and >> are good examples of operators overloading, although the build-in definition of the << operator is for shifting of bits, it is also used for displaying the value or displaying the string. This has been made possible by the header file iostream where a number of overloading definitions for << are included. Thus the statement
invokes the definition for displaying a double type-value and
cout << “welcome”;
invokes the definition for displaying a char value. However , none of these definations in iostream affect the built in meaning of the operator.
similarly, so we can say define additional meanings to others c++ operators. for example, we can define + operator to add two structures or objects. Almost all C++ operators can be overloaded with a few exceptions such as the member access operators(. and .*), conditional operators (?:), scope resolution operator(::) and the size operator(sizeof).
Definitions for operator overloading are discussed
Operator that cannot be overloaded are as follows:
Scope operator (::)
Sizeof
member selector(.)
member pointer selector(*)
ternary operator(?:)
Syntax of Operator Overloading
return_type class_name : : operator op(argument_list)
{
// body of the function.
}
|
Where the return type is the type of value returned by the function.
class_name is the name of the class.
operator op is an operator function where op is the operator being overloaded, and the operator is the keyword.
Rules for Operator Overloading
Existing operators can only be overloaded, but the new operators cannot be overloaded.
The overloaded operator contains atleast one operand of the user-defined data type.
We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.
C++ Operators Overloading Example
// program to overload the unary operator ++.
#include < iostream >
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<“The Count is: “<
}
};
int main()
{
Test tt;
++tt; // calling of a function “void operator ++()”
tt.Print();
return 0;
}
|