SlideShare una empresa de Scribd logo
1 de 23
More C++ Concepts ,[object Object],[object Object],[object Object],[object Object]
Operator overloading ,[object Object],[object Object]
Why Operator Overloading int i, j, k;  // integers float m, n, p;  // floats k = i  +  j;   // integer addition and assignment p = m  +  n;   // floating addition and assignment The compiler overloads the  +  operator for built-in integer and float types by default, producing integer addition with i+j, and floating addition with m+n. We can make object operation look like individual int variable operation, using operator functions  Complex a,b,c; c = a  +  b;
Operator Overloading Syntax ,[object Object],operator @(argument-list) --- operator is a function --- @ is one of C++ operator symbols (+, -, =, etc..) Examples: operator+ operator- operator* operator/
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Example of Operator Overloading void CStr::cat(char *s) { int n; char *pTemp; n=strlen(s); if (n==0) return; pTemp=new char[n+nLength+1]; if (pData)  strcpy(pTemp,pData); strcat(pTemp,s); pData=pTemp; nLength+=n; }
The Addition (+) Operator ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],new_string str1 strlen(str1) strcat(str1,str2) strlen(str1)+strlen(str2)
How does it work? ,[object Object],[object Object],[object Object],CStr CStr::operator+ (CStr str1,CStr str2) { CStr new_string(str1); new_string.cat(str2.get()); return new_string; } “ John Johnson” Temporary CStr object Copy constructor name
Implementing Operator Overloading ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],Implementing Operator Overloading class Complex { ... public: ... Complex operator +(const Complex &op)  { double real  = _real  + op._real, imag = _imag + op._imag; return(Complex(real, imag)); } ... }; c = a+b; c =  a.operator+  (b);
[object Object],Implementing Operator Overloading class Complex { ... public: ... double real() { return _real; } //need access functions double imag() { return _imag; } ... }; Complex operator +(Complex &op1, Complex &op2)  { double real  = op1.real()  + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); } c = a+b; c =  operator+  (a, b);
[object Object],Implementing Operator Overloading class Complex { ... public: ... friend Complex operator +( const Complex &,  const Complex & ); ... }; Complex operator +(Complex &op1, Complex &op2)  { double real  = op1._real  + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); } c = a+b; c =  operator+  (a, b);
Ordinary Member Functions, Static Functions and Friend Functions ,[object Object],[object Object],[object Object],[object Object]
What is ‘Friend’? ,[object Object],[object Object],[object Object],[object Object],[object Object]
More about ‘Friend’ ,[object Object],[object Object],[object Object],[object Object],[object Object]
Assignment Operator ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Assignment for CStr class ,[object Object],[object Object],[object Object],[object Object],Assignment operator for CStr: CStr&  CStr::operator=(const CStr & source) Copy Assignment is  different  from Copy Constructor Return type - a reference to (address of) a CStr object Argument type - a reference to a CStr object (since it is const, the function cannot modify it) Assignment function is called as a member function of the left operand =>Return the object itself str1=str2; str1.operator=(str2)
The “ this ” pointer  ,[object Object],[object Object],[object Object],pData nLength this Data member reference Equivalent to pData this->pData nLength this->nLength CStr object (*this)
Overloading stream-insertion and stream-extraction operators ,[object Object],[object Object],[object Object],friend ostream& operator<< (ostream &os, const Date &d); ostream& operator<<(ostream &os, const Date &d) { os<<d.month<<“/”<<d.day<<“/”<<d.year; return os; } … cout<< d1;  //overloaded operator ostream& operator<<(ostream &os, const Date &d) { os<<d.month<<“/”<<d.day<<“/”<<d.year; return os; } … cout<< d1;  //overloaded operator cout  ----  object of  ostream cout  ----  object of  ostream
Overloading stream-insertion and stream-extraction operators ,[object Object],istream& operator>> (istream &in, Date &d)  { char mmddyy[9]; in >> mmddyy; // check if valid data entered if (d.set(mmddyy))  return in; cout<< &quot;Invalid date format: &quot;<<d<<endl; exit(-1); } friend istream& operator>> (istream &in, Date &d); cin >> d1; cin  ----  object of  istream
Inline functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Example of Inline functions  Inline functions within class declarations Inline functions outside of class declarations In both cases, the compiler will insert the code of the functions get_Data() and getlength() instead of generating calls to these functions
Inline functions (II) ,[object Object],[object Object]
Take Home Message ,[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

#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
Operator overloadingOperator overloading
Operator overloading
abhay singh
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 

La actualidad más candente (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
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 in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
#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
operator overloadingoperator overloading
operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
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
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
c++
c++c++
c++
 

Destacado

PROEXPOSURE spotlight: Yenenesh Abraham
PROEXPOSURE spotlight: Yenenesh AbrahamPROEXPOSURE spotlight: Yenenesh Abraham
PROEXPOSURE spotlight: Yenenesh Abraham
PROEXPOSURE CIC
 
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
Natasha Khramtsovsky
 

Destacado (20)

Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011
 
STL Algorithms In Action
STL Algorithms In ActionSTL Algorithms In Action
STL Algorithms In Action
 
Set Theory In C++
Set Theory In C++Set Theory In C++
Set Theory In C++
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
500 important and useful bangla translation
500 important and useful bangla translation500 important and useful bangla translation
500 important and useful bangla translation
 
CiviCRM bij Cavaria
CiviCRM bij CavariaCiviCRM bij Cavaria
CiviCRM bij Cavaria
 
Ханс Фредрик Берг - NOARK : Норвежский стандарт управления документами
Ханс Фредрик Берг - NOARK: Норвежский стандарт управления документамиХанс Фредрик Берг - NOARK: Норвежский стандарт управления документами
Ханс Фредрик Берг - NOARK : Норвежский стандарт управления документами
 
PROEXPOSURE spotlight: Yenenesh Abraham
PROEXPOSURE spotlight: Yenenesh AbrahamPROEXPOSURE spotlight: Yenenesh Abraham
PROEXPOSURE spotlight: Yenenesh Abraham
 
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
InterPARES 2: Система принципов для разработки политик, стратегий и стандарто...
 
Международные стандарты функциональных требований к системам электронного док...
Международные стандарты функциональных требований к системам электронного док...Международные стандарты функциональных требований к системам электронного док...
Международные стандарты функциональных требований к системам электронного док...
 

Similar a Lecture5

C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
dhavalbl38
 

Similar a Lecture5 (20)

Lecture5
Lecture5Lecture5
Lecture5
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Presentation
PresentationPresentation
Presentation
 
02.adt
02.adt02.adt
02.adt
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
C++ language
C++ languageC++ language
C++ language
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

Lecture5

  • 1.
  • 2.
  • 3. Why Operator Overloading int i, j, k; // integers float m, n, p; // floats k = i + j; // integer addition and assignment p = m + n; // floating addition and assignment The compiler overloads the + operator for built-in integer and float types by default, producing integer addition with i+j, and floating addition with m+n. We can make object operation look like individual int variable operation, using operator functions Complex a,b,c; c = a + b;
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.