KEMBAR78
Lecture Slides - Operator Overloading | PDF | Computing | Object Oriented Programming
0% found this document useful (0 votes)
7 views23 pages

Lecture Slides - Operator Overloading

OOP

Uploaded by

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

Lecture Slides - Operator Overloading

OOP

Uploaded by

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

CS231 Object Oriented Programming

Courtesy:
E Balagurusamy, Yashwant Kanetkar, and Programiz

Dr. Balu L. Parne


DoCSE, SVNIT Surat.
Operator Overloading in C++
Operator Overloading in C++:
• Operator overloading is one of the many exciting features of C++
language and it is an important technique that has enhanced the
power of extensibility of C++.
• We observed that C++ tries to make the user-defined data types
behave in much the same way as the built in types.
• C++ permits us to add two variables of user-defined types with the
same syntax that is applied to the basic types.
• This means that C++ has the ability to provide the operators with a
special meaning for a data type. The mechanism of giving such
special meaning to an operator is known as operator overloading.
Operator Overloading in C++:
• Operator overloading is a compile-time polymorphism.
• It is an idea of giving special meaning to an existing operator in C++
without changing its original meaning.
• In C++, we can make operators work for user-defined classes. This
means C++ has the ability to provide the operators with a special
meaning for a data type, this ability is known as operator overloading.
• For example, we can overload an operator ‘+’ in a class like String so
that we can concatenate two strings by just using +.
• Other example classes where arithmetic operators may be overloaded
are Complex Numbers, Fractional Numbers, Big integers, etc.
Operator Overloading in C++:
• Here, variables “a” and “b” are of
types “int” and “float”, which are
built-in data types.
• Hence the addition operator ‘+’ can
easily add the contents of “a” and
“b”.
• This is because the addition
operator “+” is predefined to add
variables of built-in data type only.
Operator Overloading in C++:
• In this example, we have 3 variables “a1”,
“a2” and “a3” of type “class A”.
• Here we are trying to add two objects “a1”
and “a2”, which are of user-defined type i.e.
of type “class A” using the “+” operator.
• This is not allowed, because the addition
operator “+” is predefined to operate only
on built-in data types.
• But here, “class A” is a user-defined type, so
the compiler generates an error. This is
where the concept of “Operator
overloading” comes in.
Operator Overloading in C++:
• Now, if the user wants to make the operator “+” add two class
objects, the user has to redefine the meaning of the “+” operator
such that it adds two class objects.
• This is done by using the concept of “Operator overloading”. So
the main idea behind “Operator overloading” is to use C++
operators with class variables or class objects.
• Redefining the meaning of operators really does not change their
original meaning; instead, they have been given additional
meaning along with their existing ones.
Syntax for C++ Operator Overloading:
• To define an additional task to an operator, we must specify what it
means in relation to the class to which the operator is applied.
• This is done with the help of a special keyword, called operator,
which describes the task. The general form of an operator function is:

• returnType is the return type of


the function.
• operator is a keyword.
• op is the operator we want to
overload. Like: +, <, -, ++, etc.
• arglist is the arguments passed to
the function.
Syntax for C++ Operator Overloading:
• To overload an operator, we use a special operator function. We
define the function inside the class or structure whose
objects/variables we want the overloaded operator to work with.

• returnType is the return type of


the function.
• operator is a keyword.
• symbol is the operator we want to
overload. Like: +, <, -, ++, etc.
• arguments is the arguments
passed to the function.
Syntax for C++ Operator Overloading:
• Operator functions must be either member functions or friend
functions.
• 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 object used to invoke the member function is passed
implicitly and therefore is available for the member function.
• Arguments may be passed either by value or by reference in case
of the friend function.
Syntax for C++ Operator Overloading:
• The process of overloading involves the following steps:
• Create a class that defines the data type that is to be used in the
overloading operation.
• Declare the operator function operator op() in the public part of the class.
It may be either a member function or a friend function.
• Define the operator function to implement the required operations.
• Overloaded operator function can be invoked by expression such as
op X or X op for unary operators.
• X op Y for binary operators.
• op X or X op would be interpreted as operator op (X) for friend
functions.
Syntax for C++ Operator Overloading:
• op X or X op would be interpreted as operator op (X) for
friend functions.
• Similarly, the expression X op Y would be interpreted as
either
X.operator op (Y) in case of member functions, or
operator op (X, Y) in case of friend functions.

• When both the forms are declared, standard argument


matching is applied to resolve any ambiguity.
• Programming Example to overload unary operator.
Overloading Unary Operator:
• When we use ++count1;, the void operator ++ () is called. This
increases the value attribute for the object count1 by 1.
• When we overload operators, we can use it to work in any way we
like. For example, we could have used ++ to increase value by 100.
• To make ++ work as a postfix we use this syntax.

• Notice the int inside the parentheses. It's the syntax used for using
unary operators as postfix; it's not a function parameter.
Overloading Binary Operators:
• As a rule, in overloading binary operators, the left hand
operand is used to invoke the operator function and the
right hand operand is passed as an argument.
• The Statement c3 = c1 + c2; invokes operator+() function.
• We know that a member function can be invoked only by an
object of the same class. Here, the object c1 takes the
responsibility of invoking the function and c2 plays the role
of an argument that is passed to the function.
• The above invocation statement is equivalent to c3 =
c1.operator+(c2)
Overloading Binary Operators:
Overloading Binary Operators:
• When we overload the binary operator for user-defined types by
using the code:

• The operator function is called using the obj1 object and obj2 is
passed as an argument to the function.
• using & makes our code efficient by referencing the complex2
object instead of making a duplicate object inside the operator
function.
• using const is considered a good practice because it prevents the
operator function from modifying complex2.
Operator Overloading:
• Two operators = and & are already overloaded by default in C++. For
example, to copy objects of the same class, we can directly use the =
operator. We do not need to create an operator function.
• Operator overloading cannot change the precedence and associativity
of operators. However, if we want to change the order of evaluation,
parentheses should be used.
• There are 4 operators that cannot be overloaded in C++. They are:
• :: (scope resolution)
• . (member selection)
• .* (member selection through pointer to function)
• ?: (ternary operator)
Role of Friend Function:
• C3 = C1+C2; is equivalent to C3 = operator+(C1, C2);
• In both the cases we will get the same results by the use of either a
friend function or a member function.
• Why then an alternative is made available?
• A = B+2; (or A = B*2; )
• A = 2+B; (or A = 2*B; ) This will not work….Why?
• Because, the left hand operand which is responsible for invoking the
member function should be an object of the same class.
• An object need not be used to invoke a friend function but can be passed
as an argument.
• We can use a friend function with a built in type data as the left hand
operand and an object as the right hand operand.
Manipulation of Strings Using Operators:
• ANSI C manipulates strings using character arrays, pointers and
string functions. There are no operators for manipulating the
strings.
• One of the main drawbacks of string manipulations in C is that
whenever a string is to be copied, the programmer must first
determine its length and allocate the required amount of memory.
• Although these limitations exist in the C++ as well, it permits us to
create our own definitions of operators that can be used to
manipulate the strings very much similar to the decimal numbers.
• Recently, ANSI C++ committee has added a new class called string
to the C++ class library that supports all kinds of string
manipulations.
Manipulation of Strings Using Operators:
• It is possible to use statements like
• string3 = string 1 + string 2;
• if (string1 >= string2) string = string1;
• Strings can be defined as class objects which can be then
manipulated like the built-in types.
• Since, the strings vary greatly in size, we use new operator to
allocate memory for each string and a pointer variable to point to
the string array.
• Thus we must create string objects that can hold these two pieces
of information, namely, length and location which are necessary
for string manipulations.
Rules for Overloading Operators:
• There are certain restrictions and limitations in overloading the
operators as given below:
• 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.
• The overloaded operator follows the syntax rules of the original operators.
They cannot be overridden.
• There are some operators like membership operator, pointer to member
operator, scope resolution operator and conditional operator that cannot be
overloaded.
• We cannot use friend functions to overload certain operators like Assignment
operator (=), Function call operator, Subscripting operator, and class member
access operator (->).
Rules for Overloading Operators:
• There are certain restrictions and limitations in overloading the
operators as given below:
• Unary operators, overloaded by means of a member function, take no explicit
arguments and return no explicit values, but those overloaded by means of a
friend function, take one reference argument (the object of the relevant
class).
• 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.
• By operator overloading, we cannot change the precedence, associativity and
number of arguments of an operator.
Thank You.

You might also like