© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.

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. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Operator Overloading Fundamentals
計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Chapter 15: Operator Overloading
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Chapter 8 Operator Overloading, Friends, and References.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
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,
OPERATOR OVERLOADING Customised behaviour of operators.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions on Operator Overloading 8.4Operator.
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 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Chapter 10 Operator Overloading; Class string
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Operator Overloading; Class string
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading, Friends, and References
Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading I
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Operator Overloading; String and Array Objects
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions on Operator Overloading 18.4Operator Functions as Class Members vs. as friend Functions 18.5Overloading Stream-Insertion and Stream-Extraction Operators 18.6Overloading Unary Operators 18.7Overloading Binary Operators 18.8Case Study: An Array Class 18.9Converting between Types 18.10Overloading ++ and --

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this chapter, you will learn: –To understand how to redefine (overload) operators to work with new types. –To understand how to convert objects from one class to another class. –To learn when to, and when not to, overload operators. –To study several interesting classes that use overloaded operators. –To create an Array class.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Chapter 16 and 17 –ADT’s and classes –Function-call notation is cumbersome for certain kinds of classes, especially mathematical classes In this chapter –We use C++’s built-in operators to work with class objects

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Operator overloading –Use traditional operators with user-defined objects –Straightforward and natural way to extend C++ –Requires great care When overloading is misused, programs become difficult to understand

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Operator Overloading Use operator overloading to improve readability –Avoid excessive or inconsistent usage Format –Write function definition as normal –Function name is keyword operator followed by the symbol for the operator being overloaded. –operator+ would be used to overload the addition operator ( + )

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Operator Overloading Assignment operator ( = ) –may be used with every class without explicit overloading –memberwise assignment –Same is true for the address operator ( & )

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Restrictions on Operator Overloading Most of C++’s operators can be overloaded

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Restrictions on Operator Overloading

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Restrictions on Operator Overloading Arity (number of operands) cannot be changed –Unary operators remain unary, and binary operators remain binary –Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Restrictions on Operator Overloading No new operators can be created –Use only existing operators Built-in types –Cannot overload operators –You cannot change how two integers are added

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Operator Functions as Class Members vs. as friend Functions Operator functions –Can be member or non-member functions Overloading the assignment operators –i.e: (), [], ->, = –Operator must be a member function

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Operator Functions as Class Members vs. as friend Functions Operator functions as member functions –Leftmost operand must be an object (or reference to an object) of the class –If left operand of a different type, operator function must be a non-member function –A non-member operator function must be a friend if private or protected members of that class are accessed directly

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Operator Functions as Class Members vs. as friend Functions Non-member overloaded operator functions –Enable the operator to be commutative HugeInteger bigInteger1; long int number; bigInteger1 = number + bigInteger1; or bigInteger1 = biginteger1 + number;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Overloading Stream-Insertion and Stream-Extraction Operators Overloaded > operators –Must have left operand of types ostream &, istream & respectively –It must be a non-member function (left operand not an object of the class) –It must be a friend function if it accesses private data members

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_03.cpp (1 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_03.cpp (2 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_03.cpp (3 of 3) Enter phone number in the form (123) : (800) The phone number entered was: (800)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Overloading Unary Operators Overloading unary operators –Avoid friend functions and friend classes unless absolutely necessary. –Use of friend s violates the encapsulation of a class. –As a member function: class String { public: bool operator!() const;... };

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Overloading Binary Operators Overloaded binary operators –Non- static member function, one argument –Non-member function, two arguments class String { public: const String &operator+=( const String & );... }; // end class String y += z; equivalent to y.operator+=( z );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Overloading Binary Operators Example class String { friend const String &operator+=( String &, const String & );... }; // end class String y += z; equivalent to operator+=( y, z );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: An Array class Implement an Array class with –Range checking –Array assignment –Arrays that know their size –Outputting/inputting entire arrays with > –Array comparisons with == and !=

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.h (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.h (2 of 2) array1.cpp (1 of 6)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.cpp (2 of 6)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.cpp (3 of 6)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.cpp (4 of 6)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.cpp (5 of 6)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. array1.cpp (6 of 6) fig18_04.cpp (1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_04.cpp (2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_04.cpp (3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig18_04.cpp (4 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Array output (1 of 1) # of arrays instantiated = 0 # of arrays instantiated = 2 Size of array integers1 is 7 Array after initialization: Size of array integers2 is 10 Array after initialization: Input 17 integers: After input, the arrays contain: integers1: integers2: Evaluating: integers1 != integers2 They are not equal Size of array integers3 is 7 Array after initialization:

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Array output (2 of 2) Assigning integers2 to integers1: integers1: integers2: Evaluating: integers1 == integers2 They are equal integers1[5] is 13 Assigning 1000 to integers1[5] integers1: Attempt to assign 1000 to integers1[15] Assertion failed: 0 <= subscript && subscript < size, file Array1.cpp, line 95 abnormal program termination

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Converting between Types Cast operator –Convert objects into built-in types or other objects –Conversion operator must be a non- static member function. –Cannot be a friend function –Do not specify return type For user-defined class A A::operator char *() const; A::operator int() const; A::operator otherClass() const; –When compiler sees (char *) s it calls s.operator char*()

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Converting between Types The compiler can call these functions to create temporary objects. –If s is not of type char * Calls A::operator char *() const; for cout << s;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Overloading ++ and -- Pre/post-incrementing/decrementing operators –Can be overloaded –How does the compiler distinguish between the two? –Prefix versions overloaded same as any other prefix unary operator would be. i.e. d1.operator++(); for ++d1; Postfix versions –When compiler sees postincrementing expression, such as d1++; Generates the member-function call d1.operator++( 0 ); –Prototype: Date::operator++( int );