Module 9
Module 9
Intructors:
Abir Das and
Sourangshu
Module 09: Programming in C++
Bhattacharya
Operator Overloading
Objectives &
Outline
Operators &
Functions
Intructors: Abir Das and Sourangshu Bhattacharya
Operator
Overloading
Examples
Department of Computer Science and Engineering
String
Indian Institute of Technology, Kharagpur
Enum
{abir, sourangshu}@cse.iitkgp.ac.in
Operator
Overloading
Rules
Slides taken from NPTEL course on Programming in C++
Summary
by Prof. Partha Pratim Das
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 1
Module Objectives
Module 09
Intructors:
Understand the Operator Overloading
Abir Das and
Sourangshu
Bhattacharya
Objectives &
Outline
Operators &
Functions
Operator
Overloading
Examples
String
Enum
Operator
Overloading
Rules
Summary
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 2
Module Outline
Module 09
Intructors:
Basic Differences between Operators & Functions
Abir Das and
Sourangshu Operator Overloading
Bhattacharya
Examples of Operator Overloading
Objectives &
Outline
operator+ for String & Enum
Operators & Operator Overloading Rules
Functions
Operator
Overloading
Examples
String
Enum
Operator
Overloading
Rules
Summary
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 3
Operator & Function
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 4
Difference between Operator & Functions
Module 09
Operator Function
Intructors:
Abir Das and
Sourangshu • Usually written in infix nota- • Always written in prefix no-
Bhattacharya
tion tation
Objectives & • Examples: • Examples:
Outline Infix: a + b; a ? b : c; Prefix: max(a, b);
Operators &
Prefix: ++a; qsort(int[], int, int,
Functions Postfix: a++; void (*)(void*, void*));
Operator
• Operates on one or more • Operates on zero or more ar-
Overloading operands, typically up to 3 guments
Examples (Unary, Binary or Ternary)
String
Enum
• Produces one result • Produces up to one result
Operator • Order of operations is de- • Order of application is de-
Overloading
Rules
cided by precedence and asso- cided by depth of nesting
ciativity
Summary
• Operators are pre-defined • Functions can be defined as
needed
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 5
Operator Functions in C++
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 6
Program 09.01: String Concatenation
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 8
Operator Overloading – Summary of Rules
Module 09
No new operator such as **, <>, or &| can be defined for overloading
Intructors:
Abir Das and Intrinsic properties of the overloaded operator cannot be change
Sourangshu
Bhattacharya Preserves arity
Preserves precedence
Objectives & Preserves associativity
Outline
These operators can be overloaded:
Operators &
Functions [] + - * / % ^ & | ~ ! = += -= *= /= %= ^= &= |=
<< >> >>= <<= == != < > <= >= && || ++ -- , ->* -> ( ) [ ]
Operator
Overloading For unary prefix operators, use: MyType& operator++(MyType& s1)
Examples For unary postfix operators, use: MyType operator++(MyType& s1, int)
String
Enum The operators :: (scope resolution), . (member access), .* (member access
through pointer to member), sizeof, and ?: (ternary conditional) cannot
Operator
Overloading be overloaded
Rules
The overloads of operators &&, ||, and , (comma) lose their special
Summary properties: short-circuit evaluation and sequencing
The overload of operator-> must either return a raw pointer or return an
object (by reference or by value), for which operator-> is in turn overloaded
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 9
Overloading disallowed for
Summary
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 10
Do not overload these operators
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 11
Module Summary
Module 09
Intructors:
Introduced operator overloading
Abir Das and
Sourangshu Explained the rules of operator overloading
Bhattacharya
Objectives &
Outline
Operators &
Functions
Operator
Overloading
Examples
String
Enum
Operator
Overloading
Rules
Summary
NPTEL MOOCs Programming in C++ Intructors: Abir Das and Sourangshu Bhattacharya 12