Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 8 Operator Overloading, Friends, and References.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
© 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 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Operator Overloading in C++
IT PUTS THE ++ IN C++ Object Oriented Programming.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Friends, Overloaded Operators, and Arrays in Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 Chapter 8 Operator Overloading, Friends, and References.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Operator Overloading.
Operator Overloading Introduction
COMP 53 – Week Two Operator Overloading.
Learning Objectives Pointers as dada members
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Chapter 5 Classes.
Operator Overloading BCA Sem III K.I.R.A.S.
The dirty secrets of objects
Advanced Program Design with C++
Constructors and Other Tools
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
Andy Wang Object Oriented Programming in C++ COP 3330
Presentation transcript:

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345 – Advanced Program Design with C++ Lecture 8: Operator overloading

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-2 Learning Objectives  Basic Operator Overloading  Unary operators  As member functions  Friends and Automatic Type Conversion  Friend functions, friend classes  Constructors for automatic type conversion

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-3 Operator Overloading Introduction  Operators +, -, %, ==, etc.  Really just functions!  Simply "called" with different syntax: x + 7  "+" is binary operator with x & 7 as operands  We "like" this notation as humans  Think of it as: +(x, 7)  "+" is the function name  x, 7 are the arguments  Function "+" returns "sum" of it’s arguments

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-4 Operator Overloading Perspective  Built-in operators  e.g., +, -, =, %, ==, /, *  Already work for C++ built-in types  In standard "binary" notation  We can overload them!  To work with OUR types!  To add "Chair types", or "Money types"  As appropriate for our needs  In "notation" we’re comfortable with  Always overload with similar "actions"!

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-5 Overloading Basics  Overloading operators  VERY similar to overloading functions  Operator itself is "name" of function  Example Declaration: const Money operator +(const Money& amount1, const Money& amount2);  Overloads “+” for operands of type Money  Uses constant reference parameters for efficiency  Returned value is type Money  Allows addition of "Money" objects

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-6 Overloaded "+"  Given previous example:  Note: overloaded "+" not a member function  Definition is "more evolved" than number "add"  Requires issues of Money type addition  Must handle negative/positive values  Operator overload definitions generally very simple  Just perform "addition" particular to "your" type

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-7 Overloading as external operator  The operator is overloaded as a “free function is defined, i.e. outside of the class:  Note: no Money:: in the operator definition header, as the operator is not a member of the Money class class Money { public: … // no declaration of + operator in the class … } const Money operator +(const Money m1, const Money m2) {…}

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-8 Money "+" Definition: Display 8.1 Operator Overloading  Definition of "+" operator for Money class:

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-9 Overloaded "=="  Equality operator, ==  Enables comparison of Money objects  Declaration: bool operator ==(const Money& amount1, const Money& amount2);  Returns bool type for true/false equality  Again, it’s a non-member function (like "+" overload)

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-10 Overloaded "==" for Money: Display 8.1 Operator Overloading  Definition of "==" operator for Money class:

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-11 Returning by const Value  Consider "+" operator overload again: const Money operator +(const Money& amount1, const Money& amount2);  Returns a "constant object“?  Why?  Consider impact of returning "non-const" object to see… 

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-12 Returning by non-const Value  Consider "no const" in declaration: Money operator +(const Money& amount1, const Money& amount2);  Consider the expression: m1 + m2  Where m1 and m2 are Money objects  Calls the operator declared above  Object returned is Money object  We can "do things" with objects!  Like call member functions…

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-13 What to do with Non-const Object  Can call member functions:  We could invoke member functions on the object returned by expression m1+m2:  (m1+m2).output(); //Legal, right?  Not a problem: doesn’t change anything in the (m1+m2) object  (m1+m2).input(); //Legal!  PROBLEM! //Legal, but MODIFIES the (m1+m2) object!  Allows modification of "anonymous" object!  Can’t allow that here!  Even more:  (m1 + m2) = m3; //Legal!!!  So we define the return object as const to avoid these problems  Ideally:  const Money operator +(const Money&, const Money&) const;

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-14 Overloading Unary Operators  C++ has unary operators:  Defined as taking one operand  e.g., - (negation)  x = -y; // Sets x equal to negative of y  Other unary operators:  ++, --  Unary operators can also be overloaded

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-15 Overload "-" for Money  Overloaded "-" function declaration  Placed outside class definition: const Money operator –(const Money& amount);  Notice: only one argument  Since only 1 operand (unary)  "-" operator is overloaded twice!  For two operands/arguments (binary)  For one operand/argument (unary)  Definitions must exist for both

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-16 Overloaded "-" Definition  Overloaded "-" function definition: const Money operator –(const Money& amount) { return Money(-amount.getDollars(), -amount.getCents()); }  Applies "-" unary operator to built-in type  Operation is "known" for built-in types  Returns anonymous object again

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-17 Overloaded "-" Usage  Consider: Money amount1(10), amount2(6), amount3; amount3 = amount1 – amount2;  Calls binary "-" overload amount3.output();//Displays $4.00 amount3 = -amount1;  Calls unary "-" overload amount3.output()//Displays -$10.00

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-18 Overloading as Member operator  Previous examples: standalone functions  Defined outside a class  Can overload as "member operator"  Considered "member function" like others  When operator is member function:  Only ONE parameter, not two!  Calling object serves as 1 st parameter

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-19 Overloading as Member operator  Declared and defined as a member function, with operator declaration syntax:  Note: operator is declared as const : it is not changing the values of the data members of its calling object (not necessary but informative and precautious). class Money { public: … const Money operator +(const Money m1) const; … } const Money Money::operator +(const Money m1) const {…}

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-20 Member Operator in Action  Money cost(1, 50), tax(0, 15), total; total = cost + tax;  If "+" overloaded as member operator:  Variable/object cost is calling object  Object tax is single argument  Think of as: total = cost.+(tax);  Declaration of "+" in class declaration:  const Money operator +(const Money& amount);  Notice only ONE argument

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-21 const Functions  When to make function const ?  Constant functions not allowed to alter class member data  Constant objects can ONLY call constant member functions  Good style dictates:  Any member function that will NOT modify data should be made const  Use keyword const after function declaration and heading  const Money operator +(const Money&, const Money&) const;

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-22 Overloading Operators: Which Method?  Object-Oriented-Programming  Principles suggest member operators  Many agree, to maintain "spirit" of OOP  Member operators more efficient  No need to call accessor & mutator functions  At least one significant disadvantage  (Later…)

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-23 Friend Functions  Nonmember functions  Recall: operator overloads as nonmembers  They access data through accessor and mutator functions  Very inefficient (overhead of calls)  Friends can directly access private class data  No need to use accessors/mutators, no overhead, more efficient  So: best to make nonmember operator overloads as friend operators

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-24 Friend Functions  Friend function of a class  Not a member function  Has direct access to private members  Just as member functions do  In function declaration (inside the class):  Use keyword friend in front of function declaration  Specified IN class definition  But they’re NOT member functions!  Even if members are declared as private or protected, they are considered public from the perspective of the friend  In function definition:  Don’t use keyword friend  Don’t use ClassName:: in front of function name: it is not a member of ClassName

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-25 Friend Function Uses  Operator Overloads  Most common use of friends  Improves efficiency  Avoids need to call accessor/mutator member functions  Operator must have access anyway  Might as well give full access as friend  Friends can be any function  If a member function is declared as friend, it must be accessible

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-26 Friend function example // friend functions #include using namespace std; class CRectangle { int width, height; public: void set_values (int, int); int area () {return (width * height);} friend CRectangle duplicate (CRectangle); }; void CRectangle::set_values (int a, int b) { width = a; height = b; } CRectangle duplicate (CRectangle rectparam) { CRectangle rectres; rectres.width = rectparam.width*2; rectres.height = rectparam.height*2; return (rectres); } int main () { CRectangle rect, rectb; rect.set_values (2,3); rectb = duplicate (rect); cout << rectb.area(); return 0; }

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-27 Friend class  A class B can be declared as friend to another class A  Allows B to freely access all members of class A  Friend is widely considered a “rogue feature” of C++  It conflicts with information hiding  Complexifies inheritance

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-28 Friend class example class CSquare; class CRectangle { int width, height; public: int area (){ return (width * height); } void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) { side=a; } friend class CRectangle; }; void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4); rect.convert(sqr); cout << rect.area(); return 0; }

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-29 Friend operators: automatic type conversion  In case of member-defined operators:  Checks for a Money member to call on baseAmount :  Money Money::operator + (int)  If not, we might have a constructor Money(int) that will be called to translate 25 into $ If not: compilation error. Money baseAmount(100,60), fullAmount fullAmount = baseAmount + 25; // problem? fullAmount.output; // $125.60

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-30 Friend operators: automatic type conversion  However:  As operator “+” of int is certainly not overloaded for a Money object parameter, 25 cannot be a calling object, and type conversion using a constructor does not work for calling objects.  This is a great disadvantage of overloading binary operators as class members  Solution: overload the operator as a friend Money baseAmount(100,60), fullAmount; fullAmount = 25 + baseAmount; // problem!! fullAmount.output; // $125.60

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-31 Operator overloading as a friend  Overload the operator externally, then include it as a friend:  Note: no Money:: and no friend in the definition.  This way, both operands can be automatically converted using constructors (as the left operand is not a calling object), and the operator can refer to private data members in its operation (thus saving computation time by not using accessors/mutators). class Money { public: … friend const Money operator +(const Money m1, const Money m2); … } const Money operator +(const Money m1, const Money m2){…}

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-32 Operator overloading: 3 ways  As non-member non-friend  Defined as external to the class  Not related to the class, except by its operands type  Implementation code cannot refer to private members of its operands, leading to inefficient implementation through the use of accessors/mutators.  Has two declared operands and no calling object  May have type conversion for both of its operators  As member  Defined as a member of the class  May use data members, as the operator is a member of the class  Is clear that the operator belongs to the class (as opposed to non-member)  Best from the point of view of OO principles, but…  Used as the left parameter being the calling object of the operator i.e. o1 + o2 =~= o1.+(o2)  Suffers from lack of type conversion of the left operand  As friend  Defined as external to the class, but introduced inside the class as a friend  At least, it is mentioned in the class declaration… though it is not a member.  May use private data members, thus more efficient than non-member non-friend  Has two declared operands and no calling object  May have type conversion for both of its operators

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-33 Summary 1  C++ built-in operators can be overloaded  To work with objects of your class  Operators are really just functions that can be called in infix mode i.e. a+b instead of +(a,b)  Friend functions have direct private member access  Operators can be overloaded as member functions  1 st operand is calling object

Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-34 Summary 2  Friend external operators add efficiency only  Not required if sufficient accessors/mutators available, though inefficient  Use const return type, unless in exceptional cases.