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

CSC-210 Lecture 04-Operator Overloading

The document discusses operator overloading in C++. It explains how operator overloading allows adding meaning to operators in custom contexts by overriding their default behavior. It provides the syntax for declaring overloaded operators and examples of overloading binary operators like addition and subtraction. It also covers overloading assignment, increment, decrement and other special operators, and lists some restrictions on operator overloading in C++.

Uploaded by

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

CSC-210 Lecture 04-Operator Overloading

The document discusses operator overloading in C++. It explains how operator overloading allows adding meaning to operators in custom contexts by overriding their default behavior. It provides the syntax for declaring overloaded operators and examples of overloading binary operators like addition and subtraction. It also covers overloading assignment, increment, decrement and other special operators, and lists some restrictions on operator overloading in C++.

Uploaded by

Usman Ghani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSC-210

OBJECT ORIENTED PROGRAMMING

ABDUL ATTAYYAB KHAN


EMAIL ADDRESS: [email protected]
WEBSITE: https://sites.google.com/view/oopbukc/course-contents

Operator Overloading

1
CONTENTS

 Adding Meaning to Operators.


 Syntax for Operator Overloading.
 Overloading Arithmetic Operators.
 Overloading Binary Operators.
 Overloading addition operator.

 Overloading subtraction operator.

 Defining operator function outside Class definition


 Overloading short hand operators.

 Overloading increment or decrement in prefix form.


 What cannot be overloaded.

ADDING MEANING TO OPERATORS


 Consider a class that defines a complex number. A complex number consists of real
and imaginary parts.
 Thus, the addition of two complex numbers cannot be represented equivalently to
the arithmetic addition of two real numbers.
 To add two complex numbers, we would be required to write a function that
probably takes two complex numbers as parameters and returns another complex
number as a result.
 The function declaration may look like the following:
Complex AddComplex (Complex C1, Complex C2);
 In the above declaration, Complex is a class that defines a complex number. A call
to the above method would be as follows:
CLICK TO EDIT MASTER TITLE STYLE
Complex c3=AddComplex(c1, c2);
 Since it is an addition of two numbers that we are representing in the above
statement, the program code would be more readable if we could write the above
statement as:
Complex c3= c1 + c2;

2
ADDING MEANING TO OPERATORS
Complex c3= c1 + c2;
 The above statement indicates two numbers c1 and c2.
 The actual operation is more involved than a simple arithmetic addition between
two numbers, we would be required to add more meaning to the ‘+’ operator in the
specified context. The C++ feature called operator overloading does this.
 Operator overloading allows to override the default meaning of assigned to the
regular operators in a given context.

CLICK TO EDIT MASTER TITLE STYLE

SYNTAX FOR OPERATOR OVERLOADING


 The general syntax for declaring an operator overloading method is as follows:
retun_type classname::operator# (argument-list)
{
// operations;
}
 Here the # sign is replaced by the operator to be overloaded and the return-type
defines the return type of the method.

CLICK TO EDIT MASTER TITLE STYLE

3
OVERLOADING ARITHMETIC OPERATORS
 First we will consider overloading arithmetic operators such as the addition (‘+’)
and the subtraction (‘-’) operators. These are binary operators; it means they
require two operands.

CLICK TO EDIT MASTER TITLE STYLE

OVERLOADING ARITHMETIC OPERATORS


Overloading Binary Operators:
 Overloading addition operator

CLICK TO EDIT MASTER TITLE STYLE

4
OVERLOADING ARITHMETIC OPERATORS (TASK)
Overloading Binary Operators:
 Overloading addition operator

CLICK TO EDIT MASTER TITLE STYLE

OVERLOADING ARITHMETIC OPERATORS


Overloading Binary Operators:
 Overloading subtraction operator

 Overloading subtraction operator is a straightforward as overloading addition


operator.
 First, we declare the function prototype in the class declaration as follows:
Complex_operator-(Complex complex)

 The function implementation is similar to the implementation of the overloaded


addition operator function. The function definition is shown below:

CLICK TO EDIT MASTER TITLE STYLE

5
OVERLOADING ARITHMETIC OPERATORS
Defining operator function outside Class definition:

CLICK TO EDIT MASTER TITLE STYLE

OVERLOADING SHORT HAND OPERATORS


 will discuss overloading of assignment operators. We will overload += and -=operators.

CLICK TO EDIT MASTER TITLE STYLE

6
OVERLOADING SHORT HAND OPERATORS (TASK)
 will discuss overloading of assignment operators. We will overload += and *=operators.

CLICK TO EDIT MASTER TITLE STYLE

OVERLOADING INCREMENT & DECREMENT OPERATORS IN


PREFIX FORM
 will discuss overloading of ++ and - - operators.

CLICK TO EDIT MASTER TITLE STYLE

7
OVERLOADING INCREMENT & DECREMENT OPERATORS IN
POSTFIX FORM (TASK)

CLICK TO EDIT MASTER TITLE STYLE

THIS KEYWORD IN C++

CLICK TO EDIT MASTER TITLE STYLE

8
OVERLOADING SPECIAL OPERATORS
 Overloading of some special operators like: array subscripting operator [ ] ,
function calling ( ) and class member axis operator -> brings some useful task to
achieve.
 But there are some restrictions while overloading these operators:
 They must be non-static member functions.
 These functions cannot be friend function to overload.

CLICK TO EDIT MASTER TITLE STYLE

OVERLOADING SPECIAL OPERATORS


OVERLOADING ARRAY ACCESS OPERATOR [ ]:

CLICK TO EDIT MASTER TITLE STYLE

9
OVERLOADING SPECIAL OPERATORS (TASK)
OVERLOADING FUNCTION CALL OPERATOR ( ):

CLICK TO EDIT MASTER TITLE STYLE

RULES AND RESTRICTION FOR OPERATOR OVERLOADING


 Overloading of the following operators is not allowed:

 The scope resolution operator : :


 The pointer to member operator .*

 The ternary operator ? :


 The dot operator .

CLICK TO EDIT MASTER TITLE STYLE

10

You might also like