Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Operator Overloading Fundamentals
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Chapter 8 Operator Overloading, Friends, and References.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
CS Object Oriented Programming Using C++
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 CSC241: Object Oriented Programming Lecture No 05.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Operator Overloading.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Overloading C++ supports the concept of overloading Two main types
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Visit for more Learning Resources
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
Operator Overloading.
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++
Operator Overloading; String and Array Objects
Lecture 7.
Operator overloading Friend Function This Operator Inline Function
Presentation transcript:

Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading

Computer Science Department Operator Overloading Operator overloading is a powerful feature of C++ It provides programmers with a concise notation for manipulating user defined objects It is simple to implement given a few basic rules CPS235:Operator Overloading2

Computer Science Department CPS235:Operator Overloading3 int i, j, k; // integers float m, n, p;// floats // integer addition and assignment k = i + j; // floating addition and assignment p = m + n; Why Operator Overloading ? The compiler overloads the + operator for built- in integer and float types by default, producing integer addition with i+j, and floating addition with m+n We can make object operation look like individual int variable operation, using operator functions Complex a,b,c; c = a + b;

Computer Science Department Operators in C++ C++ has a rich collection of operators most of which are common to other programming languages Standard arithmetic and logical operators + - * / % & ! > < || && == etc Array indexing and function evaluation operators [] () Assignment operators = += -= *= /= etc. Auto increment and decrement operators Pointer de-referencing and address of operators * & Memory management operators new delete new[] delete[] CPS235:Operator Overloading4

Computer Science Department Operators in C++ We can divide up the set of operators into unary and binary operators A unary operator has one operand Examples CPS235:Operator Overloading5 if (!x){..}// unary operator !, operand x x++;// post-fix operator ++, operand x --x;// pre-fix operator --, operand x int y=a[5];// operator [], operand a

Computer Science Department Operators in C++ A binary operator has two operands Examples CPS235:Operator Overloading6 int z=x+y;// binary operator +, operands x and y bool z=x&&y;// binary operator && operands x and y x+=y;// binary operator +=, operands x and y

Computer Science Department Operator Overload Functions In order to overload an operator op, a function operator op must be defined –operator+ to overload + –operator+= to overload += –etc We can either provide member functions of a class or external functions (possibly friend functions of a class) CPS235:Operator Overloading7

Computer Science Department Restrictions on operator overloading Overloading an operator cannot change it’s precedence Overloading an operator cannot change the number of operands It is not possible to create new operators, only new versions of existing operators can be created Operator meaning on built-in features cannot be changed –For instance you cannot change the “+” for integers At least one argument of an operator function must be an object or reference of a user-defined type – This prevents programmers from changing how operators work on fundamental types CPS235:Operator Overloading8

Computer Science Department CPS235:Operator Overloading9 Operator Overloading Syntax Syntax is: operator is a is one of C++ operator symbols (+, -, =, etc..) Examples: operator+ operator- operator* operator/

Computer Science Department Operator Overloading Format Format –The name of an operator is always a conjunction of the keyword operator and then the symbol itself –Examples Operator+ Operator++ Operator- - Operator – Operator << Operator == Operator = CPS235:Operator Overloading10

Computer Science Department Operators that can be overloaded CPS235:Operator Overloading11 +-*/%^ &|~!=< >+=-=*=/=%= ^=&=|=<<>>>>= <<===!=<=>=&& ||++--->*,-> []()newdeletenew[]delete[]

Computer Science Department Operators that cannot be overloaded..*::?: CPS235:Operator Overloading12

Computer Science Department CPS235:Operator Overloading13 Forms of Overloaded Operators Member Functions Friend Functions Free-Standing or Global Functions

Computer Science Department CPS235:Operator Overloading14 Operator Functions When to make operator functions class members, friends or global functions? If operator overload function is a member function, then “ this” is implicitly available for one of the arguments When overloading =, ( ), [ ], ->, the operator overloading function must be declared as a class member. For other operators, the overloading functions can be non-members

Computer Science Department Operator Functions When an operator function is implemented as a member function, the left most (or only in the case of unary operators) operand must be a class object (or a reference to a class object) of operator's class If the left operand must be an object of a different class or a built-in type, this operator must be implemented as a non-class member. eg. > operators CPS235:Operator Overloading15

Computer Science Department Operator Functions An operator function implemented as a non-member must be a friend if it needs to access non-public data members of that class The overloaded << operator must have a left operand of type ostream. Therefore, it must be a non- member function. Also, it may require access to the private data members of the class. Thus, it needs to be a friend function for that class Similar observation holds for >> operator which has a left operand of type istream CPS235:Operator Overloading16

Computer Science Department Operator Functions Operator member functions are classed only when the left operand of a binary operator is specifically an object of that class or when the single operand of a unary operator is an object of that class If the operator needs to be commutative (a + b = b + a), then making it a non-member function is necessary CPS235:Operator Overloading17

