Introduction to overloading operator (++) and (--).
Operator Overloading:
We know that operator is a symbol that performs some operations on one or more operands. The ability to overload operators is one of the C++’s most powerful features++ allows us to provide new definitions to some built-in operators. We can give several meaning to an operator, depending upon the types of arguments used. This capability to overload an operator is called operator overloading.
Operators are overloaded by creating operator functions.an operator function consists of a function definition, similar to a normal function except that the function name now becomes the keyword OPERATOR followed by the symbol being overloaded. The general form or syntax of an operator function is
Return type class name:: operator <operator symbol>(argument list)
{
//Body of the function
}
Where return type is the data type of the value return after the specific operation, operator symbol is the operator being overloaded, that is preceded by the keyword operator, and Operator function is usually a member function or a friend function.
An operator that deals with only one operand is called as a unary operator. Some of the commonly used unary operators are ++,--,!,~ and unary minus.
Overloading increment and decrement operators: The increment operator (++) and the decrement operator ( -- ) are unary operators since they act on only one operand. Both the increment and decrement operators have two forms. They are the prefix(i.e do the operation and then use the content of the variable.) and postfix (i.e use the content of the variable and then do the
operation)notation. The general syntax form or the syntax of the prefix and postfix operator functions are
//prefix increment
return type operator++()
{
// Body of the prefix operator
}
//postfix increment
Return operator++(int x)
{
//body of the postfix operator
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: