0% found this document useful (0 votes)
4 views

Lecture 16 Operator Overloading-Stream Insertion Extraction_+ Operator

The document discusses operator overloading in C++ as a nonmember function, emphasizing its advantages for binary arithmetic operators. It explains the use of friend functions to access private data and provides an example of a Vector class that supports operator overloading for vector addition. Additionally, it covers the extraction and insertion operators for input and output stream operations.

Uploaded by

neelamshaheen660
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 16 Operator Overloading-Stream Insertion Extraction_+ Operator

The document discusses operator overloading in C++ as a nonmember function, emphasizing its advantages for binary arithmetic operators. It explains the use of friend functions to access private data and provides an example of a Vector class that supports operator overloading for vector addition. Additionally, it covers the extraction and insertion operators for input and output stream operations.

Uploaded by

neelamshaheen660
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Object Oriented

Programming
(SE1143)
Lecture 16

Department of Software Engineering


Capital University of Science and Technology (CUST)
Outline

 Operator Overloading as a nonmember function


 Overloading binary arithmetic operator +
 Overloading stream extraction operator >>
 Overloading stream insertion operator <<

2
Overloading as a nonmember
function
 When we overload a binary operator as a member function, one of
the operands needs to be the host object. This is fine when each
operand has a different role in the operation.
 However, in some operators, such as (a + b), the two operands play
the same role and neither of them is related to the result. In these
cases, it is better to use a nonmember function.
 C++ allows functions to be declared as friend functions of the class.
 A friend function has no host object, but it is granted friendship so
that it can access the private data members and member functions of
the class without calling the public member functions.
 We use friend functions to overload selected operators.

3
Binary Arithmetic Operators

 It is more appropriate to overload binary arithmetic operators as


friend functions.
 The two operands must already exist. We create a new object inside
the function and return it.
 We can pass the two operands as reference, but the return object
cannot be a reference type because it is created inside the function
definition.
 We can instantiate an object and then assign the results to that
object if needed (fract = fract1 + fract2). We must make sure that the
assignment operator is overloaded.

4
5
6
Extraction and Insertion Operators

 The value of a fundamental type can be extracted from an input


stream object using the extraction operator (>>) or inserted into an
output stream using the insertion operator (<<).
 We can overload these two operators for our class types
 Each of these operators is a binary operator, but the left operand is
an object of the istream class in the case of the extraction operator
and the object of the ostream class in the case of the insertion
operator.

7
8
9
Example for Practice

 Let's consider developing a vector library in C++ to handle vector


operations such as addition. We will design a Vector class that
supports operator overloading for addition (+) to add two vectors.
 Vector Class: The Vector class will be defined with private members
size and a std::vector to store vector elements.
 Constructor and setValue() Function: The constructor initializes the
vector size, and setValue() sets individual vector elements.
 Operator Overloading (operator+): The + operator is overloaded to
perform vector addition. It checks if the dimensions of the vectors
are compatible before performing addition.
 Main Function: In main(), vectors A and B are created and initialized
with values. Vector addition A + B is performed, and the resulting
vector C is displayed using the display() function.

10
This is all for Week 16

11

You might also like