3. INTRODUCTION:
• An overloaded declaration is a declaration that has been
declared with the same name as a previously declared
declaration in the same scope, except that both declarations
have different arguments and obviously different definition
i.e. implementation.
• When you call an overloaded function or operator, the
compiler determines the most appropriate definition to use
by comparing the argument types you used to call the
function or operator with the parameter types specified in the
definitions. The process of selecting the most appropriate
overloaded function or operator is called overload resolution.
4. • Each C++ operator has a predefined meaning. Operator
Overloading is a type of polymorphism in which an operator is
overloaded to give user defined meaning to it or say to give an
additional meaning to it.
• For example, + operator can be overloaded to perform an
operation of string concatenation along with its pre-defined
job of adding two numeric values.
• In simple words, overloading an operator means assigning
additional operation or job to it; relative to a specific class.
• When an operator is overloaded, none of its original meaning
will be lost.
5. SYNTAX:
• Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator
being defined. Like any other function, an overloaded
operator has a return type and a parameter list.
6. CONSIDER FOLLOWING PIECE OF CODE. CONSIDERING THE +
OPERATOR IS OVERLOADED IN SUCH A WAY WITH CLASS STRING
THAT IT WILL CONCATENATE TWO STRING OBJECTS :
int main()
{
int n1 = 11
2, n2 = 23
4, n3;
String s1 = “Hello”, s2=“World”, s3;
n3 = n1 + n2; // addition
s3 = s1 + s2; // concatenation
}
7. RULES FOR OVERLOADING OPERATORS:
• Only existing operators can be overloaded.New operators cannot be
created.
• The overloaded operator must have at least one operand that is of user-
defined type.
• We cannot change the basic meaning of an operator.
• You cannot change any operators precedence.
• Overloaded operators follow the syntax rules of the original operators.
• Binary operators overloaded through a member function take one explicit
argument and those which are overloaded through a friend function take
two explicit arguments.
• When using binary operators overloaded through a member function, the
left hand operand must be an object of the relevant class.
• Binary arithmetic operators such as +, -, * and / must explicitly return a
value. They must not attempt to change their own arguments.
8. TYPES OF OPERATORS:
• Unary (++, --)
• Binary (+, -)
• Ternary (?:)
You are free to overload most of the built-in operators, but you cannot
create new operators of your own.Therefore, although you can give your
class an increment operator (++), you cannot create a squared
operator.You can overload these operators to do anything you want, but it
is a good programming practice for them to make sense.That means you
can have the ++ operator to decrement, but it would make no sense to do
so.
9. OPERATOR FUNCTIONS:
• Operators are overloaded by creating operator functions.
• An operator function defines the operations that the overloaded operator
will perform relative to the class upon which it will work.
• An operator function is created using the keyword operator.
• Operator functions can be either
I. Member Functions of a class
II. Friend Functions of a class
• The way operator functions are written differs between member and non-
member functions.
• You can overload most of Unary and Binary Operators, but you cannot
overload the ternary operator (?:).Binary as well as Unary operators can be
overloaded by both approaches namely Member Functions Approach and
Friend Function Approach.
10. A MEMBER OPERATOR FUNCTION FOR BINARY OPERATORS:
• RetType ClassName::operator#(arg-list)
{
// operations
}
• For example, if you are overloading the + operator, use
operator+.
• While overloading binary operators using member function,
thearg-list will contain one parameter.
11. A MEMBER OPERATOR FUNCTION FOR UNARY OPERATORS:
RetType ClassName::operator#()
{
// operations
}
• While overloading an unary operator, arg-list will be empty.
12. A FRIEND OPERATOR FUNCTION FOR BINARY OPERATORS:
RetType operator#(arg-list)
{
// operations
}
• While overloading a binary operator using friend function;
argument list must take 2 arguments, one of them must be
of userdefined type.
13. A FRIEND OPERATOR FUNCTION FOR UNARY OPERATORS:
RetType operator#(arg-list)
{
// operations
}
• While overloading an unary operator using friend function;
argument list must have 1 argument as reference to the
object.
14. DIFFERENCE BETWEEN MEMBER FUNCTION AND FRIEND
FUNCTION:
• A basic difference between them is that a friend function will
have only one argument for unary operators and two for
binary operators,while a member function has no arguments
for unary operators and only one for binary operators.
• The friend function takes one argument more than member
function because the invoking appears as an explicit
parameter to the friend function whereas in member
functions it is passed as an implicit parameter.