Object-Oriented Programming (OOP) Lecture No. 16

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Operator Overloading Fundamentals
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Operator Overloading and Type Conversions
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 CSC241: Object Oriented Programming Lecture No 08.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Chapter 7: Expressions and Assignment Statements
Data Types, Variables & Arithmetic
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 9: Operator Overloading
Review What is an object? What is a class?
Object Oriented Programming COP3330 / CGS5409
Object-Oriented Programming (OOP) Lecture No. 21
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Object-Oriented Programming (OOP) Lecture No. 17
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
The dirty secrets of objects
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Explicit and Implicit Type Changes
Arithmetic Operators in C
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Introduction to C++ Programming
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 18
Arithmetic Operators in C
Overview of C++ Overloading
Chapter-3 Operators.
Dr. Bhargavi Dept of CS CHRIST
Operator overloading Dr. Bhargavi Goswami
Object-Oriented Programming (OOP) Lecture No. 20
CISC/CMPE320 - Prof. McLeod
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Java Programming Language
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 16

Operator overloading Consider the following class: class Complex{ private: double real, img; public: Complex Add(const Complex &); Complex Subtract(const Complex &); Complex Multiply(const Complex &); … }

Operator overloading Function implementation: Complex Complex::Add( const Complex & c1){ Complex t; t.real = real + c1.real; t.img = img + c1.img; return t; }

Operator overloading The following statement: Complex c3 = c1.Add(c2); Adds the contents of c2 to c1 and assigns it to c3 (copy constructor)

Operator overloading To perform operations in a single mathematical statement e.g: c1+c2+c3+c4 We have to explicitly write: c1.Add(c2.Add(c3.Add(c4)))

Alternative way is: t1 = c3.Add(c4); t2 = c2.Add(t1); t3 = c1.Add(t2); Operator overloading Alternative way is: t1 = c3.Add(c4); t2 = c2.Add(t1); t3 = c1.Add(t2);

Operator overloading If the mathematical expression is big: Converting it to C++ code will involve complicated mixture of function calls Less readable Chances of human mistakes are very high Code produced is very hard to maintain

“Operator overloading” C++ provides a very elegant solution: “Operator overloading” C++ allows you to overload common operators like +, - or * etc… Mathematical statements don’t have to be explicitly converted into function calls

Operator overloading Assume that operator + has been overloaded Actual C++ code becomes: c1+c2+c3+c4 The resultant code is very easy to read, write and maintain

Operator overloading C++ automatically overloads operators for pre-defined types Example of predefined types: int float double char long

Example: float x; int y; x = 102.02 + 0.09; Y = 50 + 47; Operator overloading Example: float x; int y; x = 102.02 + 0.09; Y = 50 + 47;

Operator overloading The compiler probably calls the correct overloaded low level function for addition i.e: // for integer addition: Add(int a, int b) // for float addition: Add(float a, float b)

Operator overloading Operator functions are not usually called directly They are automatically invoked to evaluate the operations they implement

List of operators that can be overloaded in C++: Operator overloading List of operators that can be overloaded in C++:

Operator overloading List of operators that can’t be overloaded: Reason: They take name, rather than value in their argument except for ?: ?: is the only ternary operator in C++ and can’t be overloaded

Operator overloading The precedence of an operator is NOT affected due to overloading Example: c1*c2+c3 c3+c2*c1 both yield the same answer

Operator overloading Associativity is NOT changed due to overloading Following arithmetic expression always is evaluated from left to right: c1 + c2 + c3 + c4

Operator overloading Unary operators and assignment operator are right associative, e.g: a=b=c is same as a=(b=c) All other operators are left associative: c1+c2+c3 is same as (c1+c2)+c3

Operator overloading Always write code representing the operator Example: Adding subtraction code inside the + operator will create chaos

Operator overloading Creating a new operator is a syntax error (whether unary, binary or ternary) You cannot create $

Operator overloading Arity of an operator is NOT affected by overloading Example: Division operator will take exactly two operands in any case: b = c / d

Binary operators act on two quantities Binary operators:

Binary operators General syntax: Member function: TYPE1 CLASS::operator B_OP( TYPE2 rhs){ ... }

Binary operators General syntax: Non-member function: TYPE1 operator B_OP(TYPE2 lhs, TYPE3 rhs){ ... }

Binary operators The “operator OP” must have at least one formal parameter of type class (user defined type) Following is an error: int operator + (int, int);

Binary operators Overloading + operator: class Complex{ private: double real, img; public: … Complex operator +(const Complex & rhs); };

Binary operators Complex Complex::operator +( const Complex & rhs){ Complex t; t.real = real + rhs.real; t.img = img + rhs.img; return t; }

Binary operators The return type is Complex so as to facilitate complex statements like: Complex t = c1 + c2 + c3; The above statement is automatically converted by the compiler into appropriate function calls: (c1.operator +(c2)).operator +(c3);

Binary operators If the return type was void, public: class Complex{ ... public: void operator+( const Complex & rhs); };

Binary operators void Complex::operator+(const Complex & rhs){ real = real + rhs.real; img = img + rhs.img; };

Binary operators we have to do the same operation c1+c2+c3 as: c1+c2 // final result is stored in c1

Binary operators Drawback of void return type: Assignments and cascaded expressions are not possible Code is less readable Debugging is tough Code is very hard to maintain