Page 142 - DCAP404 _Object Oriented Programming
P. 142
Balraj Kumar, Lovely Professional University Unit 7: Operator Overloading
Unit 7: Operator Overloading Notes
CONTENTS
Objectives
Introduction
7.1 Defining Operator Overloading
7.2 Rules for Overloading Operators
7.3 Overloading Unary Operators
7.4 Overloading Binary Operators
7.4.1 Using Friend Function
7.4.2 Using Member Function
7.5 Manipulation of Strings using Operator Overloading
7.6 Summary
7.7 Keywords
7.8 Review Questions
7.9 Further Readings
Objectives
After studying this unit, you will be able to:
Recognize the operator overloading
Describe the rules for operator overloading
Explain the overloading of unary operators
Discuss the various binary operators with friend function and member function
Introduction
C++ provides a rich collection of operators. You have already seen the meaning and uses of
many such operators in previous units. One special feature offered by C++ is operator
overloading. This feature is necessary in a programming language supporting objects oriented
features.
Overloading an operator simply means attaching additional meaning and semantics to an
operator. It enables an operator to exhibit more than one operation polymorphically, as illustrated
below:
You know that addition operator (+) is essentially a numeric operator and therefore, requires
two number operands. It evaluates to a numeric value, which is equal to the sum of the two
operands. Evidently this cannot be used in adding two strings. However, we can extend the
operation of addition operator to include string concatenation. Consequently, the addition
operator would work as follows:
“COM” + “PUTER”
LOVELY PROFESSIONAL UNIVERSITY 135