SlideShare a Scribd company logo
5
Most read
7
Most read
9
Most read
Md.Ashraful Islam,CSE 2/1,DUET
presentation
p
h
P
C++
c
java
S
o
+
o
p
CSS
Md.Ashraful Islam,CSE 2/1,DUET
Unary Operator
Overloading(Prefix)
Md.Ashraful Islam,CSE 2/1,DUET
Content
 why we are using operator Overloading
 Defining unary operator Overloading.
 General format operator Overloading.
 Example of unary ++/-- operator Overloading(prefix)
with code analysis.
Md.Ashraful Islam,CSE 2/1,DUET
 Example of unary - operator Overloading.
Generalization of function
overloading
Adding multiple object0202
Extension of language to
include user-defined types
.
04
Readable code
Md.Ashraful Islam,CSE 2/1,DUET
One of the Exciting
Fetures of c++
Works only on the
single variable.
Can be overloaded 2 ways:
Static member function
& Friend function
-,++,-- those are unary
operator Which
we can overload.
Unary
Operator
Overloading
Md.Ashraful Islam,CSE 2/1,DUET
Operator overloading
format
keywordAny type Operator symbol
returnType operator*(parameters)
Example:
void operator ++();
Md.Ashraful Islam,CSE 2/1,DUET
Unary ++/-- operator overloading(prefix).
Md.Ashraful Islam,CSE 2/1,DUET
#include <iostream>
using namespace std;
class Check{
private:
int x;
public:
Check(int a) { x=a; }
void Display(){
cout<<“value of x is = " << x << endl;
}
void operator ++() {
x += 1;
}
void operator --() {
x -= 1;
}
};
int main(){
Check obj(5);
obj.Display();
++obj;
obj.Display();
cout<<“when decrementing”<<endl;
--obj;
obj.Display();
return 0;
}
output
Value of x is = 5
Value of x is = 6
Value of x is = 5
Output
Value of x is = 5 //initial value
Value of x is = 6// incrementing 1
Value of x is = 5//decrementing 1
Constructor
++ operator
overloaded
-- operator
overloaded
Overloading outside the class with *this pointer
Md.Ashraful Islam,CSE 2/1,DUET.
#include <iostream>
using namespace std;
class Check{
private:
int x;
public:
Check(int a) { x=a; }
void Display(){
cout<<“value of x is = " << x <endl;
}
Check operator ++();
Check operator --() ;
};
Check Check:: operator ++() {
x +=1; }
Check Check:: operator --() {
x -=1; }
int main(){
Check obj(5);
obj.Display();
++obj;
obj.Display();
cout<<“when decrementing”<<endl;
--obj;
obj.Display();
return 0;
}
Output
Value of x is = 5
Value of x is = 6
Value of x is = 5
When using
return type there a
simple change occure.
int operator ++() { x += 1;
return *this;
}
int operator --() { x -= 1;
return *this;
}
Using
*this
pointer
#include <iostream>
using namespace std;
class Check{
private:
int x;
public:
Check(int a) { x=a; }
void Display() {
cout<<“value of x is = " << x <endl;
}
Void operator -() {
x =-x ;
} ;
int main(){
Check obj(5);
obj.Display();
-obj;
obj.Display();
obj.Display();
return 0;
}
Output
Value of x is = 5
Value of x is = -5
Output
Value of x is = 5//initial value
Value of x is = -5//overloaded value
Calling
overloaded
operator
Value
initialize
- Operator
overloading
Unary – operator overloading
Md.Ashraful Islam,CSE 2/1,DUET
Thank You For
Watching.
Md.Ashraful Islam,CSE 2/1,DUET.

More Related Content

PPTX
Operator overloading
PDF
Function overloading ppt
PPT
Strings
PPT
Function overloading(c++)
PPTX
Method overloading
PPTX
Tuple in python
PPTX
Structure in c language
Operator overloading
Function overloading ppt
Strings
Function overloading(c++)
Method overloading
Tuple in python
Structure in c language

What's hot (20)

PPTX
classes and objects in C++
PPTX
Data members and member functions
PPTX
Constructor in java
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Operators in java
PDF
03 function overloading
PPTX
Function C programming
PPTX
Member Function in C++
PDF
Methods in Java
PPTX
Strings in Java
PPTX
Classes, objects in JAVA
PPTX
Constructor in java
PDF
Friend function in c++
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PPTX
Access specifiers(modifiers) in java
PPTX
Functions in c++
PDF
Strings in java
PDF
Constructors and destructors
PPTX
PDF
Python set
classes and objects in C++
Data members and member functions
Constructor in java
Data Structures - Lecture 8 [Sorting Algorithms]
Operators in java
03 function overloading
Function C programming
Member Function in C++
Methods in Java
Strings in Java
Classes, objects in JAVA
Constructor in java
Friend function in c++
Conversion of Infix to Prefix and Postfix with Stack
Access specifiers(modifiers) in java
Functions in c++
Strings in java
Constructors and destructors
Python set
Ad

Similar to Unary operator overloading (20)

PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPT
Unary operator overloading
PPTX
Mca 2nd sem u-4 operator overloading
PDF
Ch-4-Operator Overloading.pdf
PPTX
Operator overloading
PPT
Operator Overloading
PPTX
Bca 2nd sem u-4 operator overloading
PPTX
Week7a.pptx
PPT
Polymorphism and function overloading_new.ppt
PDF
Operator overloading
PDF
Operator Overloading Introduction and Concept of OV
PPT
Mastering Operator Overloading in C++...
PPTX
Operator Overloading
PPTX
Operator overloading
PPT
Lec 26.27-operator overloading
PPT
Operator overloading in C++
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
PPTX
Operator overloaing
PPT
Binary operator overloading
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Unary operator overloading
Mca 2nd sem u-4 operator overloading
Ch-4-Operator Overloading.pdf
Operator overloading
Operator Overloading
Bca 2nd sem u-4 operator overloading
Week7a.pptx
Polymorphism and function overloading_new.ppt
Operator overloading
Operator Overloading Introduction and Concept of OV
Mastering Operator Overloading in C++...
Operator Overloading
Operator overloading
Lec 26.27-operator overloading
Operator overloading in C++
Polymorphism and Type Conversion.pdf pot
Operator overloaing
Binary operator overloading
Ad

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Construction Project Organization Group 2.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PDF
PPT on Performance Review to get promotions
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Automation-in-Manufacturing-Chapter-Introduction.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Construction Project Organization Group 2.pptx
bas. eng. economics group 4 presentation 1.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CYBER-CRIMES AND SECURITY A guide to understanding
573137875-Attendance-Management-System-original
PPT on Performance Review to get promotions
Foundation to blockchain - A guide to Blockchain Tech
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks

Unary operator overloading

  • 4. Content  why we are using operator Overloading  Defining unary operator Overloading.  General format operator Overloading.  Example of unary ++/-- operator Overloading(prefix) with code analysis. Md.Ashraful Islam,CSE 2/1,DUET  Example of unary - operator Overloading.
  • 5. Generalization of function overloading Adding multiple object0202 Extension of language to include user-defined types . 04 Readable code Md.Ashraful Islam,CSE 2/1,DUET
  • 6. One of the Exciting Fetures of c++ Works only on the single variable. Can be overloaded 2 ways: Static member function & Friend function -,++,-- those are unary operator Which we can overload. Unary Operator Overloading Md.Ashraful Islam,CSE 2/1,DUET
  • 7. Operator overloading format keywordAny type Operator symbol returnType operator*(parameters) Example: void operator ++(); Md.Ashraful Islam,CSE 2/1,DUET
  • 8. Unary ++/-- operator overloading(prefix). Md.Ashraful Islam,CSE 2/1,DUET #include <iostream> using namespace std; class Check{ private: int x; public: Check(int a) { x=a; } void Display(){ cout<<“value of x is = " << x << endl; } void operator ++() { x += 1; } void operator --() { x -= 1; } }; int main(){ Check obj(5); obj.Display(); ++obj; obj.Display(); cout<<“when decrementing”<<endl; --obj; obj.Display(); return 0; } output Value of x is = 5 Value of x is = 6 Value of x is = 5 Output Value of x is = 5 //initial value Value of x is = 6// incrementing 1 Value of x is = 5//decrementing 1 Constructor ++ operator overloaded -- operator overloaded
  • 9. Overloading outside the class with *this pointer Md.Ashraful Islam,CSE 2/1,DUET. #include <iostream> using namespace std; class Check{ private: int x; public: Check(int a) { x=a; } void Display(){ cout<<“value of x is = " << x <endl; } Check operator ++(); Check operator --() ; }; Check Check:: operator ++() { x +=1; } Check Check:: operator --() { x -=1; } int main(){ Check obj(5); obj.Display(); ++obj; obj.Display(); cout<<“when decrementing”<<endl; --obj; obj.Display(); return 0; } Output Value of x is = 5 Value of x is = 6 Value of x is = 5 When using return type there a simple change occure. int operator ++() { x += 1; return *this; } int operator --() { x -= 1; return *this; } Using *this pointer
  • 10. #include <iostream> using namespace std; class Check{ private: int x; public: Check(int a) { x=a; } void Display() { cout<<“value of x is = " << x <endl; } Void operator -() { x =-x ; } ; int main(){ Check obj(5); obj.Display(); -obj; obj.Display(); obj.Display(); return 0; } Output Value of x is = 5 Value of x is = -5 Output Value of x is = 5//initial value Value of x is = -5//overloaded value Calling overloaded operator Value initialize - Operator overloading Unary – operator overloading Md.Ashraful Islam,CSE 2/1,DUET
  • 11. Thank You For Watching. Md.Ashraful Islam,CSE 2/1,DUET.