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
Operator Overloading Fundamentals
Advertisements

Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
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.
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
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++
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
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.
Case Study - Fractions Timothy Budd Oregon State University.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Operator Overloading Week 5.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
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,
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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 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 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
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,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
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.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
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.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
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
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
CISC/CMPE320 - Prof. McLeod
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, Cont.

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

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. McLeod3

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 instances of the class they are “friends” with. A friend function cannot access the private members of its friend object, because it is not a member of that object. You don’t need, and cannot, declare them as const. Fall 2015CISC/CMPE320 - Prof. McLeod4

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. McLeod5

Fall 2015CISC/CMPE320 - Prof. McLeod6 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. A friend function can access private members from instances.

Fall 2015CISC/CMPE320 - Prof. McLeod7 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. McLeod8 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. McLeod9 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 << (an atomic type, a string or a *char ).

Fall 2015CISC/CMPE320 - Prof. McLeod10 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. McLeod11

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. McLeod12

Fall 2015CISC/CMPE320 - Prof. McLeod13 Overloading Increment and Decrement Operators ++ and -- Pre-increment, ++x, increments by one then returns the value. Post-increment, x++, returns the value then increments by one. Member functions: MyClass& operator++(); // pre-increment MyClass operator++(int unused); // post-increment In the pre-increment form, use return *this; to return a reference to the current object.

Fall 2015CISC/CMPE320 - Prof. McLeod14 Overloading Assignment Operators The = operator is automatically generated for you, so you don’t have to overload it. By default the assignment operator carries out member-wise assignment. As long as you don’t have to carry out any dynamic memory management tasks with the heap, the default = should be fine. However you will still have to overload the combined assignment operators such as +=, -=, *=, /=, etc. (Invoke the binary operators…)

MyComplex Demo, Again See “version 4”: overloads ++ and +=. Which is faster? Pre-increment or post- increment? Fall 2015CISC/CMPE320 - Prof. McLeod15

Fall 2015CISC/CMPE320 - Prof. McLeod16 Overloading Conversion Operators You don’t need to write these for assignment 2. Consider that your Fraction class has top for the numerator and bottom for the denominator. To overload a cast to type double : Fraction::operator double() const { return static_cast (top) / bottom; } If test is a Fraction object, this allows stuff like: double aNum = static_cast (test);

Fall 2015CISC/CMPE320 - Prof. McLeod17 Overloading Conversion Operators, Cont. Similarly for int. Note the lack of return type. But if you have a conversion operator and an applicable conversion constructor this leads to an ambiguity: Which operator should be used in a mixed type expression? See MyComplexAmbiguous

Fall 2015CISC/CMPE320 - Prof. McLeod18 Overloading Conversion Operators, Cont. With just the constructor you can do things like: int aVal = 10; Fraction test = static_cast (aVal); Same as: Fraction test = Fraction(aVal); But you can’t do: aVal = static_cast (test);

Fall 2015CISC/CMPE320 - Prof. McLeod19 Overloading Conversion Operators, Cont. So, what’s to do? If you ditch the one number constructor, then you give yourself a lot more work when you need to overload operators for mixed expressions. Might be easiest to keep the constructor and compromise. Write member functions called intVal() and doubleVal() instead? Or declare the constructor explicit ?

Fall 2015CISC/CMPE320 - Prof. McLeod20 The explicit Keyword If you want a constructor to just be a constructor and not for use as a rule for implicit type conversions, declare it with explicit. For example: class Fraction { // etc. explicit Fraction(int); // etc. };

The explicit Keyword, Cont. If you use explicit : From the MyComplexAmbiguous demo, what is the result of a mixed type expression with a number on one side and a MyComplex instance on the other? Fall 2015CISC/CMPE320 - Prof. McLeod21

Fall 2015CISC/CMPE320 - Prof. McLeod22 Overloading the Subscript Operator: [ ] Appropriate for: –A data structure requiring access using an index. –A data structure that wants to make sure index values are legal. See the SafeArray class. –Note the two versions of operator[] used. –Note the use of a friend non-member function. –Note the use of a destructor (getting ahead of myself…)

Fall 2015CISC/CMPE320 - Prof. McLeod23 Overloading the Function Call Operator: ( ) Allows an object to behave as if it is a function. Called a function object. The only operator where the number of arguments is not fixed or limited to one or two. See the RandomInt class. Used in the STL.

Fall 2015CISC/CMPE320 - Prof. McLeod24 Aside - inline Functions Used in the RandomInt class. Used when the function body is short: –≤ 3 assignment statements. –one conditional. –one return. An inline function is not placed on the activation record stack, but is expanded in place and so is executed faster. Must be in declaration file. The downside is that a copy is made every time the function is invoked – so it should be short.