Computer Science Department Overloading Unary Operators (using member functions) class counter { private: int count; public: counter():count(0){} counter(int c):count(c) {} int get_count() { return count; } counter operator++ ()//prefix operator { ++count; counter temp; temp.count = count; return counter(count); } counter operator++(int)//postfix operator { return counter(count++); } }; CPS235:Operator Overloading18

Computer Science Department Overloading Unary Operators (using member functions) int main() { counter c1, c2, c3; ++c1; // or c1.operator++(); ++c2; cout<<'\n'<<c1.get_count(); cout<<'\n'<<c2.get_count(); cout<<endl; c3 = c1++; //or c3 = c1.operator++(0); cout<<'\n'<<c3.get_count(); cout<<'\n'<<c1.get_count(); getch(); return 0; } CPS235:Operator Overloading19

Computer Science Department Overloading Binary Operators (using member functions) class counter { private: int count; public: counter():count(0){} counter(int c):count(c) {} int get_count() { return count; } counter operator+(const counter & rhs) { return (counter(count+rhs.count)); } }; CPS235:Operator Overloading20

Computer Science Department Overloading Binary Operators (using member functions) int main() { counter c1(10), c2(10), c3; c3 = c1 + c2; //or c3 = c1.operator+(c2); cout<<'\n'<<c3.get_count(); getch(); return 0; } CPS235:Operator Overloading21

Computer Science Department CPS235:Operator Overloading22 Implementing Operator Overloading Two ways: –Implemented as member functions –Implemented as non-member or Friend functions the operator function may need to be declared as a friend if it requires access to protected or private data Expression translates into a function call if this function is defined within the class of which obj1 is a member if this function is defined outside the class of which obj1 is a member

Computer Science Department CPS235:Operator Overloading23 1.Defined as a member function Implementing Operator Overloading class Complex {... private: double _real, double _imag; public:... Complex operator +(const Complex &rhs) { double real = _real + op._real; double imag = _imag + op._imag; return(Complex(real, imag)); }... }; c = a+b; c = a.operator+(b);

Computer Science Department CPS235:Operator Overloading24 2.Defined as a non-member function Implementing Operator Overloading Complex operator +(Complex &op1, Complex &op2) { double real = op1.real() + op2.real(); double imag = op1.imag() + op2.imag(); return(Complex(real, imag)); } c = a+b; c = operator+ (a, b);

Computer Science Department CPS235:Operator Overloading25 class Complex {... public:... //need access functions double real() { return _real; } double imag() { return _imag; }... }; 2.Defined as a non-member function Implementing Operator Overloading

Computer Science Department class Complex {... public:... friend Complex operator +( const Complex &, const Complex & );... }; CPS235:Operator Overloading26 3.Defined as a friend function Implementing Operator Overloading c = a+b; c = operator+ (a, b); Complex operator +(Complex &op1, Complex &op2) { double real = op1._real + op2._real; double imag = op1._imag + op2._imag; return(Complex(real, imag)); }

Computer Science Department Overloading the Assignment Operator See code example assignment.cpp To enable chaining i.e., om3 = om2 = om1; CPS235:Operator Overloading27 void operator=(const omega& rhs) { strncpy(name,rhs.name,size); } omega operator=(const omega& rhs) { strncpy(name,rhs.name,size); return omega(name); }

Computer Science Department Overloading the Comparison Operators class Distance { int feet; float inches; public: Distance():feet(0),inches(0.0){} Distance(int ft, float in):feet(ft),inches(in){} void display() const { cout<<feet<<"\'-"<<inches<<'\"'; } bool operator<(Distance&) const; }; bool Distance::operator<(Distance& d1) const { float f1 = feet + inches/12; float f2 = d1.feet + d1.inches/12; return (f1<f2)? true : false; } CPS235:Operator Overloading28

Computer Science Department Overloading the Comparison Operators void main() { Distance d1(5, 11.5); Distance d2(6, 2.5); if(d1<d2) { cout<<"the distance";d1.display(); cout<<"is less than";d2.display(); cout<<endl; } else { cout<<"the distance";d1.display(); cout<<"is greater than/equal to"; d2.display(); cout<<endl; } getch(); } CPS235:Operator Overloading29

Computer Science Department Why would we need to make overloaded operators non-member functions? CPS235:Operator Overloading30

Computer Science Department class Time { private: int hours; int mins; public: Time():hours(0),mins(0){} Time(int h, int m):hours(h),mins(m){} void display() { cout<<hours<<"-"<<mins<<endl; } Time operator*(double mult_mins) const { Time result; long totalminutes = (hours * 60) + (mins * mult_mins); result.hours = totalminutes / 60; result.mins = totalminutes % 60; return result; } }; CPS235:Operator Overloading31

Computer Science Department void main() { Time t1; Time t2(5,40); cout<<"t2 is:";t2.display(); t1 = t2 * 2; //t1 = 2 * t2; //ILLEGAL STRUCTURE OPERATION cout<<"t1 is:"; t1.display(); getch(); } CPS235:Operator Overloading32

Computer Science Department Implementing overloaded operators as non-member functions When an overloaded operator is defined as a member function, the left operand is always an object on which the function is called So, in the previous example we have t1 = t2 * 2; // t1.operator*(2) When we write t1 = 2 * t2; the left operand is no longer an object on which the function can be invoked Solution: Implement the overloaded operator function as a non-member function –Since this function needs to access the private data members of the Time class, we declare it as a friend in the Time class declaration CPS235:Operator Overloading33

Computer Science Department Implementing overloaded operators as non-member functions //goes into the declaration of class Time friend Time operator* (double,const Time&); //function defined outside class declaration Time operator*(double mult_mins,const Time& t) { Time result; long totalminutes = (t.hours * 60) + (t.mins * mult_mins); result.hours = totalminutes / 60; result.mins = totalminutes % 60; return result; } CPS235:Operator Overloading34

Computer Science Department Overloading the stream insertion operator << (version 1) Is it possible to do the following? Time t1; cout<<t1; It can be done if we overload the << operator Remember that cout is an object of class ostream If you want to overload the << operator, should it be done via class member function or non-member function? If you use a class member function to overload <<, you would have to write t1 << cout; CPS235:Operator Overloading35

Computer Science Department Overloading the stream insertion operator << (version 1) So, we define the overloaded << operator as a non- member friend function //goes into the declaration of class Time friend void operator<<(ostream&,const Time&); //function defined outside class declaration void operator<<(ostream& os, const Time& t) { os<<t.hours<<"-"<<t.mins<<endl; } CPS235:Operator Overloading36

Computer Science Department Overloading the stream insertion operator << (version 1) With the << operator overloaded as above, the following works fine Time t1; cout<<t1; But if you want to write cout<<t1<<“is the time”<<endl; It will not work! Why? C++ reads the output statement from left to right ((cout<<t1)<<“is the time”)<<endl; CPS235:Operator Overloading37

Computer Science Department Overloading the stream insertion operator << (version 2) The << operator as defined in iostream takes an ostream object to its left The statement cout<<t1 satisfies the above requirement But the output statement also requires that the whole expression (cout<<t1) should be a type ostream object because this expression is to the left of “is the time” You can modify the operator<< to return an ostream object CPS235:Operator Overloading38

Computer Science Department Overloading the stream insertion operator << (version 2) ostream& operator<<(ostream& os, const Time& t) { os<<t.hours<<"-"<<t.mins<<endl; return os; } The statement cout<<t1; becomes the following function call operator<<(cout,trip); And this call returns the cout object CPS235:Operator Overloading39

Computer Science Department Analysis cout<<t1<<“is the time”<<endl; is actually (((cout<<t1)<<“is the time”)<<endl); Invokes the user-defined operator<< that displays t1 and returns the cout object, so the original statement becomes ((cout<<“is the time”)<<endl); Now, the program uses the ostream definition of << for strings to display the string and again returns the cout object. This reduces the statement to (cout<<endl); This also uses the ostream definition of << for endl CPS235:Operator Overloading40

Computer Science Department iostream.h ostream& operator<< (bool& val ); ostream& operator<< (short& val ); ostream& operator<< (unsigned short& val ); ostream& operator<< (int& val ); ostream& operator<< (unsigned int& val ); ostream& operator<< (long& val ); ostream& operator<< (unsigned long& val ); ostream& operator<< (float& val ); ostream& operator<< (double& val ); ostream& operator<< (long double& val ); CPS235:Operator Overloading41

Computer Science Department Returning by Reference int x; int& getnset() { return x; } void main() { getnset() = 56; cout<<"value of x is:"<<getnset(); cout<<“Address is:”<<&getnset(); } CPS235:Operator Overloading42

Computer Science Department Returning by Reference You cannot return a local variable from a function int& getnset() { int x; return x; } //x goes out of scope here You cannot return a constant int& getnset() { return 3; } CPS235:Operator Overloading43

Computer Science Department Returning by Reference You cannot return an expression int& preinc(int& x) { return x++; } CPS235:Operator Overloading44

Computer Science Department What would be the ouput? int& preinc(int& x) { x++; cout<<"\nvalue in func:"<<x<<endl; return x; } void main() { int y = 0; cout<<"\nvalue in main after incrementing once:"<<preinc(y); cout<<"\nAfter calling preinc again"; preinc(y) = 5; cout<<"\nValue in main is:"<<y; getch(); } CPS235:Operator Overloading45

Computer Science Department Compulsory Reading Robert Lafore, Chapter 8: Operator Overloading Deitel and Deitel (5 th edition) –Topics 11.1 – 11.4, 11.6, 11.7 Another useful link: CPS235:Operator Overloading46