SlideShare a Scribd company logo
1 of 11
Operator Overloading
Dustin Chase
C++ Lightning Talks
December 14, 2015
Outline of Presentation
 Introduction
 Enumeration
 Operator Overloading
 Operator Overloading In Action
 Closing Thoughts
Introduction
 Who am I?
 Expected Background/Audience
 Source Material
Enumeration
 Enumeration (enum): A very simple user-defined type, specifying its
set of values (its enumerators) as symbolic constants.
enum class Card {copper, silver, gold, council_room, …};
if (card_played == Card::copper) {
copperAction();
}
Operator Overloading
 Operator Overloading: Define almost any C++ operator for class or
enumeration operands.
 Provide conventional notation for a type we design
 “+” means add, “=“ means assignment
 Convenience, Efficiency, Clarity
Enumeration and Overloading Together
 Example enumeration:
enum class Month {
Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
};
 Next month?
A Function!
Month nextMonth(const Month &m) {
switch (m)
{
case(Month::Jan):
return Month::Feb;
break;
case(Month::Feb):
return Month::Mar;
break;
…
}
}
 Lots of code to write – error prone
 Not as convenient for users of your code
 What does nextMonth(const Month &m)
do?
Operator Overload!
Month& operator++(Month &m) {
m = (m==Month::Dec) ? Month::Jan : Month(int(m) + 1);
return m;
}
Month end = Month::Jan;
++end;
//vs. end = nextMonth(end);
//More natural to read than a series of function calls
++ Operator Usage
for (Month n = Month::Jan; n < Month::Dec; ++n) {
cout << int(n) << endl;
}
Another example: ‘[]’ operator
 C++ strings
string name = “William”;
name[3] = ‘x’;
//name is now “Wilxiam”;
 Saves us from iterating through the string object, or using some
combination of other functions
Closing
 Enum helps make your code readable
 Card::copper vs. 7 (magic number)
 Operator overloading as well
 Define operators only with conventional meaning
 “+” should mean addition, not “tooblekane”
 Don’t define operators for a type unless you’re sure it will make
positive changes to your code
 The most interesting operators aren’t +, -, * and /, but =, ==, !=, [] and
().

More Related Content

What's hot

#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 

What's hot (20)

#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
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
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
c++
c++c++
c++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Overloading
OverloadingOverloading
Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-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
 

Viewers also liked

Viewers also liked (20)

С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Overloading
OverloadingOverloading
Overloading
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
Effective stl notes
Effective stl notesEffective stl notes
Effective stl notes
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий Леванов
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
 
Effective c++notes
Effective c++notesEffective c++notes
Effective c++notes
 
Modern C++
Modern C++Modern C++
Modern C++
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0
 
STL Algorithms In Action
STL Algorithms In ActionSTL Algorithms In Action
STL Algorithms In Action
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
File Pointers
File PointersFile Pointers
File Pointers
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 

Similar to Operator Overloading

Problem DefinitionBuild a Date class and a main function to test i.pdf
Problem DefinitionBuild a Date class and a main function to test i.pdfProblem DefinitionBuild a Date class and a main function to test i.pdf
Problem DefinitionBuild a Date class and a main function to test i.pdf
smitaguptabootique
 

Similar to Operator Overloading (20)

Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
C++
C++C++
C++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Ch03
Ch03Ch03
Ch03
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Problem DefinitionBuild a Date class and a main function to test i.pdf
Problem DefinitionBuild a Date class and a main function to test i.pdfProblem DefinitionBuild a Date class and a main function to test i.pdf
Problem DefinitionBuild a Date class and a main function to test i.pdf
 

Recently uploaded

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
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
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
VishalKumarJha10
 

Recently uploaded (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
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 🔝✔️✔️
 
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
 
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 ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
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 🔝✔️✔️
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
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
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
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
 

Operator Overloading

  • 1. Operator Overloading Dustin Chase C++ Lightning Talks December 14, 2015
  • 2. Outline of Presentation  Introduction  Enumeration  Operator Overloading  Operator Overloading In Action  Closing Thoughts
  • 3. Introduction  Who am I?  Expected Background/Audience  Source Material
  • 4. Enumeration  Enumeration (enum): A very simple user-defined type, specifying its set of values (its enumerators) as symbolic constants. enum class Card {copper, silver, gold, council_room, …}; if (card_played == Card::copper) { copperAction(); }
  • 5. Operator Overloading  Operator Overloading: Define almost any C++ operator for class or enumeration operands.  Provide conventional notation for a type we design  “+” means add, “=“ means assignment  Convenience, Efficiency, Clarity
  • 6. Enumeration and Overloading Together  Example enumeration: enum class Month { Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };  Next month?
  • 7. A Function! Month nextMonth(const Month &m) { switch (m) { case(Month::Jan): return Month::Feb; break; case(Month::Feb): return Month::Mar; break; … } }  Lots of code to write – error prone  Not as convenient for users of your code  What does nextMonth(const Month &m) do?
  • 8. Operator Overload! Month& operator++(Month &m) { m = (m==Month::Dec) ? Month::Jan : Month(int(m) + 1); return m; } Month end = Month::Jan; ++end; //vs. end = nextMonth(end); //More natural to read than a series of function calls
  • 9. ++ Operator Usage for (Month n = Month::Jan; n < Month::Dec; ++n) { cout << int(n) << endl; }
  • 10. Another example: ‘[]’ operator  C++ strings string name = “William”; name[3] = ‘x’; //name is now “Wilxiam”;  Saves us from iterating through the string object, or using some combination of other functions
  • 11. Closing  Enum helps make your code readable  Card::copper vs. 7 (magic number)  Operator overloading as well  Define operators only with conventional meaning  “+” should mean addition, not “tooblekane”  Don’t define operators for a type unless you’re sure it will make positive changes to your code  The most interesting operators aren’t +, -, * and /, but =, ==, !=, [] and ().