0% found this document useful (0 votes)
0 views

17. C++ OOP - Overloading(Function and Operator)

The document explains overloading in C++, which allows multiple definitions for a function name or operator in the same scope, known as function and operator overloading. It details how the compiler resolves which definition to use based on argument types and outlines the rules for function overloading, emphasizing that definitions must differ by argument types or counts. Additionally, it describes how to implement operator overloading using the 'operator' keyword and provides an example with string concatenation.

Uploaded by

vinnoval123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

17. C++ OOP - Overloading(Function and Operator)

The document explains overloading in C++, which allows multiple definitions for a function name or operator in the same scope, known as function and operator overloading. It details how the compiler resolves which definition to use based on argument types and outlines the rules for function overloading, emphasizing that definitions must differ by argument types or counts. Additionally, it describes how to implement operator overloading using the 'operator' keyword and provides an example with string concatenation.

Uploaded by

vinnoval123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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:

You might also like