Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.

Slides:



Advertisements
Similar presentations
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Advertisements

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.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Case Study - Fractions Timothy Budd Oregon State University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
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.
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.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
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,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
CISC/CMPE320 - Prof. McLeod
Chapter 15: Overloading and Templates
CISC/CMPE320 - Prof. McLeod
The dirty secrets of objects
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading CSCE 121 J. Michael Moore
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
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,
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
CISC/CMPE320 - Prof. McLeod
CISC101 Reminders Assignment 3 due today.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator Overloading

Presentation Schedule – Week 6 Tuesday, Oct. 20 – Basswood, Beech, Cherry, Walnut. Thursday, Oct. 22, Lecture Time – Hickory, Maple, Oak, Birch. Thursday, Oct. 22, Tutorial Time (room TBA) – Poplar, BalsamFir, Spruce, Cedar. Friday, Oct. 23 – Hemlock, JackPine, Tamarack, WhitePine. Fall 2015CISC/CMPE320 - Prof. McLeod2

Presentation Schedule – Week 6, Cont. Plan on 10 minutes. Few questions? You must fill out peer evaluations in Moodle on the team’s Wiki and on their presentation – but not for your own team! Attendance sheets will be circulated – if you are not there, you cannot evaluate the presentation! Fall 2015CISC/CMPE320 - Prof. McLeod3

Fall 2015CISC/CMPE320 - Prof. McLeod4 Team Presentations, Cont. Talking to upper management. Describe your project, its scope, the inherent challenge. You have lots of material from your RAD. Describe how you intend to complete your project: –Team role assignments –Time line (what you will do when) What you have done so far. Prototypes? Minimum one person to present (!), but everyone on the team should have a chance to review and comment on or edit the presentation.

Fall 2015CISC/CMPE320 - Prof. McLeod5 Operator Overloading – Java You should remember that the binary + operator is overloaded in Java: is 5 “two” + “three” is “twothree” 2 + “three” is “2three” “two” + 3 is “two3” So the result of the operator depends on the type of the operands. If there is a String literal on one side then the other side is converted to a String and a concatenation takes place.

Fall 2015CISC/CMPE320 - Prof. McLeod6 + and string Objects in C++ string two("two"); string three("three"); string sum1 = two + three; // sum1 is "twothree" string sum2 = 2 + three; // doesn’t work Note that a string literal is actually a char* object and the + operator does not work with them if they are on both sides of the operator.

Fall 2015CISC/CMPE320 - Prof. McLeod7 + and string Objects in C++, Cont. However, something like: string two("two"); string sum1 = two + "five"; does work. The char* literal is converted to a string object first. So, the operation that takes place depends on the types of the operands on either side of the operator.

Operator Overloading in string Class See cplusplus.com for list of overloaded operators in this class: = [ ] += + == != >= >> << Fall 2015CISC/CMPE320 - Prof. McLeod8

Fall 2015CISC/CMPE320 - Prof. McLeod9 Operator Overloading This flexibility is a result of operator overloading in the string class – a task carried out by the class designer to make his class easier to use. In Java operator overloading is almost non- existent. For example, if a, b and c are BigIntegers, to carry out the operation: x = a + b * c; you would have to write x = a.add(b.multiply(c));

Fall 2015CISC/CMPE320 - Prof. McLeod10 Overloadable Operators in C++ +-*/%^& |~!=<>+= -=*=/=%=^=&=|= <<>>>>=<<===!=<= >=&&||++--->*, ->[]()newnew[] deletedelete[]

Fall 2015CISC/CMPE320 - Prof. McLeod11 Operator Functions You can overload an operator as a member or a non- member function. Overloading function header syntax: return_type operatoroperator_symbol(parameters) For example, non-member overloading the + operator for an object called MyClass : MyClass operator+(const MyClass& lhs, const MyClass& rhs);

Fall 2015CISC/CMPE320 - Prof. McLeod12 Operator Functions, Cont. Member overloading functions: For example, overloading the + operator for an object called MyClass : MyClass operator+(const MyClass& rhs) const; The object owning the function is the LHS operand in this case.

MyComplex Class Demo See the start of a simple class to hold complex numbers: myComplex.h and myComplex.cpp: First version uses non-member overloading and accessors. Second version uses member overloading of the + operator. Third version uses non-member friend s and no accessors. Fall 2015CISC/CMPE320 - Prof. McLeod13

Version 1, Observations Overloaded binary operator should return a copy of the value, not a reference! (How does operator<< get away with returning a reference?) Non-member overloadings must have accessors to get at attributes. Used the single parameter constructor as conversion constructor to allow mixed type expressions with int or double on either side. Nice! What would you have to do if this did not work this way? Fall 2015CISC/CMPE320 - Prof. McLeod14

Version 2, Observations Member overloading does not need accessors. Current object is LHS of binary operation. Parameter is RHS. Mixed type expressions cannot be evaluated if LHS is not a MyComplex object. Fall 2015CISC/CMPE320 - Prof. McLeod15

Version 3, Observations “friend” functions are declared in the scope of the class but are implemented like non-member functions. They can see private members of the class. Fall 2015CISC/CMPE320 - Prof. McLeod16

Summary of MyComplex, So Far Mixed type expressions (using your object) need conversion constructors, that will be invoked automatically to match types before the operator is evaluated. If you are going to use your binary operators in mixed type expressions it is best to overload them as non-member functions. If you do not wish to provide accessors, then non- member functions will have to be declared as friend s. So, we know how to overload the binary arithmetic operators and <<. Fall 2015CISC/CMPE320 - Prof. McLeod17

Fall 2015CISC/CMPE320 - Prof. McLeod18 Member or Non-Member Overloading? You will probably need to use both kinds. For example, assignment operators must be member functions, because you need to modify the LHS. Non-member functions cannot access private things in the class, unless they are a friend function.

Fall 2015CISC/CMPE320 - Prof. McLeod19 Overloading Arithmetic Operators Binary: + - * / and % –Easy to do. Usually a non-member function will be best if you can use accessors for the private stuff (or are a friend function). Unary: - * and & (negation, pointer de-reference, address-of operator). –Also easy. If a non-member function, you only need a single parameter. A member function does not need any.

Fall 2015CISC/CMPE320 - Prof. McLeod20 friend Functions, Summary (Classes can also be declared friend s.) A function that is declared as a friend function can be implemented as a non-member function but can still access and change private data fields. The function prototype must be inside the class definition (before the }; )

Fall 2015CISC/CMPE320 - Prof. McLeod21 Overloading Boolean Operators ==,, =, != Normally a non-member function. Return a bool. Consider writing a member function called something like “compare” that returns an int. compare will take two objects and return a negative int if the first is less than the second, zero if they are equal and a positive int if the first is greater than the second. The other comparison operators can invoke compare.

Fall 2015CISC/CMPE320 - Prof. McLeod22 Overloading Input and Output Stream output: << This operator takes the RHS, adds it to the stream obtained as the LHS and then returns this stream. As a non-member function: ostream& operator<<(ostream& out, const MyClass myC) { out << myC.convert(); return out; } convert() changes myC to something that can normally be handled by << (a fundamental type, a string or a *char ).

Fall 2015CISC/CMPE320 - Prof. McLeod23 Overloading Input and Output, Cont. Stream input: >> As for output except you use istream& instead of ostream&.

Aside – Converting Between Strings and Numbers Applies to C++98: Use the stringstream stream ( #include ) See StringNumberConversion.cpp These functions also provide a “pre-look” at how to code with templates. (Like “generics” in Java). Gets around the problem of having to write separate functions for each numeric type. Avoids the use of C functions like atof() and atoi() in. Fall 2015CISC/CMPE320 - Prof. McLeod24

Converting Between Strings and Numbers, Cont. Should be much nicer now in C++11 The library has built-in functions for conversions in both directions. See the library docs. The current distro. (4.8.1) of MinGW still does not support the string conversion functions. But you can download a patch: And follow the instructions on this site. This: says that the bug is fixed for 64 bit versions of MinGW only. Fall 2015CISC/CMPE320 - Prof. McLeod25