SlideShare a Scribd company logo
1 of 10
My Introduction
Dr. Mujtaba Asad
Education:
PhD: Shanghai Jiao Tong University
Subject QS Rank: 23, Overall QS Rank: 47
MS(CS), BS(TE): FAST-NUCES Lahore
Intermediate (I.C.S): Govt. College University (G.C.U) Lahore
Experience:
10 Years of Teaching & Research Experience
Publications:
6 Impact Factor Journal Publications in Last 3 Years
Research Area:
Deep Learning, Computer Vision (Video Analysis), Anomaly Detection
Operator Overloading in C++
Why Operator Overloading Is
Needed ?
• As we know that the only applicable operators on the class
objects are the assignment operator ( = ) , member
selection operator ( . ).
• Therefore, other operators cannot be directly applied to
class objects.
• However, C++ allows the programmer to extend the
definitions of most of the operators so that operators—such
as relational operators, arithmetic operators, the insertion
operator for data output, and the extraction operator for
data input—can be applied to classes.
• In C++ terminology, this is called operator overloading.
Class Example (Complex Numbers)
How to Redefine (Overload) Operators)
• Overloading an operator (Syntax)
– 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 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 “+” operator as
Member function for Complex
Class
Overloading “<<” operator as
Non-Member function for
Complex Class
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
new[] delete[]
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

More Related Content

Similar to OperatorOverloading.ppt

Similar to OperatorOverloading.ppt (20)

Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Unit ii
Unit iiUnit ii
Unit ii
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
Oops
OopsOops
Oops
 
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
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
ProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPSProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPS
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Synapse india complain sharing info on chapter 8 operator overloading
Synapse india complain sharing info on chapter 8   operator overloadingSynapse india complain sharing info on chapter 8   operator overloading
Synapse india complain sharing info on chapter 8 operator overloading
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 

Recently uploaded (20)

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 

OperatorOverloading.ppt

  • 1. My Introduction Dr. Mujtaba Asad Education: PhD: Shanghai Jiao Tong University Subject QS Rank: 23, Overall QS Rank: 47 MS(CS), BS(TE): FAST-NUCES Lahore Intermediate (I.C.S): Govt. College University (G.C.U) Lahore Experience: 10 Years of Teaching & Research Experience Publications: 6 Impact Factor Journal Publications in Last 3 Years Research Area: Deep Learning, Computer Vision (Video Analysis), Anomaly Detection
  • 3. Why Operator Overloading Is Needed ? • As we know that the only applicable operators on the class objects are the assignment operator ( = ) , member selection operator ( . ). • Therefore, other operators cannot be directly applied to class objects. • However, C++ allows the programmer to extend the definitions of most of the operators so that operators—such as relational operators, arithmetic operators, the insertion operator for data output, and the extraction operator for data input—can be applied to classes. • In C++ terminology, this is called operator overloading.
  • 5. How to Redefine (Overload) Operators) • Overloading an operator (Syntax) – 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
  • 6. 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
  • 7. Overloading “+” operator as Member function for Complex Class
  • 8. Overloading “<<” operator as Non-Member function for Complex Class
  • 9. 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 new[] delete[]
  • 10. 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

Editor's Notes

  1. I am Dr. Mujtaba Asad. I have been in the teaching profession since 2013 and I have total 10 years of teaching and research experience. I completed my PhD from Shanghai Jiao Tong University, which is one the top ranked universities in world, with a subject QS rank of 23rd in the world. I had been awarded the Scholarship for my PhD Studies from Ministry of Interprovincial Coordination (IPC) for top 50 ranked universities of the world. I did my MS and BS degrees from FAST Lahore. I spent 7 years of life studying here in this institute. I did my intermediate from Govt. College University Lahore. I have obtained first division results throughout my academic career. I have total 13 research publications including 6 impact factor journal publications in last 3 years. My areas of research are Deep Learning, Video Analysis and Video Anomaly detection.
  2. Why we need to overload the operators?? As we know that the only applicable operators for the objects of classes are the assignment operator ( = ) , member selection operator (.) Therefore, other operators such as arithmetic, relational, insertion and extraction operators cannot be directly applied to class objects. Lets take an example of complex numbers class.
  3. As C++ does not have a built in data type for storing the complex number so we have created our own datatype by writing the class for it. Now If I declare an object of this datatype (class), I can store and display complex numbers in my program. However, I am still unable to do mathematical calculation on the this type of data. As the complex numbers is the object of a class. . Because for example, if I do this ComplexType z1, z2(3,5), z3(4,6); And then z1=z2+z3 The compiler will not able to decide which member of 1st object to be added in the member of 2nd object. Therefore, to resolve this we have to redefine the meaning of the operator + so that it can work with our complex number datatype. This procedure is known as operator overloading.
  4. For overloading operators, we simply have to add a user defined functions in our program where the name of the function is “operator followed by the operator symbol. The returnType and formal parameters for this function are selected on the basis of operator type and their workings. Lets take an example of overloading Addition operator for ComplexType class. As the + operator returns the same datatype as of its operands therefore, in the this case the returnType will be ComplexType. Also the + operator is a binary operator so it always will take two operands (one on its left and one on its right). So should have two formal parameters for this overloading function. However, there are two ways writing overloading functions in C++ One is to write the overloading function as the member of class And the other is to write the overloading function as non-member function of the class.
  5. For some operators, we have the choice to either write the member or non-member overloading function. However, for others operators we do no have the choice. For square brackets, parenthesis and arrow operator we must have to write member function. For operators, where the left operand is the same object of the class for which the overloading function is being written, then we have a choice to either write member of non-member function of the class For operators, where the left operand is not the same object of the class for which the overloading function is being written, then we must have to write a non-member function of the class. And in this case we have to made this function as friend of the class so that it can access the private members of the class through the operator function.
  6. Coming back to the overloading function of operator +, Lets write the overloading function for operator + as member function of the class. Remember, whenever an overloading function is written as member function the function call with be for the left operand. So for this expression: z1=z2+z3 , the overloading function call be made through the operand z2 and the call will be like this z1=z2.operator(z3). So instead of two formal parameters this member overloading function will have only one parameter of Type ComplexType.
  7. Now if we don’t want to use the complexType class’s member function “Display” to print the complex numbers on the screen. And we just want to use cout<< to display our complex numbers. Then we also have ti overload the stream insertion operator. Now Notice that the left side operand for operator << is not an object of class ComplexType. Therefore, we must have to write non-member overloading function for the operator <<. Remember, whenever an overloading function is written as non-member function , then the function call with made be as standalone call. So for this expression: Cout<<z1; , the stand alone call will be made for the overloading function of operator <<, and the call will be like this operator<<(cout , z1). So instead of one formal parameters this non-member overloading function will have now two parameters of Type ostream and ComplexType. Also, we have to make this function as the friend of class ComplexType so that the stream insertion operator can easily access the private members of complexType object z1. and the function will be like this