Download presentation
Presentation is loading. Please wait.
1
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 9/22/2018 CISC/CMPE320 Notices: Assignment 1 due this Friday at 7pm. The rest of the assignments have been moved ahead a week. Teamwork: You were busy yesterday! RAD will be due at the end of next week as a document in Confluence. I will discuss this document in class and there is more description on the group project page. Fall 2017 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
2
Is Software Ever Finished?
Fall 2017 CISC/CMPE320 - Prof. McLeod
3
CISC/CMPE320 - Prof. McLeod
Today More About Functions Start Operator Overloading Fall 2017 CISC/CMPE320 - Prof. McLeod
4
CISC/CMPE320 - Prof. McLeod
Back to Functions A few odds ‘n ends: Return types Overloading Variable scope static local variables Default arguments Pointers to functions Fall 2017 CISC/CMPE320 - Prof. McLeod
5
CISC/CMPE320 - Prof. McLeod
Return Types If nothing is to be returned then use void as the return type. Never return a pointer or a reference to a variable that is local to the function! Use of the returned value will lead to “undefined behaviour”. (However, you can return a pointer to an object created on the heap, using the “new” keyword. – Later…) Fall 2017 CISC/CMPE320 - Prof. McLeod
6
CISC/CMPE320 - Prof. McLeod
Function Overloading You can overload functions in the same class by changing the parameter list, not by changing the return type. Each overloaded function must still have its own prototype. You cannot overload parent member functions in a child class in C++. You will “shadow” or “hide” the functions instead! More later… Fall 2017 CISC/CMPE320 - Prof. McLeod
7
CISC/CMPE320 - Prof. McLeod
Variable Scope A variable declared inside a function is local to that function. A variable declared outside any function or class definition is global in scope. Fall 2017 CISC/CMPE320 - Prof. McLeod
8
Aside – static Variables
You can declare a local variable to be static inside a function. See StaticLocalTest.cpp Note that the static variable is only set to zero once. Look at the program in a debugger? Fall 2017 CISC/CMPE320 - Prof. McLeod
9
CISC/CMPE320 - Prof. McLeod
static Variables, Cont. This type of variable is placed in a special area of memory that holds all global variables and functions. This memory is allocated before your program even runs and is only emptied out when your program is finished. The size of this area of memory does not change. Fall 2017 CISC/CMPE320 - Prof. McLeod
10
CISC/CMPE320 - Prof. McLeod
Default Arguments For example: int getVol(int len, int width = 1, int height = 1); This can be done by overloading alone. You can accept the default value or replace it. Default arguments must come after normal parameters, and cannot have a normal parameter mixed in the list. Note – do not assign default arguments again in the function implementation. See DefaultArguments.cpp Fall 2017 CISC/CMPE320 - Prof. McLeod
11
CISC/CMPE320 - Prof. McLeod
Pointers to Functions You can define a pointer to a function. See FunctionsAsPointers.cpp Note that the return type and parameter list must match up. The example does not look useful, but you can also pass function pointers as parameters. The function could be a comparator, for example. Fall 2017 CISC/CMPE320 - Prof. McLeod
12
Pointers to Functions, Cont.
This is mostly a C usage. There are better ways to do this including function templates and by taking advantage of polymorphism – this is all C++! See another example: SortAlgorithmTemplateExample.cpp modified from an example in cplusplus.com. Fall 2017 CISC/CMPE320 - Prof. McLeod
13
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” 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 2017 CISC/CMPE320 - Prof. McLeod
14
+ 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 2017 CISC/CMPE320 - Prof. McLeod
15
+ 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. Fall 2017 CISC/CMPE320 - Prof. McLeod
16
CISC/CMPE320 - Prof. McLeod
Operator Overloading This flexibility is a result of operator overloading in the string class – a task carried out by the class designer to make the 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 2017 CISC/CMPE320 - Prof. McLeod
17
Overloadable Operators in C++
- * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new new[] delete delete[] Fall 2017 CISC/CMPE320 - Prof. McLeod
18
Overloadable Operators, Cont.
These include unary operators, binary operators and both bitwise shift and stream insertion operators (already overloaded!). 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 2017 CISC/CMPE320 - Prof. McLeod
19
Overloadable Operators, Cont.
And, you only overload the ones that make sense for your class!!! Fall 2017 CISC/CMPE320 - Prof. McLeod
20
CISC/CMPE320 - Prof. McLeod
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 2017 CISC/CMPE320 - Prof. McLeod
21
CISC/CMPE320 - Prof. McLeod
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 friends and no accessors. Fall 2017 CISC/CMPE320 - Prof. McLeod
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.