SlideShare a Scribd company logo
Operator Overloading
 The process of defining additional meaning to an operator is called
operator overloading.
 It enables the operator to perform different operations depending on the
type of operands.
 It also enable the operators to process the user defined data types.
Example:
 The addition operator is used to add tow numeric values. Suppose a, b and
c are three integer variables. The following statement will add the
contents of a and b and store the result in variable c.
c = a + b;
 The addition operator already know how to process the integer operands.
Continue…
 But it does not know how to process to user defined objects. The above
statement generates an error if a, b and c are objects of a class. However,
operator overloading can enable the addition operator to manipulate two
objects.
Overloading an Operator
 An operator can be overloaded by declaring a special member function in
the class. The member function uses the keyword operator with the
symbol of operator to be overloaded.
Syntax:
 The syntax of overloading an operator is as follows:
return_type operator op()
{
function body;
}
Return_type: It indicates the type of value returned by the member
function.
Operator: It is the keyword that indicates that the member function is
used to overload an operator
Op: It is the symbol of an operator to be overloaded.
Overloading Unary Operators
 A type of operator that works with single operands is called unary
operator. The unary operators are overloaded to increase their
capabilities.
 The unary operators that can be overloaded in OOP using c++ are as
follows:
++ -- () -> new delete
Example: Overloading ++ operator
#include<iosteam> void operator ++()
Class count {
{ n = n+1;
private: }
int n; };
public: int main()
count() {
{ count obj;
n = 0; obj.show();
} ++obj;
void show() obj.show();
{ getch();
cout<<“n=”<<n<<endl; }
}
How above Program Works?
 The above program overloads the increment operator ++ to work to work
with all objects of user defined class count. It increases the value of data
member by 1. The user can use the same format to increase the value of
an object as used with integers.
Operator Overloading with returned value
 The increment operator can be used in assignment operator to store the
incremented value in another variable.
 Suppose a and b are two integers, the following statement will increment
the value of a by 1 then assign the value to b
b = ++a;
The above functionality can be assigned to the operator by returning the
new value from member function. The returned value can be stored in
another object of the same type on the left side of the assignment operator
Example:
#include<iostream>
class count
{
private:
int n, name ,rol_no;
public:
count()
{
n = 0;
}
void show()
{
cout<<“n=“<<n<<endl;
}
count operator ++()
{
count temp;
n = n+ 1;
temp.n = n;
return temp;
}
};
int main()
{
count x , y;
x.show();
y.show();
y = ++x;
x.show();
y.show();
}
Overloading Binary Operators
 A type of operators that works that works with operand is called binary
operators. These operators are overloaded to increase their capabilities.
 The binary operators that can be overloaded in OOP using c++ are as
follows:
== += + - * / % & << >>
-=
Example:
#include<iostream>
class add
{
private:
int a , b;
public:
add()
{
a = b = 0;
}
void input()
{
cout<<“ENTER a = “;
cin>>a;
cout<<“ENTER b = “;
cin>>b ;
}
void show()
{
cout<<“a=“<<a<<endl;
cout<<“b=“<<b<<endl;
}
add operator +(add p)
{
add temp;
temp.a = p.a+a;
temp.b = p.b+b;
return temp;
}
};
int main()
{
add x,y,z;
x.input();
y.input();
z = x + y;
x.show();
y.show();
z.show();
}

More Related Content

What's hot

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
C functions
C functionsC functions
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Type casting
Type castingType casting
Type casting
simarsimmygrewal
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 

What's hot (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
C functions
C functionsC functions
C functions
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Friend functions
Friend functions Friend functions
Friend functions
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Type casting
Type castingType casting
Type casting
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 

Similar to Operator overloading

08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563Youth For Peace
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloadingyatinnarula
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
BalajiGovindan5
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
BalajiGovindan5
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
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
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Functions1
Functions1Functions1
Functions1
DrUjwala1
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Chap 5 c++Chap 5 c++
Programming presentation
Programming presentationProgramming presentation
Programming presentation
Fiaz Khokhar
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
thenmozhip8
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Srikanth Mylapalli
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
ChhaviCoachingCenter
 

Similar to Operator overloading (20)

08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
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
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Oops
OopsOops
Oops
 
Functions1
Functions1Functions1
Functions1
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 

More from Burhan Ahmed

Wireless mobile communication
Wireless mobile communicationWireless mobile communication
Wireless mobile communication
Burhan Ahmed
 
Virtual function
Virtual functionVirtual function
Virtual function
Burhan Ahmed
 
Uses misuses and risk of software
Uses misuses and risk of softwareUses misuses and risk of software
Uses misuses and risk of software
Burhan Ahmed
 
Types of computer
Types of computerTypes of computer
Types of computer
Burhan Ahmed
 
Trees
TreesTrees
Topology
TopologyTopology
Topology
Burhan Ahmed
 
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conductThe distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
Burhan Ahmed
 
Software house organization
Software house organizationSoftware house organization
Software house organization
Burhan Ahmed
 
Social interaction
Social interactionSocial interaction
Social interaction
Burhan Ahmed
 
Role model
Role modelRole model
Role model
Burhan Ahmed
 
Rights and duties
Rights and dutiesRights and duties
Rights and duties
Burhan Ahmed
 
Planning work activities
Planning work activitiesPlanning work activities
Planning work activities
Burhan Ahmed
 
Peripheral devices
Peripheral devicesPeripheral devices
Peripheral devices
Burhan Ahmed
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applications
Burhan Ahmed
 
Normalization
NormalizationNormalization
Normalization
Burhan Ahmed
 
Managing strategy
Managing strategyManaging strategy
Managing strategy
Burhan Ahmed
 
Letter writing
Letter writingLetter writing
Letter writing
Burhan Ahmed
 
Job analysis and job design
Job analysis and job designJob analysis and job design
Job analysis and job design
Burhan Ahmed
 
Intellectual property
Intellectual propertyIntellectual property
Intellectual property
Burhan Ahmed
 
Inheritance
InheritanceInheritance
Inheritance
Burhan Ahmed
 

More from Burhan Ahmed (20)

Wireless mobile communication
Wireless mobile communicationWireless mobile communication
Wireless mobile communication
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Uses misuses and risk of software
Uses misuses and risk of softwareUses misuses and risk of software
Uses misuses and risk of software
 
Types of computer
Types of computerTypes of computer
Types of computer
 
Trees
TreesTrees
Trees
 
Topology
TopologyTopology
Topology
 
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conductThe distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
The distinction of prophet muhammad (s.a.w) among the teachers of moral conduct
 
Software house organization
Software house organizationSoftware house organization
Software house organization
 
Social interaction
Social interactionSocial interaction
Social interaction
 
Role model
Role modelRole model
Role model
 
Rights and duties
Rights and dutiesRights and duties
Rights and duties
 
Planning work activities
Planning work activitiesPlanning work activities
Planning work activities
 
Peripheral devices
Peripheral devicesPeripheral devices
Peripheral devices
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applications
 
Normalization
NormalizationNormalization
Normalization
 
Managing strategy
Managing strategyManaging strategy
Managing strategy
 
Letter writing
Letter writingLetter writing
Letter writing
 
Job analysis and job design
Job analysis and job designJob analysis and job design
Job analysis and job design
 
Intellectual property
Intellectual propertyIntellectual property
Intellectual property
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 

Recently uploaded (20)

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 

Operator overloading

  • 1.
  • 2. Operator Overloading  The process of defining additional meaning to an operator is called operator overloading.  It enables the operator to perform different operations depending on the type of operands.  It also enable the operators to process the user defined data types.
  • 3. Example:  The addition operator is used to add tow numeric values. Suppose a, b and c are three integer variables. The following statement will add the contents of a and b and store the result in variable c. c = a + b;  The addition operator already know how to process the integer operands.
  • 4. Continue…  But it does not know how to process to user defined objects. The above statement generates an error if a, b and c are objects of a class. However, operator overloading can enable the addition operator to manipulate two objects.
  • 5. Overloading an Operator  An operator can be overloaded by declaring a special member function in the class. The member function uses the keyword operator with the symbol of operator to be overloaded.
  • 6. Syntax:  The syntax of overloading an operator is as follows: return_type operator op() { function body; } Return_type: It indicates the type of value returned by the member function. Operator: It is the keyword that indicates that the member function is used to overload an operator Op: It is the symbol of an operator to be overloaded.
  • 7. Overloading Unary Operators  A type of operator that works with single operands is called unary operator. The unary operators are overloaded to increase their capabilities.  The unary operators that can be overloaded in OOP using c++ are as follows: ++ -- () -> new delete
  • 8. Example: Overloading ++ operator #include<iosteam> void operator ++() Class count { { n = n+1; private: } int n; }; public: int main() count() { { count obj; n = 0; obj.show(); } ++obj; void show() obj.show(); { getch(); cout<<“n=”<<n<<endl; } }
  • 9. How above Program Works?  The above program overloads the increment operator ++ to work to work with all objects of user defined class count. It increases the value of data member by 1. The user can use the same format to increase the value of an object as used with integers.
  • 10. Operator Overloading with returned value  The increment operator can be used in assignment operator to store the incremented value in another variable.  Suppose a and b are two integers, the following statement will increment the value of a by 1 then assign the value to b b = ++a; The above functionality can be assigned to the operator by returning the new value from member function. The returned value can be stored in another object of the same type on the left side of the assignment operator
  • 11. Example: #include<iostream> class count { private: int n, name ,rol_no; public: count() { n = 0; } void show() { cout<<“n=“<<n<<endl; } count operator ++() { count temp; n = n+ 1; temp.n = n; return temp; } }; int main() { count x , y; x.show(); y.show(); y = ++x; x.show(); y.show(); }
  • 12. Overloading Binary Operators  A type of operators that works that works with operand is called binary operators. These operators are overloaded to increase their capabilities.  The binary operators that can be overloaded in OOP using c++ are as follows: == += + - * / % & << >> -=
  • 13. Example: #include<iostream> class add { private: int a , b; public: add() { a = b = 0; } void input() { cout<<“ENTER a = “; cin>>a; cout<<“ENTER b = “; cin>>b ; } void show() { cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl; } add operator +(add p) { add temp; temp.a = p.a+a; temp.b = p.b+b; return temp; } };
  • 14. int main() { add x,y,z; x.input(); y.input(); z = x + y; x.show(); y.show(); z.show(); }