0% found this document useful (0 votes)
4 views27 pages

11. Operator Overloading (1)

The document provides an overview of operator overloading in C++, detailing its significance, types, and implementation methods. It explains the difference between function overloading and operator overloading, outlines the operators that can and cannot be overloaded, and discusses the restrictions on operator overloading. Additionally, it covers the use of member and friend functions for implementing operator overloading, along with examples of unary and binary operator overloading.

Uploaded by

f2024065470
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)
4 views27 pages

11. Operator Overloading (1)

The document provides an overview of operator overloading in C++, detailing its significance, types, and implementation methods. It explains the difference between function overloading and operator overloading, outlines the operators that can and cannot be overloaded, and discusses the restrictions on operator overloading. Additionally, it covers the use of member and friend functions for implementing operator overloading, along with examples of unary and binary operator overloading.

Uploaded by

f2024065470
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/ 27

Object Oriented Programming

Instructor: Shinawar Naeem


Department of Software Engineering
Semester: Spring 2025
Course Code: CC-1022
OPERATOR
OVERLOADING
Introduction to Operator
Overloading
• Operator overloading is one of the best features of C++. By overloading
the operators, we can give additional meaning to the operators like
+-*/=.,= etc., which by default are supposed to work only on standard
data types like int, float, char, void etc. It is an essential concept in C++.
It’s a type of polymorphism in which an operator is overloaded to give it
the user-defined meaning.
• C++ allows us 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. The process of selecting the most
suitable overloaded function or operator is called overload resolution.
• So now let’s understand what is operator overloading in c++.
Continue…
What is operator overloading in C++?
Operator overloading in c++ is defined as one of the best features that
is used to overload most of the operators like “+” “–” “*” “/” “=” “.” “,”
etc in c++.
What are Operators?
An operator is a symbol that tells the compiler to perform specific
mathematical, logical calculations or some other special operations.
What are the types of operator overloading?

There are two types of operator overloading:


• Function overloading.
• Operator overloading.
What is function overloading? What is Operator overloading?
• The process of having two or • In C++, it can add special
more functions with the same meanings to the functionality and
name but with different behavior of already existing
parameters (arguments) is called operators like arithmetic and
function overloading. other operations.
• The function is redefined by • The mechanism of giving special
either using different types of meaning to an operator is known
arguments or a different number as operator overloading. For
of arguments. It is only through example, we can overload an
these differences that a compiler operator ‘+’ in a class-like string to
can differentiate between concatenate two strings by just
functions. using +.
Operations that can be
performed:
• Athematic operations: + – * / %
• Logical operations: && and ||
• Relational operations: == != >= <=
• Pointer operators: & and *
• Memory management operator: new, delete []
Restrictions on Operator Overloading

• C++ operators that can be overloaded


Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]

• C++ Operators that cannot be overloaded


Operators that cannot be overloaded
. .* :: ?: sizeof
Restrictions on Operator
Overloading
• Overloading restrictions
• Precedence of an operator cannot be changed
• Associativity of an operator cannot be changed
• Arity (number of operands) cannot be changed
• Unary operators remain unary, and binary operators remain
binary
• Operators &, *, + and - each have unary and binary versions
• Unary and binary versions can be overloaded separately
• No new operators can be created
• Use only existing operators
• No overloading operators for built-in types
• Cannot change how two integers are added
• Produces a syntax error
Implementing Operator
overloading:
We can implement the concept of operator overloading with the help of member
function or friend function.

• Member function: It is in the scope of the class in which it is declared.


• Friend function: It is a non-member function of a class with permission to access
both private and protected members.
Syntax for C++ Operator
Overloading
Operator Overloading Examples
Unary Operators and Binary
Operator overloading
Unary operators Binary operators
• Operators which work on a • Operators which works on Two
single operand are called unary operands are called binary
operators. operator.
• Examples:
• Increment operators(++),
Decrement operators(--),
Logical not operator(!) etc…
Unary Operator
Overloading
We can overload a unary operator
like any other operator.
We can redefine the unary
operators to behave in a certain
way on certain operands using
unary operator overloading in C++.
It is basically used for operating on
user-defined datatypes, like classes
and structures.
Example: Let us try overloading the
increment and decrement unary
operators through a C++ program.
Binary Operator
Overloading
We can redefine the binary
operators to operate in a certain
way for user-defined objects. The
binary operators are the operators
that work on two operands, such as
addition (+), multiplication (*), etc.
A single operator can carry out a
variety of functionalities using two
operands provided by the
programmer or user in this
polymorphic compile technique.
Example: Let us see the following C+
+ code that elaborates the
overloading of the addition
operator.
Assignment Operators Overloading
in C++
• You can overload the assignment operator (=) just as you can other
operators and it can be used to create an object just like the copy
constructor.

• Coding Example:
Comparison between
Copy Constructor
and Assignment
Operator
The main purpose of both the
concepts in C++ is to assign the
value, but the main difference
between both is copy
constructor creates a new object
and assigns the value but
assignment operator does not
create a new object, instead it
assigns the value to the data
member of the same object.
The following table highlights
the major differences between
copy constructor and assignment
operator.
Conclusion
• The difference between a copy constructor and an assignment
operator is that a copy constructor helps to create a copy of an
already existing object without altering the original value of the
created object, whereas an assignment operator helps to assign a
new value to a data member or an object in the program.
Operator Functions as
Class Members vs. as friend
Functions
• Member vs non-member
• Operator functions can be member or non-member functions
• When overloading ( ), [ ], -> or any of the assignment
operators, must use a member function
• Operator functions as member functions
• Leftmost operand must be an object (or reference to an
object) of the class
• If left operand of a different type, operator function must be a non-
member function
• Operator functions as non-member functions
• Must be friends if needs to access private or protected
members
• Enable the operator to be commutative
Overloading stream insertion
(<>) operators
• In C++, stream insertion operator “<<” is used for output and
extraction operator “>>” is used for input.
• We must know the following things before we start overloading these
operators.
1) cout is an object of ostream class and cin is an object of istream
class
2) These operators must be overloaded as a global function. And if we
want to allow them to access private data members of the class, we
must make them friend.
Why these operators must be overloaded as
global?

• In operator overloading, if an operator is overloaded as a member, then it


must be a member of the object on the left side of the operator. For
example, consider the statement “ob1 + ob2” (let ob1 and ob2 be objects
of two different classes). To make this statement compile, we must
overload ‘+’ in a class of ‘ob1’ or make ‘+’ a global function.
• The operators ‘<<‘ and ‘>>’ are called like ‘cout << ob1’ and ‘cin >> ob1’.
So if we want to make them a member method, then they must be made
members of ostream and istream classes, which is not a good option
most of the time. Therefore, these operators are overloaded as global
functions with two parameters, cout and object of user-defined class.
#include <iostream>
using namespace std;

class Complex
{ int main()
private: {
int real, imag; Complex c1;
public:
Complex(int r = 0, int i =0)
cin >> c1;
{ real = r; imag = i; } cout << "The complex object is ";
friend ostream & operator << (ostream &out, const Complex &c); cout << c1;
friend istream & operator >> (istream &in, Complex &c); return 0;
}; }
ostream & operator << (ostream &out, const Complex &c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}

istream & operator >> (istream &in, Complex &c)


{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginary Part ";
in >> c.imag;
return in;
}
Overloading Stream-
Insertion and Stream-
Extraction Operators
• Overloaded << and >> operators
• Overloaded to perform input/output for user-defined
types
• Left operand of types ostream & and istream &
• Must be a non-member function because left operand is
not an object of the class
• Must be a friend function to access private data
members

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy