SlideShare a Scribd company logo
Department of I.T. Engineering
Presentation
On
Operator Overloading
Academic year 2018-19
Laxmi Institute of Technology, Sarigam
Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad
Enrolment
number
Name
160860116018 Nishant P. Joshi
160860116029 Raj M. Patel
160860116032 Udit A. Patel
Content
 Introduction
 Operator overloading
 Defining operator overloading
 Overloading Input/output operator
 Overloading Unary Operator
 Overloading Binary Operator
 Rules for overloading operator
 Restrictions on Operator Overloading
Introduction
 Overloading an operator
◦ Write function definition as normal
◦ Function name is keyword operator followed by the
symbol for the operator being overloaded
◦ operator+ used to overload the addition operator (+)
 Using operators
◦ To use an operator on a class object it must be
overloaded unless the assignment operator(=)or the
address operator(&)
 Assignment operator by default performs
memberwise assignment
 Address operator (&) by default returns the address
of an object
Operator overloading
 Addition (+) operator can work on operands of type
char, int, float & double.
 However, if s1, s2, s3 are objects of the class string, the
we can write the statement,
s3 = s1 + s2;
 This means C++ has the ability to provide the operators
with a special meaning for a data type.
 Mechanism of giving special meaning to an operator is
known as operator overloading.
Operator overloading
 Operator – is a symbol that indicates an operation.
 Overloading – assigning different meanings to an
operation, depending upon the context.
 For example:
input(>>) / output(<<) operator
– The built-in definition of the operator << is for
shifting of bits.
– It is also used for displaying the values of various
data types
Defining operator overloading
 The general form of an operator function is:
return-type class-name :: operator op (argList)
{
function body // task defined.
}
– where return-type is the type of value returned by
the specified operation.
– op is the operator being overloaded.
– operator op is the function name, where operator is
a keyword.
Overloading Input/output operator
 C++ is able to input and output the built-in data types
using the stream extraction operator >> and the stream
insertion operator <<.
 Overloaded to perform input/output for user defined data
types.
 Left Operand will be of types ostream & and istream &.
 Function overloading this operator must be a Non-
Member function because left operand is not an Object
of the class.
 It must be a friend function to access private data
members.
Overloading Input/output operator
#include<iostream>
using namespace std;
class time
{
int hr,min,sec;
public: time()
{
hr=0, min=0; sec=0;
}
time(int h,int m, int s)
{
hr=h, min=m; sec=s;
}
friend ostream& operator << (ostream &out, time &tm);
//overloading ‘<<' operator
};
Overloading Input/output operator
ostream& operator << (ostream &out, time &tm) //operator function
{
out << "Time is " << tm.hr << "hour : " << tm.min<< "min : " <<
tm.sec
<< "sec";
return out;
}
int main()
{
time tm(3,15,45); cout << tm; return 0;
}
Output:
Time is 3 hour : 15 min : 45 sec
Overloading Unary Operator
#include <iostream>
using namespace std;
class temp
{
private:
int count;
public:
temp():count(5)
{ }
void operator ++()
{
count=count+1;
}
void Display()
{
cout<<“Count:”<<count;
}
};
int main()
{
temp t;
++t;
/* operator function void
operator ++() is called */
t.Display();
return 0;
Output
Count: 6
Overloading Binary Operator
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the
value of Complex Numbers
a,b:";
cin>>a>>b;
}
complex
operator+(complex ob)
{ complex t;
t.a=ob.a +a;
t.b=ob.b+b;
return(t);
complex operator-(complex ob)
{
complex t;
t.a=ob.a - a;
t.b=ob.b -b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"
<<"n";
}
};
cout<<"Input Values:n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
void main()
{
clrscr();
complex
obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
Overloading Binary Operator
In overloading of binary operators the left hand operand
is used to invoke the operator function and the right hand
Rules for overloading operator
 Only existing operators can be overloaded. We cannot
create a new operator.
 Overloaded operator should contain one operand of
user-defined data type. – Overloading operators are only
for classes. We cannot overload the operator for built-in
data types.
 Overloaded operators have the same syntax as the
original operator.
 Operator overloading is applicable within the scope
(extent) in which overloading occurs.
Restrictions on Operator Overloading
 Overloading restrictions
◦ Arity (number of operands) cannot be changed
◦ Unary operators remain unary, and binary operators remain
binary
 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
Restrictions on Operator Overloading
 C++ operators that can be overloaded
 C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
Thank you

More Related Content

What's hot

Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Function
FunctionFunction
Function
jayesh30sikchi
 
Operators in java
Operators in javaOperators in java
Operators in java
Muthukumaran Subramanian
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
University of Madras
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ramya marichamy
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Destructors
DestructorsDestructors
Destructors
DeepikaT13
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
anshu_atri
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 

What's hot (20)

Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Python functions
Python functionsPython functions
Python functions
 
Function
FunctionFunction
Function
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Destructors
DestructorsDestructors
Destructors
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Function in C
Function in CFunction in C
Function in C
 
Structure in c
Structure in cStructure in c
Structure in c
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 

Similar to operator overloading

Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Lecture5
Lecture5Lecture5
Lecture5
ravifeelings
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
RashidFaridChishti
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
 
Oops
OopsOops
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
NabeelaNousheen
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
piyush Kumar Sharma
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
Northeastern University
 

Similar to operator overloading (20)

Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Lecture5
Lecture5Lecture5
Lecture5
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
 
Oops
OopsOops
Oops
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 

Recently uploaded

Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 

Recently uploaded (20)

Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 

operator overloading

  • 1. Department of I.T. Engineering Presentation On Operator Overloading Academic year 2018-19 Laxmi Institute of Technology, Sarigam Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad Enrolment number Name 160860116018 Nishant P. Joshi 160860116029 Raj M. Patel 160860116032 Udit A. Patel
  • 2. Content  Introduction  Operator overloading  Defining operator overloading  Overloading Input/output operator  Overloading Unary Operator  Overloading Binary Operator  Rules for overloading operator  Restrictions on Operator Overloading
  • 3. Introduction  Overloading an operator ◦ Write function definition as normal ◦ Function name is keyword operator followed by the symbol for the operator being overloaded ◦ operator+ used to overload the addition operator (+)  Using operators ◦ To use an operator on a class object it must be overloaded unless the assignment operator(=)or the address operator(&)  Assignment operator by default performs memberwise assignment  Address operator (&) by default returns the address of an object
  • 4. Operator overloading  Addition (+) operator can work on operands of type char, int, float & double.  However, if s1, s2, s3 are objects of the class string, the we can write the statement, s3 = s1 + s2;  This means C++ has the ability to provide the operators with a special meaning for a data type.  Mechanism of giving special meaning to an operator is known as operator overloading.
  • 5. Operator overloading  Operator – is a symbol that indicates an operation.  Overloading – assigning different meanings to an operation, depending upon the context.  For example: input(>>) / output(<<) operator – The built-in definition of the operator << is for shifting of bits. – It is also used for displaying the values of various data types
  • 6. Defining operator overloading  The general form of an operator function is: return-type class-name :: operator op (argList) { function body // task defined. } – where return-type is the type of value returned by the specified operation. – op is the operator being overloaded. – operator op is the function name, where operator is a keyword.
  • 7. Overloading Input/output operator  C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<.  Overloaded to perform input/output for user defined data types.  Left Operand will be of types ostream & and istream &.  Function overloading this operator must be a Non- Member function because left operand is not an Object of the class.  It must be a friend function to access private data members.
  • 8. Overloading Input/output operator #include<iostream> using namespace std; class time { int hr,min,sec; public: time() { hr=0, min=0; sec=0; } time(int h,int m, int s) { hr=h, min=m; sec=s; } friend ostream& operator << (ostream &out, time &tm); //overloading ‘<<' operator };
  • 9. Overloading Input/output operator ostream& operator << (ostream &out, time &tm) //operator function { out << "Time is " << tm.hr << "hour : " << tm.min<< "min : " << tm.sec << "sec"; return out; } int main() { time tm(3,15,45); cout << tm; return 0; } Output: Time is 3 hour : 15 min : 45 sec
  • 10. Overloading Unary Operator #include <iostream> using namespace std; class temp { private: int count; public: temp():count(5) { } void operator ++() { count=count+1; } void Display() { cout<<“Count:”<<count; } }; int main() { temp t; ++t; /* operator function void operator ++() is called */ t.Display(); return 0; Output Count: 6
  • 11. Overloading Binary Operator #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=ob.a +a; t.b=ob.b+b; return(t); complex operator-(complex ob) { complex t; t.a=ob.a - a; t.b=ob.b -b; return(t); } void display() { cout<<a<<"+"<<b<<"i" <<"n"; } };
  • 12. cout<<"Input Values:n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); } void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; Overloading Binary Operator In overloading of binary operators the left hand operand is used to invoke the operator function and the right hand
  • 13. Rules for overloading operator  Only existing operators can be overloaded. We cannot create a new operator.  Overloaded operator should contain one operand of user-defined data type. – Overloading operators are only for classes. We cannot overload the operator for built-in data types.  Overloaded operators have the same syntax as the original operator.  Operator overloading is applicable within the scope (extent) in which overloading occurs.
  • 14. Restrictions on Operator Overloading  Overloading restrictions ◦ Arity (number of operands) cannot be changed ◦ Unary operators remain unary, and binary operators remain binary  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
  • 15. Restrictions on Operator Overloading  C++ operators that can be overloaded  C++ Operators that cannot be overloaded Operators that cannot be overloaded . .* :: ?: sizeof Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete