Hello friends, in this post today, we learn about what is operator overloading in c++. so lets start...
Operator overloading |
Operator overloading:-
Operator overloading is compile time polymorphism.
Operator overloading refers to overloading of one operator for many different purpose.
It is one of the powerful and fascinating features of the C++ which give additional meaning to built-in standard operators like +, -, *, /, >, <, < =, > = etc.
For example, the binary + can be used to add two integer numbers, two float numbers, two structures variables, two union variables or two class objects.
• The operator overloading provides mechanism to perform operations on user defined data type.
• When an operator is overloaded with multiple jobs, it is known as operator overloading.
• We can give special meaning to any operators in which program it is implemented.
Rules for operator overloading:-
1. Only existing operator can be overloaded.
2. Any overloaded operator function must have at least one operand which is user defined type. All of the operands cannot be of basic types. If this is the case than function must be friend function of some class.
3. We cannot change the basic meaning and syntax of an operator.
4. We cannot use friend function to overload certain operators. However member function can be used to overload them.
5. Unary operators, overloaded by means of a member function, take no explicit argument s and return no explicit value, but, those overloaded by means of a friend function, take one reference argument.
6. Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
7. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.
8. We cannot overload following operators.
Operators Name
Dot(.) and Pointer to member operator(.* )
:: Scope Resolution Operator
sizeof() The Size of Operator
?: Conditional Operator
Syntax:-
Example for Unary Operator Overloading
Output:
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with something new information .
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment