CISC/CMPE320 - Prof. McLeod

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 Fundamentals
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Review of C++ Programming Part II Sheng-Fang Huang.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
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.
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.
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.
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 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
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 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
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.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
Chapter 13: Overloading and Templates
Overloading Operator MySting Example
Object Oriented Programming COP3330 / CGS5409
CISC/CMPE320 - Prof. McLeod
Chapter 15: Overloading and Templates
CISC/CMPE320 - Prof. McLeod
Operator Overloading BCA Sem III K.I.R.A.S.
The dirty secrets of objects
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operators Lecture 10 Fri, Feb 8, 2008.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Overview of C++ Overloading
CISC/CMPE320 - Prof. McLeod
Operator Overloading, Friends, and References
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading.
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,
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Operator Overloading I
Operator Overloading; String and Array Objects
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 2/23/2019 CISC/CMPE320 All project level linking of our three tools is complete. RAD due this Friday 7pm. See last Wednesday’s lecture for more info. You must include a sprint made up with issues that have time to completion estimates. Don’t forget that the document must be created and presented in Confluence. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Today Continue Operator Overloading. Fall 2018 CISC/CMPE320 - Prof. McLeod

Overloadable Operators in C++ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new new[] delete delete[] Fall 2018 CISC/CMPE320 - Prof. McLeod

Overloadable Operators, Cont. These include unary operators, binary operators and both bitwise shift and stream insertion operators. An overloaded operator is only used when one or the other, or both, operands are of your class type. (ie. you cannot overload int + int – sorry…) You cannot create new operators. You cannot change associativity or the order of precedence. You cannot make a unary operator a binary one, or visa-versa. Fall 2018 CISC/CMPE320 - Prof. McLeod

Overloadable Operators, Cont. And, you only overload the ones that make sense for your class!!! Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operator Overloading Three ways to overload an operator: Member function. No need for accessors, but possible problems with mixed type expressions. Non-member function. Need accessors. Works better with mixed type expressions. Non-member friend function No need for accessors for function arguments. Works like non-member function with mixed type expressions. Fall 2018 CISC/CMPE320 - Prof. McLeod

Operator Overloading, Cont. You will likely need a mix of these techniques, even in a single class. Depends on: Which operator you are overloading. If you will have accessors or not. Whether mixed type expressions are required. Whether or not you allow conversion constructors. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operator Functions General 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 2018 CISC/CMPE320 - Prof. McLeod

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. Assignment operators must be member functions, because you need to modify the LHS. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod MyComplex Class Demo See the start of a simple class to hold complex numbers: myComplex.h and myComplex.cpp. Overload just the + and << operators. First version uses non-member overloading and accessors. Second version uses member overloading of the + operator. Third version uses non-member friends and no accessors. Fall 2018 CISC/CMPE320 - Prof. McLeod

Aside - Complex Numbers in C++ Of course C++ already has a library for a templated complex numbers: #include <complex> Types include std::complex<double> See the reference docs… Fall 2018 CISC/CMPE320 - Prof. McLeod

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 friends. So, we know how to overload the binary arithmetic operators and <<. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod friend Functions (Classes can also be declared friends.) 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 of arguments of the current type. The function prototype must be inside the class definition (before the };) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod friend Functions, Cont. Convenient, but: They break the privacy rules of a stringent OOP language. They do not have access to the this pointer. Too many friend functions can lead to spaghetti code. They can be polymorphic, but it requires some extra coding. Use sparingly, with caution… Fall 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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

Aside - Converting Between Strings and Numbers Is much nicer now in C++11 The <string> library has built-in functions for conversions in both directions: Fall 2018 CISC/CMPE320 - Prof. McLeod

Overloading Increment and Decrement Fall 2018 CISC/CMPE320 2/23/2019 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 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

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. (Maybe invoke the binary operators?…) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod MyComplex Demo, Again See “version 4”: overloads ++ and +=. Which is faster? Pre-increment or post-increment? Why doesn’t post increment return a reference? Fall 2018 CISC/CMPE320 - Prof. McLeod

Aside - The this Pointer This keyword provides a pointer to the current instance of the class at run-time. “Myself” You can use it to obtain members using -> *this de-references the pointer. Fall 2018 CISC/CMPE320 - Prof. McLeod