C++ 程序语言设计 Chapter 11: Operator Overloading. Outline  Operator overloading  Using of friends  Overload “<<” operator.

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Chapter 13: Overloading.
Chapter 15: Operator Overloading
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
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.
Operator Overloading and Type Conversions
Operator Overloading Customised behaviour of operators Unit - 06.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
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.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
C++ 程序语言设计 Chapter 8: Inline Functions. Macro  like a function without function overhead  implemented with the preprocessor instead of the compiler.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
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. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Session 03 - Templates Outline 03.1Introduction 03.2Function Templates 03.3Overloading Template Functions 03.4Class Templates 03.5Class Templates and Non-type.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Overloading C++ supports the concept of overloading Two main types
《Visual C++程序设计》 软件与通信工程学院 李 刚 QQ: 上课时间:周二567节 上课地点:荟庐W101
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Constructor & Destructor
Operator Overloading BCA Sem III K.I.R.A.S.
The dirty secrets of objects
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
Presentation transcript:

C++ 程序语言设计 Chapter 11: Operator Overloading

Outline  Operator overloading  Using of friends  Overload “<<” operator

thinking about “*” and “+”  How many methods to use “*”? 5 * 3; int* point;  How about operator “+”? int i, j, k; k = i + j; int i[10], j[10], k[10]; K = i + j;//is it right?, how to do it?

operator and function call  another way to make a function call  differences between operator and function call: The syntax is different The compiler determines which “function” to call. double i, j, k; k = i + j; float i, k; int j; k = i + j;

add arrays each other  Normal method for(int num = 0; num < 10; num ++) { k[num] = i[num] + j[num]; }  Define a class for array, use member function addsum() objK = objI.addsum(objJ);  Overload “+”operator objK = objI + objJ;

Warning & reassurance  make the code involving your class easier to write and especially easier to read.  Make your operator overloading reasonable.  All the operators used in expressions that contain only built-in data types cannot be changed. “ 1 << 4”  Only an expression containing a user- defined type can have an overloaded operator.

Syntax represents the operator that’s being overloaded The number of arguments in the overloaded operator’s argument list depends on two factors:  Whether it’s a unary operator or a binary operator.  Whether the operator is defined as a global function or a member function.

Syntax class Salesperson{ float money; Salesperson operator+(const Salesperson &) const; } district2, sid, sara; district2 = sid + sara; district2 = sid.operator+(sara);

Example-count time 5 hours, 40 minutes + 2 hours, 55 minutes = 8 hours, 35minutes See the file: time0.*, time1.*, time2.*

the restrictive use  cannot combine operators that currently have no meaning (such as ** )  cannot change the evaluation precedence of operators  cannot change the number of arguments required by an operator  至少必须有一个操作数是用户定义类型

the restrictive use  不能违反运算符原来的语法规则  不能重载下面的操作符: sizeof 、. 、.* 、 :: 、 ?: 、 const_cast 、 dynamic_cast 、 reinterpret_const 、 static_cast  下列运算符只能通过成员函数进行重载 = 、 () 、 [] 、 ->

Operators can be overloaded +-*/%& |-=!=<> +=/=%=^=&=|= <<>>==!=<=>= &&||++--->->* ()[],<<=>>=new deletenew[]delete[]*=

Using of friends A = B * 2.75 => A = B.operator*(2.75) A = 2.75 * B => A = operator*(2.75, B)

Using of friends friend Time operator*(double, const Time&); Time operator*(double mult, const Time& t) { Time result; long totalminutes = t.hours * mult * 60 + t.minutes * mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; return result; }

Overload “<<” operator  设 trip 是一个 Time 对象,显示 Time 的值: trip.Show(); cout << trip;  cout << “hello world” << endl;  必须使用友元函数,否则 trip << cout; void operator<<(ostream& os, const Time& t){ os << t.hours << " hours, " << t.minutes << " minutes" << endl; }  cout << “trip time: ” << trip << “!”;

成员函数与非成员函数的比较  对于多数运算符,可以选择使用成员函数和 非成员的友元函数实现运算符重载。 Time operator+(const Time&) const; friend Time operator+ (const Time& , const Time&); A = B + C => A = B.operator+(C) A = operator+(B, C)

Non-member operators Basic guidelines OperatorRecommended use All unary operatorsmember = ( ) [ ] –> –>*must be member += –= /= *= ^= &= |= %= >>= <<= member All other binary operatorsnon-member

Unary operators  the syntax to overload all the unary operators See the file: OverloadingUnaryOperators.cpp

Increment & decrement  friend function ++a => operator++(a) a++ => operator++(a, int)  member function ++b => B::operetor++() b++ => B::operator++(int)

Binary operators  the syntax to overload all the Binary operators See the file: IntegerTest.cpp, ByteTest.cpp

self-assignment  all of the assignment operators have code to check for self-assignment  The most important place to check for self-assignment is operator= because with complicated objects disastrous results may occur  handle a single type, also possible to handle mixed types

Arguments & return values  function argument not change : const reference left-hand argument : not const const member function  the type of return value depends on the expected meaning of the operator returned by value as a const

Arguments & return values  all the assignment operators modify the lvalue, the return value for all of the assignment operators should be a nonconst reference to the lvalue  the logical operators get at worst an int back, and at best a bool

Arguments & return values  increment and decrement operators Understand the meanings of them Both of them are const ++a (a pre-increment), it generates a call to operator++(a); but when it sees a++, it generates a call to operator++(a, int).

Arguments & return values  return by value as const Matching the temporary object. f(a+b) When you send message to return value, can just call the const member function. (a+b).g( )

the return optimization  return Integer(left.i + right.i); building the object directly into the location of the outside return value. requires only a single ordinary constructor call no copy-constructor no destructor call  Integer tmp(left.i + right.i);return tmp; created the tmp object the copy-constructor copies the tmp to the location of the outside return value call the destructor

Unusual operators  The subscript operator[ ] must be a member function and it requires a single argument the object it’s being called for acts like an array often return a reference be conveniently used on the left-hand side of an equal sign

Unusual operators  Operator-> used when you want to make an object appear to be a pointer must be a member function additional, atypical constraints :  must return an object (or reference to an object) that also has a pointer dereference operator  must return a pointer that can be used to select what the pointer dereference operator arrow is pointing at see the file :SmartPointer.cpp

Unusual operators  Operator->* provided for those situations to mimic the behavior provided by the built-in pointer-to-member syntax must first create a class with an operator( ) see the file :PointerToMemberOperator.cpp

Overloading assignment  Understand the assignment in C++ MyType b; MyType a = b; a = b; initializing an object using an =,copy- constructor will be called. See CopyingVsInitialization.cpp  When dealing with the = sign If the object hasn’t been created yet, initialization is required otherwise the assignment operator= is used.

Behavior of operator= member function  = can be only a member function. objA = 5; 5 = objA;  must copy all of the necessary information from the right-hand object into the current object. See SimpleAssignment.cpp self-assignment  Avoid situation of self-assignment.  Pointers in classes if the object contains pointers to other objects. Simply copying a pointer means that you’ll end up with two objects pointing to the same storage location. You must deal with this things.

Behavior of operator=  two common approaches to this problem: 1. copy whatever the pointer refers to when you do an assignment or a copy-construction. see : CopyingWithPointers.cpp 2. Reference Counting copy-construction or assignment means attaching another pointer to an existing object and incrementing the reference count.  Save the memory and reduce the initialization overhead.  Destruction means Reduce the reference count destroying the object if reference count = 0; copy-on-write Use copy-on-write. See ReferenceCounting.cpp

Behavior of operator=  Automatic operator= creation Compiler create a = if you do not make one. See :AutomaticOperatorEquals.cpp Suggestions: 1.Make = by yourself. 2.If you donot want to use it. declare operator= as a private function.

Automatic type conversion  In C /C++, compiler can auto convert type for expression and function call.  In C++, define auto type conversion functions for class to convert type. 1.Particular type of constructor 2.Overloaded operator.  Constructor conversion a constructor single argument an object (or reference) of another type See : AutomaticTypeConversion.cpp

Automatic type conversion Benefits Benefits: avoid that define two overloaded versions of f( ). Defect Defect: hidden the constructor call to Two  Preventing constructor conversion sometimes constructor conversion can cause problems to turn it off use the keyword : explicit Means do not use the constructor to do auto conversion It is only to specify constructor. See : ExplicitKeyword.cpp

Automatic type conversion  Operator conversion Use operator to define a overloading operator. operator followed by the type you want to convert to. It is unique for there is not a return type. The return is the name you’re overloading. Maybe some compiles do not support it. See : OperatorOverloadingConversion.cpp

Automatic type conversion  Reflexivity One of the most convenient reasons to use global overloaded operators is for : automatic type conversion may be applied to either operand. Member function can only to right hand operand. left- hand operand must already be the proper type. See :ReflexivityInOverloading.cpp “1-1;”???  Type conversion example see : Strings1.cpp Strings2.cpp

Automatic type conversion  Pitfalls in automatic type conversion You should design your conversions correctly. otherwise  compiler must choose how to quietly perform a type conversion  There are two ways: 1. Constructor. 2. Operator. see : TypeConversionAmbiguity.cpp TypeConversionFanout.cpp

Automatic type conversion  Hidden activities Automatic type conversion can introduce more underlying activities than you may expect. see : CopyingVsInitialization2.cpp be award of the synthesized auto calls by compiler The default constructor copy-constructor operator= destructor.  Automatic type conversion should be used carefully

next… Dynamic Object Creation thanks!