Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC/CMPE320 - Prof. McLeod

Similar presentations


Presentation on theme: "CISC/CMPE320 - Prof. McLeod"— Presentation transcript:

1 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

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

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

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

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

19 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

20 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

21 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

22 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

23 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


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google