C++ PROGRAMMING
OOP – OVERLOADING (FUNCTION AND OPERATOR)
WHAT IS OVERLOADING ?
• C++ allows you to specify more than one definition for
a function name or an operator in the same scope, which is
called function overloading and operator
overloading respectively.
• An overloaded declaration is a declaration that had 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 (implementation).
HOW OVERLOADING WORKS?
• 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.
TYPES OF OVERLOADING IN C++
• Function overloading
• Operators overloading
FUNCTION OVERLOADING
• You can have multiple definitions for the same function
name in the same scope.
• The definition of the function must differ from each
other by the types and/or the number of arguments
in the argument list.
• You can not overload function declarations that differ
only by return type.
EXAMPLE#1
OUTP
UT:
OPERATOR OVERLOADING
• 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.
• Standard libraries offers some classes that performs
operator for example the String class.
HOW OPERATOR WORKS IN
OVERLOADING
For example the String class library. You can concatenate
two string objects by using operator “+”
OUTP
UT:
HOW TO DO OPERATOR OVERLOADING
Class-name operator+(const class-name& object-name);
Operator (+, - ,++,
Keyword for etc)
Operator
• This is how to declare the operator overloading
function of class.
HOW TO DO OPERATOR OVERLOADING
Take note of this
operators. Some
operators can be used
as unary, arithmetic,
relational, etc.
EXAMPLE
OUTP
UT: