Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,

Similar presentations


Presentation on theme: "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,"— Presentation transcript:

1 Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions, cont. –Random file I/O. –Start Operator Overloading. (If we have time.) Soon: –More about what goes in a RAD.

2 Function Details! A few odds ‘n ends: –Return types –Overloading –Variable scope –static local variables –Default arguments –Passing by value, by reference and by constant reference –Pointers to functions Fall 2015CISC/CMPE320 - Prof. McLeod2 Last time.

3 Fall 2015CISC/CMPE320 - Prof. McLeod3 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?

4 Fall 2015CISC/CMPE320 - Prof. McLeod4 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

5 Fall 2015CISC/CMPE320 - Prof. McLeod5 Passing Arguments See PassingThings.cpp. In summary: –Pass a reference to avoid using the copy constructor. –If you are not going to change the reference’s contents, use the const keyword. –If you are going to change the parameter, then “call by reference” is a bit easier. –Pass arrays by passing the pointer. You may need to pass the size of the array as another argument… –Pass an object by constant reference to avoid side effects, and to take the least amount of time and memory.

6 Fall 2015CISC/CMPE320 - Prof. McLeod6 Aside - 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.

7 Fall 2015CISC/CMPE320 - Prof. McLeod7 Aside - 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.

8 Fall 2015CISC/CMPE320 - Prof. McLeod8 Random File Access Text I/O as discussed before is all sequential access – unidirectional. To open a file for input and output at the same time and to read any position in any order, you need to use random I/O. Same library: fstream.

9 Fall 2015CISC/CMPE320 - Prof. McLeod9 Random File Access, Cont. To open for input and output, for example: fstream rwStream; rwStream.open(“stuff.txt”, ios::in | ios::out); Last time we used ifstream for input and ofstream for output. fstream extends both of these classes, allowing you to do input and output at the same time.

10 Random File Access, Cont. File open modes: Combine modes using the binary “or” |. Fall 2015CISC/CMPE320 - Prof. McLeod10 ModeMeaning ios::app Append to end of file and you cannot move anywhere else. ios::ate Starts like append, but then you can move anywhere in the file. ios::in Open for input. ios::out Open for output. ios::trunc Discard contents (default behavior for ios::out). ios::binary Open for binary I/O.

11 Fall 2015CISC/CMPE320 - Prof. McLeod11 Random File Access, Cont. Imagine two pointers in the file - a read pointer (“get”) and a write pointer (“put”). To read or write you first have to position the pointer at a certain byte position. For example: rwStream.seekp(1000, ios::beg); Positions the write pointer 1000 bytes from the beginning of the file.

12 Fall 2015CISC/CMPE320 - Prof. McLeod12 Random File Access, Cont. rwStream.seekg(1000, ios::end); Positions the read pointer 1000 bytes from the end of the file. You can also use ios::cur to position the pointer in relative terms. After the seek, you read and write the normal way. But how to know where to position the “pointers”?

13 Fall 2015CISC/CMPE320 - Prof. McLeod13 Random File Access, Cont. This only works when you have a structured file. You need to know what is stored, in which order it is stored and how big each item is. Remember that you can use the sizeof() operator to get the size of anything – primitive types or objects. This way you could write an entire object to the file and read it back the same way.

14 Fall 2015CISC/CMPE320 - Prof. McLeod14 Operator Overloading – Java You should remember that the binary + operator is overloaded in Java: 2 + 3 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.

15 Fall 2015CISC/CMPE320 - Prof. McLeod15 + 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.

16 Fall 2015CISC/CMPE320 - Prof. McLeod16 + 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.

17 Fall 2015CISC/CMPE320 - Prof. McLeod17 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));

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

19 Fall 2015CISC/CMPE320 - Prof. McLeod19 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.

20 Fall 2015CISC/CMPE320 - Prof. McLeod20 Overloadable Operators, Cont. And, you only overload the ones that make sense!!!


Download ppt "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,"

Similar presentations


Ads by Google