SlideShare a Scribd company logo
1 of 20
Object-Oriented ProgrammingObject-Oriented Programming
(OOP)(OOP)
Lecture No. 19Lecture No. 19
Stream InsertionStream Insertion
operatoroperator
►Often we need to display the dataOften we need to display the data
on the screenon the screen
►Example:Example:
int i=1, j=2;int i=1, j=2;
cout << “i= ”<< i << “n”;cout << “i= ”<< i << “n”;
Cout << “j= ”<< j << “n”;Cout << “j= ”<< j << “n”;
Stream InsertionStream Insertion
operatoroperator
Complex c1;Complex c1;
cout << c1;cout << c1;
cout << c1 << 2;cout << c1 << 2;
// Compiler error: binary '<<' : no// Compiler error: binary '<<' : no
operator // defined which takes a right-operator // defined which takes a right-
hand // operand of typehand // operand of type ‘‘classclass
Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
……
public:public:
……
void operator << (constvoid operator << (const
Complex & rhs);Complex & rhs);
};};
Stream InsertionStream Insertion
operatoroperator
int main(){int main(){
Complex c1;Complex c1;
cout << c1;cout << c1; // Error// Error
c1 << cout;c1 << cout;
c1 << cout << 2; // Errorc1 << cout << 2; // Error
return 0;return 0;
};};
Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
……
public:public:
……
void operator << (ostream &);void operator << (ostream &);
};};
Stream InsertionStream Insertion
operatoroperator
void Complex::operator <<void Complex::operator <<
(ostream & os){(ostream & os){
osos << ‘(‘ << real<< ‘(‘ << real
<< ‘,’ << img << ‘)’;<< ‘,’ << img << ‘)’;
}}
Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
......
friend ostream & operator <<friend ostream & operator <<
(ostream & os, const Complex(ostream & os, const Complex
& c);& c);
};}; Note: this object
is NOT const
Note: return type
is NOT const
Stream InsertionStream Insertion
operatoroperator
// we want the output as:// we want the output as: (real, img)(real, img)
ostream & operator << (ostream &ostream & operator << (ostream &
os, const Complex & c){os, const Complex & c){
os << ‘(‘ << c.realos << ‘(‘ << c.real
<< ‘,‘<< ‘,‘
<< c.img << ‘)’;<< c.img << ‘)’;
return os;return os;
}}
Stream InsertionStream Insertion
operatoroperator
Complex c1(1.01, 20.1),Complex c1(1.01, 20.1),
c2(0.01, 12.0);c2(0.01, 12.0);
cout << c1 << endl << c2;cout << c1 << endl << c2;
Stream InsertionStream Insertion
operatoroperator
Output:Output:
( 1.01 , 20.1 )( 1.01 , 20.1 )
( 0.01 , 12.0 )( 0.01 , 12.0 )
Stream InsertionStream Insertion
operatoroperator
cout << c1 << c2;cout << c1 << c2;
is equivalent tois equivalent to
operator<<(operator<<(
operator<<(cout,c1)operator<<(cout,c1),c2);,c2);
Stream ExtractionStream Extraction
OperatorOperator
►OverloadingOverloading ““>>>>”” operator:operator:
class Complex{class Complex{
......
friend istream & operatorfriend istream & operator
>> (istream & i, Complex &>> (istream & i, Complex &
c);c);
};};
Note: this object
is NOT const
Stream ExtractionStream Extraction
OperatorOperator
istream & operator << (istreamistream & operator << (istream
& in, Complex & c){& in, Complex & c){
in >> c.real;in >> c.real;
in >> c.img;in >> c.img;
return in;return in;
}}
Stream ExtractionStream Extraction
OperatorOperator
►Main Program:Main Program:
Complex c1(1.01, 20.1);Complex c1(1.01, 20.1);
cin >> c1;cin >> c1;
// suppose we// suppose we
entered // 1.0025 forentered // 1.0025 for
c1.real and // 0.0241c1.real and // 0.0241
for c1.imgfor c1.img
cout << c1;cout << c1;
Stream ExtractionStream Extraction
OperatorOperator
Output:Output:
( 1.0025 , 0.0241 )( 1.0025 , 0.0241 )
Other BinaryOther Binary
operatorsoperators►Overloading comparison operators:Overloading comparison operators:
class Complex{class Complex{
public:public:
bool operator == (const Complex & c);bool operator == (const Complex & c);
//friend bool operator == (const//friend bool operator == (const
//Complex & c1, const Complex & c2);//Complex & c1, const Complex & c2);
bool operator != (const Complex & c);bool operator != (const Complex & c);
//friend bool operator != (const//friend bool operator != (const
//Complex & c1, const Complex & c2);//Complex & c1, const Complex & c2);
……
};};
Other BinaryOther Binary
operatorsoperatorsbool Complex::operator ==(constbool Complex::operator ==(const
Complex & c){Complex & c){
ifif(((real == c.real) &&(real == c.real) &&
(img == c.img)(img == c.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}
Other BinaryOther Binary
operatorsoperatorsbool operator ==(constbool operator ==(const
Complex& lhs, const Complex& rhs){Complex& lhs, const Complex& rhs){
ifif(((lhs.real == rhs.real) &&(lhs.real == rhs.real) &&
(lhs.img == rhs.img)(lhs.img == rhs.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}
Other BinaryOther Binary
operatorsoperatorsbool Complex::operator !=(constbool Complex::operator !=(const
Complex & c){Complex & c){
ifif(((real != c.real) ||(real != c.real) ||
(img != c.img)(img != c.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}

More Related Content

What's hot

Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
Syed Umair
 

What's hot (20)

JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - Harsh
 
Unit 1 of c++ first program
Unit 1 of c++ first programUnit 1 of c++ first program
Unit 1 of c++ first program
 
Debugging JavaScript with Chrome
Debugging JavaScript with ChromeDebugging JavaScript with Chrome
Debugging JavaScript with Chrome
 
Unit Testing Front End JavaScript
Unit Testing Front End JavaScriptUnit Testing Front End JavaScript
Unit Testing Front End JavaScript
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223Jarmo van de Seijp Shadbox ERC223
Jarmo van de Seijp Shadbox ERC223
 
Reactive x
Reactive xReactive x
Reactive x
 
JavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet WetJavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet Wet
 
The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the Microcosm
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
Hypercritical C++ Code Review
Hypercritical C++ Code ReviewHypercritical C++ Code Review
Hypercritical C++ Code Review
 
Loops (1)
Loops (1)Loops (1)
Loops (1)
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Implementing string
Implementing stringImplementing string
Implementing string
 
RxJS ‘Marble’ programming
RxJS ‘Marble’ programmingRxJS ‘Marble’ programming
RxJS ‘Marble’ programming
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
Promises generatorscallbacks
Promises generatorscallbacksPromises generatorscallbacks
Promises generatorscallbacks
 

Viewers also liked

Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
Matt Harrison
 

Viewers also liked (20)

Sample Website Proposal Presentation
Sample Website Proposal PresentationSample Website Proposal Presentation
Sample Website Proposal Presentation
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
Python - Lecture 1
Python - Lecture 1Python - Lecture 1
Python - Lecture 1
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
Introduction to Advanced Javascript
Introduction to Advanced JavascriptIntroduction to Advanced Javascript
Introduction to Advanced Javascript
 
Meetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonMeetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en python
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Installing Python on Mac
Installing Python on MacInstalling Python on Mac
Installing Python on Mac
 
Python for All
Python for All Python for All
Python for All
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python master class part 1
Python master class part 1Python master class part 1
Python master class part 1
 
Introduction to facebook java script sdk
Introduction to facebook java script sdk Introduction to facebook java script sdk
Introduction to facebook java script sdk
 
Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running Notes
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2
 
Lec02 structures (2)
Lec02 structures (2)Lec02 structures (2)
Lec02 structures (2)
 
Introduction to facebook javascript sdk
Introduction to facebook javascript sdk Introduction to facebook javascript sdk
Introduction to facebook javascript sdk
 
Running openCV project on Mac OS
Running openCV project on Mac OSRunning openCV project on Mac OS
Running openCV project on Mac OS
 
Concise Notes on Python
Concise Notes on PythonConcise Notes on Python
Concise Notes on Python
 

Similar to Operator Overloading

Formal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction ContractsFormal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction Contracts
Gera Shegalov
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docx
dewhirstichabod
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
pnaran46
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18
ecomputernotes
 

Similar to Operator Overloading (20)

M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
c++ program using All data type and operators
c++ program using All data type and operators c++ program using All data type and operators
c++ program using All data type and operators
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Lecture04
Lecture04Lecture04
Lecture04
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
Lecture05
Lecture05Lecture05
Lecture05
 
C++ Programs
C++ ProgramsC++ Programs
C++ Programs
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Formal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction ContractsFormal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction Contracts
 
Taming event-driven software via formal verification
Taming event-driven software via formal verificationTaming event-driven software via formal verification
Taming event-driven software via formal verification
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docx
 
Managing console
Managing consoleManaging console
Managing console
 
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
 
Good code
Good codeGood code
Good code
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxWINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
 

More from Sardar Alam

More from Sardar Alam (17)

Undoing of mental illness -- seek help
Undoing of mental illness -- seek helpUndoing of mental illness -- seek help
Undoing of mental illness -- seek help
 
introduction to python
introduction to pythonintroduction to python
introduction to python
 
skin disease classification
skin disease classificationskin disease classification
skin disease classification
 
filters for noise in image processing
filters for noise in image processingfilters for noise in image processing
filters for noise in image processing
 
Noise Models
Noise ModelsNoise Models
Noise Models
 
Noise Models
Noise ModelsNoise Models
Noise Models
 
Introduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learningIntroduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learning
 
Opengl texturing
Opengl texturingOpengl texturing
Opengl texturing
 
Mathematics fundamentals
Mathematics fundamentalsMathematics fundamentals
Mathematics fundamentals
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
 
3 d graphics with opengl part 1
3 d graphics with opengl part 13 d graphics with opengl part 1
3 d graphics with opengl part 1
 
3 d graphics basics
3 d graphics basics3 d graphics basics
3 d graphics basics
 
2 d transformations
2 d transformations2 d transformations
2 d transformations
 
Gui
GuiGui
Gui
 
Inheritance
InheritanceInheritance
Inheritance
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
 
Java basics
Java basicsJava basics
Java basics
 

Recently uploaded

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Recently uploaded (20)

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 

Operator Overloading