1 Chapter 8 Operator Overloading, Friends, and References.

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.
1 Overloading Operators COSC 1567 C++ Programming Lecture 7.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
© 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.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.
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 in C++
Review of C++ Programming Part II Sheng-Fang Huang.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
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:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
1 C++ Syntax and Semantics, and the Program Development Process.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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.
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.
 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.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 CSC241: Object Oriented Programming Lecture No 08.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Operator Overloading Introduction
COMP 53 – Week Two Operator Overloading.
Learning Objectives Pointers as dada members
Chapter 14: More About Classes.
Introduction to C++ Systems Programming.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Operator Overloading CMSC 202.
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Constructors and Other Tools
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Presentation transcript:

1 Chapter 8 Operator Overloading, Friends, and References

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 References and More Overloading – > –Operators: =, [], ++, --

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

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"!

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

6 Overloaded "+" Given previous example: –Note: overloaded "+" NOT member function –Definition is "more involved" than simple "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

7 Money "+" Definition: Display 8.1 Operator Overloading Definition of "+" operator for Money class:

8 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)

9 Overloaded "==" for Money: Display 8.1 Operator Overloading Definition of "==" operator for Money class:

10 Constructors Returning Objects Constructor a "void" function? –We "think" that way, but no –A "special" function With special properties CAN return a value! Recall return statement in "+" overload for Money type: –return Money(finalDollars, finalCents); Returns an "invocation" of Money class! So constructor actually "returns" an object! Called an "anonymous object"

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… 

12 Returning by non-const Value Consider "no const" in declaration: Money operator +(const Money& amount1, const Money& amount2); Consider expression that calls: m1 + m2 –Where m1 & m2 are Money objects –Object returned is Money object –We can "do things" with objects! Like call member functions…

13 What to do with Non- const Object Can call member functions: –We could invoke member functions on object returned by expression m1+m2: (m1+m2).output(); //Legal, right? –Not a problem: doesn’t change anything (m1+m2).input(); //Legal! –PROBLEM! //Legal, but MODIFIES! Allows modification of "anonymous" object! Can’t allow that here! So we define the return object as const

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

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

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

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

18 Overloading as Member Functions 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

19 1.include 2.#include 3.#include 4.using namespace std; 5.//Class for amounts of money in U.S. currency. 6.class Money 7.{ 8.public: 9. Money( ); 10. Money(double amount); 11. Money(int dollars, int cents); 12. Money(int dollars); 13. double getAmount( ) const; 14. int getDollars( ) const; 15. int getCents( ) const; 16. void input( ); //Reads the dollar sign as well as the amount number. 17. void output( ) const; 18. const Money operator +(const Money& amount2) const; 19. const Money operator -(const Money& amount2) const; 20. bool operator ==(const Money& amount2) const; 21. const Money operator -( ) const; 22.private: 23. int dollars; //A negative amount is represented as negative dollars and 24. int cents; //negative cents. Negative $4.50 is represented as -4 and int dollarsPart(double amount) const; 26. int centsPart(double amount) const; 27. int round(double number) const; 28.};

20 1.const Money Money::operator +(const Money& secondOperand) const 2.{ 3. int allCents1 = cents + dollars*100; 4. int allCents2 = secondOperand.cents + secondOperand.dollars*100; 5. int sumAllCents = allCents1 + allCents2; 6. int absAllCents = abs(sumAllCents); //Money can be negative. 7. int finalDollars = absAllCents/100; 8. int finalCents = absAllCents%100; 9. if (sumAllCents < 0) 10. { 11. finalDollars = -finalDollars; 12. finalCents = -finalCents; 13. } 14. return Money(finalDollars, finalCents); 15.} 16.const Money Money::operator -(const Money& secondOperand) const 17.// The rest of this definition is omitted 18.bool Money::operator ==(const Money& secondOperand) const 19.{ 20. return ((dollars == secondOperand.dollars) 21. && (cents == secondOperand.cents)); 22.} 23.const Money Money::operator -( ) const 24.{ 25. return Money(-dollars, -cents); 26.}

21 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 definition: –const Money operator +(const Money& amount); –Notice only ONE argument

22 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

23 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 in chapter…)

24 Overloading Function Application () Function call operator, ( ) –Must be overloaded as member function –Allows use of class object like a function –Can overload for all possible numbers of arguments Example: Aclass anObject; anObject(42); If ( ) overloaded  calls overload

25 Other Overloads &&, ||, and comma operator –Predefined versions work for bool types –Recall: use "short-circuit evaluation" –When overloaded no longer uses short-circuit Uses "complete evaluation" instead Contrary to expectations Generally should not overload these operators

26 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 overhead, more efficient So: best to make nonmember operator overloads friends!

27 Friend Functions Friend function of a class –Not a member function –Has direct access to private members Just as member functions do Use keyword friend in front of function declaration –Specified IN class definition –But they’re NOT member functions!

28 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

29 Friend Function Purity Friends not pure? –"Spirit" of OOP dictates all operators and functions be member functions –Many believe friends violate basic OOP principles Advantageous? –For operators: very! –Allows automatic type conversion –Still encapsulates: friend is in class definition –Improves efficiency

30 Friend Classes Entire classes can be friends –Similar to function being friend to class –Example: class F is friend of class C All class F member functions are friends of C NOT reciprocated Friendship granted, not taken Syntax: friend class F –Goes inside class definition of "authorizing" class

31 1.include 2.#include 3.#include 4.using namespace std; 5.//Class for amounts of money in U.S. currency. 6.class Money 7.{ 8.public: 9. Money( ); 10. Money(double amount); 11. Money(int dollars, int cents); 12. Money(int dollars); 13. double getAmount( ) const; 14. int getDollars( ) const; 15. int getCents( ) const; 16. void input( ); //Reads the dollar sign as well as the amount number. 17. void output( ) const; 18. friend const Money operator +(const Money& amount2) const; 19. friend const Money operator -(const Money& amount2) const; 20. friend bool operator ==(const Money& amount2) const; 21. friend const Money operator -( ) const; 22.private: 23. int dollars; //A negative amount is represented as negative dollars and 24. int cents; //negative cents. Negative $4.50 is represented as -4 and int dollarsPart(double amount) const; 26. int centsPart(double amount) const; 27. int round(double number) const; 28.};

32 1.const Money Money +(const Money& amount1, const Money& amount2) 2.{ 3. int allCents1 = amount1.cents + amount1. dollars*100; 4. int allCents2 = amount2..cents + amount2..dollars*100; 5. int sumAllCents = allCents1 + allCents2; 6. int absAllCents = abs(sumAllCents); //Money can be negative. 7. int finalDollars = absAllCents/100; 8. int finalCents = absAllCents%100; 9. if (sumAllCents < 0) 10. { 11. finalDollars = -finalDollars; 12. finalCents = -finalCents; 13. } 14. return Money(finalDollars, finalCents); 15.}

33 friend classes 一個 class A 的定義中,亦可宣告 class B 為其 friend ,如此一來, class B 中的 member function f(A &a) 若有引數為 class A 的物件,則 於函數 f() 的定義中就可以直接引用 class A 的 private elements 。 這好像是 class A 宣告 class B 為其 好朋友,因 此, class B 就可以分享 class A 的資源。 由下例 來看如何使用 friend class 的功能。

34 #include class A { public: A(int r=0){x=r;} ~A(){} friend class B; private: int x; }; class B { public: B(int r=0){y=r;} ~B(){} int g(A &a){ return a.x+y;} private: int y; }; main() { A a(3); B b(12); cout<< "g(3) = "<< b.g(a)<< endl; } 說明: 1. 於 class A 中宣告 class B 為其 friend 。 2.class B 的 member function g 可以自由引用 class A 的 private elements , 如 a.x 。 3. 該程式的執行結果為 g(3) = 15

35 References Reference defined: –Name of a storage location –Similar to "pointer" Example of stand alone reference: int robert; int& bob = robert; bob is reference to storage location for robert Changes made to bob will affect robert Confusing?

36 References Usage Seemingly dangerous Useful in several cases: –Call-by-reference Often used to implement this mechanism –Returning a reference Allows operator overload implementations to be written more naturally Think of as returning an "alias" to a variable

37 Returning Reference Syntax: double& sampleFunction(double& variable); –double& and double are different –Must match in function declaration and heading Returned item must "have" a reference –Like a variable of that type –Cannot be expression like "x+5" Has no place in memory to "refer to"

38 Returning Reference in Definition Example function definition: double& sampleFunction(double& variable) { return variable; } Trivial, useless example Shows concept only Major use: –Certain overloaded operators

39 Overloading >> and << Enables input and output of our objects –Similar to other operator overloads –New subtleties Improves readability –Like all operator overloads do –Enables: cout > myObject; –Instead of need for: myObject.output(); …

40 Overloading >> Insertion operator, << –Used with cout –A binary operator Example: cout << "Hello"; –Operator is << –1 st operand is predefined object cout From library iostream –2 nd operand is literal string "Hello"

41 Overloading >> Operands of >> –Cout object, of class type ostream –Our class type Recall Money class –Used member function output() –Nicer if we can use >> operator: Money amount(100); cout << "I have " << amount << endl; instead of: cout << "I have "; amount.output()

42 Overloaded >> Return Value Money amount(100); cout << amount; –<< should return some value –To allow cascades: cout << "I have " << amount; (cout << "I have ") << amount; Two are equivalent What to return? –cout object! Returns it’s first argument type, ostream

43 Overloaded >> Example: Display 8.5 Overloading > (1 of 5)

44 Overloaded >> Example: Display 8.5 Overloading > (2 of 5)

45 Overloaded >> Example: Display 8.5 Overloading > (3 of 5)

46 Overloaded >> Example: Display 8.5 Overloading > (4 of 5)

47 Overloaded >> Example: Display 8.5 Overloading > (5 of 5)

48 Assignment Operator, = Must be overloaded as member operator Automatically overloaded –Default assignment operator: Member-wise copy Member variables from one object  corresponding member variables from other Default OK for simple classes –But with pointers  must write our own!

49 Increment and Decrement Each operator has two versions –Prefix notation: ++x; –Postfix notation: x++; Must distinguish in overload –Standard overload method  Prefix –Add 2d parameter of type int  Postfix Just a marker for compiler! Specifies postfix is allowed

50 Overload Array Operator, [ ] Can overload [ ] for your class –To be used with objects of your class –Operator must return a reference! –Operator [ ] must be a member function!

51 Summary 1 C++ built-in operators can be overloaded –To work with objects of your class Operators are really just functions Friend functions have direct private member access Operators can be overloaded as member functions –1 st operand is calling object

52 Summary 2 Friend functions add efficiency only –Not required if sufficient accessors/mutators available Reference "names" a variable with an alias Can overload > –Return type is a reference to stream type