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/4/2019 CISC/CMPE320 You should be able to log in to Bitbucket, but I have not had a chance to create your repositories. Bitbucket is integrated with Jira. 56 users have not yet logged into Jira. 41 students have pending invitations to Slack. If you did not get these s, then let me know! If you are just slacking off (no pun intended!) – why? I’m still waiting to hear from 13 teams about their managers! Assignment 1 due this week – 7pm Friday. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

2 CISC/CMPE320 - Prof. McLeod
Teamwork Today When you agree on a project it is time to get going!: The Jira main page has a link to the “Jira 101 Guide”. See links provided in onQ in the Week 3 Content in a page called “Jira Tutorial Links” (lynda.com course portions). Create a product backlog and then use issues to create your first sprint. Customize your board in Jira, organize your folders in Confluence. Make sure everyone is using your Slack channel and is getting notifications. Change notifications and user permissions where needed. Fall 2018 CISC/CMPE320 - Prof. McLeod

3 CISC/CMPE320 - Prof. McLeod
Today Create and Use an Exception Class with the Text I/O class. Back to functions for a little while… Fall 2018 CISC/CMPE320 - Prof. McLeod

4 Update to Previous Demo Code
File stream constructors can now accept a string object. You don’t need to invoke the .c_str() accessor. So, ifstream fileIn(filename.c_str()); Becomes: ifstream fileIn(filename); Where filename is a string. Fall 2018 CISC/CMPE320 - Prof. McLeod

5 Aside - Default Constructors
Invoked without parameters. If we had one for TextFileIO it would be invoked as: TextFileIO test; No round brackets! What does: TextFileIO test(); look like to you? Fall 2018 CISC/CMPE320 - Prof. McLeod

6 CISC/CMPE320 - Prof. McLeod
Exceptions C++ and C have many ways to indicate errors. The “modern” approach is to throw exceptions. The mechanism is very similar to Java’s except that: You can throw anything except a pointer. (You could even throw an int: throw 42; - that would be legal!) Exceptions do not even need to be part of the exception heirarchy. You don’t need to annotate the method throwing the exception. You don’t have to catch the exception – you always have a choice. Fall 2018 CISC/CMPE320 - Prof. McLeod

7 CISC/CMPE320 - Prof. McLeod
Exceptions, Cont. You can define your own exception class or use/extend one of the existing C++ exception types from <stdexcept>: Fall 2018 CISC/CMPE320 - Prof. McLeod

8 CISC/CMPE320 - Prof. McLeod
C++ Exception Types exception bad_alloc logic_error runtime_error bad_cast length_error range_error overflow_error domain_error underflow_error invalid_argument out_of_range Fall 2018 CISC/CMPE320 - Prof. McLeod

9 CISC/CMPE320 - Prof. McLeod
Exception Demo See: textfileioe.h textfileioe.cpp TestFileIOException.cpp Fall 2018 CISC/CMPE320 - Prof. McLeod

10 CISC/CMPE320 - Prof. McLeod
Exceptions, Cont. The try/catch syntax is the same as in Java: TextFileIO testBad(badFile); try { vector<string> testData(testBad.readFile()); } catch (TextFileException& e) { cerr << e.what() << endl; } Always catch an exception by reference. Fall 2018 CISC/CMPE320 - Prof. McLeod

11 CISC/CMPE320 - Prof. McLeod
Exceptions, Cont. You can use the “ellipsis” operator to catch anything: try { // stuff } catch(...) { // do something (invoke destructor, for example) throw; // propagates exception } This is an interesting, but dangerous operator… Fall 2018 CISC/CMPE320 - Prof. McLeod

12 Classes, To Be Continued…
Private attributes means that you will likely need mutators and accessors. Overloaded constructors. A default constructor? And, we may need overloaded operators, too! More later. You know all you need for now to do the assignment. Fall 2018 CISC/CMPE320 - Prof. McLeod

13 Back to Functions For a While
A few odds ‘n ends: Return types Overloading Variable scope static local variables Default arguments Pointers to functions Initializer Lists as parameters Lambda functions constexpr applied to functions C++11 C++11 C++11 C++14 Fall 2018 CISC/CMPE320 - Prof. McLeod

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

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

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

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

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

19 CISC/CMPE320 - Prof. McLeod
Default Arguments For example: int getVol(int len, int width = 1, int height = 1); Assigned in the prototype. 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 2018 CISC/CMPE320 - Prof. McLeod

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

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

22 Initializer List Parameter Type
C++11 You can use std::initializer_list<type> as a parameter type to allow you to pass an initializer into a function. An initializer is a list of items of type contained in { }. #include <initializer_list> Already used by standard container classes like vector. See InitializerList.cpp. Fall 2018 CISC/CMPE320 - Prof. McLeod


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google