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 Jira accounts have been created – if you did not get an from Jira – let me know. Confluence spaces have been created – one for each team. Slack is configured – you should have received an “invitation” . There is one channel for each team. Assignment 1 due in a week – 7pm. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

2 What’s Next for Your Team?
I’m still waiting to find out the names of some managers and to get all project descriptions. But, 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. Fall 2018 CISC/CMPE320 - Prof. McLeod

3 CISC/CMPE320 - Prof. McLeod
Today A Text File I/O Demo. Re-Structure the Demo: Separate Declaration and Implementation. Create a Simple Class. Create and Use an Exception Class. (Assn 1 requires the creation of a class and file I/O.) Fall 2018 CISC/CMPE320 - Prof. McLeod

4 CISC/CMPE320 - Prof. McLeod
Text File I/O – Example See TextIODemo.cpp Stuff that also snuck into this demo: Use of vector and string classes. Function prototypes. Passing by constant reference. We will also look at an OOP version of this program. Fall 2018 CISC/CMPE320 - Prof. McLeod

5 CISC/CMPE320 - Prof. McLeod
Function Prototypes Allow you to implement functions in any order, since the function names have already been declared. Easier to see the structure of what you are coding. Parameters must be typed – optionally supply the parameter name for style. Next, the declarations will be placed in a separate file from the implementation. Fall 2018 CISC/CMPE320 - Prof. McLeod

6 CISC/CMPE320 - Prof. McLeod
File I/O Demo as a Class Restrucure TextIODemo.cpp by separating out a header file and an implementation file. Might as well make a simple class out of the result. See: textfileio.h textfileio.cpp TestFileIOClass.cpp Fall 2018 CISC/CMPE320 - Prof. McLeod

7 Header File - textfileio.h
This header file only contains the declaration of the TextFileIO class, but it could also have contained: enums structs non-member function prototypes other class declarations constants documentation Putting any implementation in the header file is considered poor structure (and poor style). Fall 2018 CISC/CMPE320 - Prof. McLeod

8 Declaration in Separate File
Now, the implementation can be completely hidden from anyone using your class. You should be able to use the class without knowing anything about the implementation! Fall 2018 CISC/CMPE320 - Prof. McLeod

9 CISC/CMPE320 - Prof. McLeod
textfileio.h, Cont. Note the use of const in the member function declaration: vector<string> readFile() const; This contract promises that the member function will not change member variables (attributes). Optional, but good programming practice, particularly for accessors. Fall 2018 CISC/CMPE320 - Prof. McLeod

10 CISC/CMPE320 - Prof. McLeod
textfileio.h, Cont. #pragma once ensures that the declarations in this file will only be made once. The *.h file will be included in any file that is going to use this class using: #include "textfileio.h" You can have as many of these #includes as you want in a project without worry! Fall 2018 CISC/CMPE320 - Prof. McLeod

11 CISC/CMPE320 - Prof. McLeod
Class Declaration The public: and private: sections control access to class members from instantiations. As you would expect, encapsulation dictates that your attributes are declared in the private: section! Fall 2018 CISC/CMPE320 - Prof. McLeod

12 Implementation File – textfileio.cpp
Has the same name (different extension) as the header file, by convention. Implements member and non – member functions. Few comments, or just comments for the developer. Users are not going to see this file. Fall 2018 CISC/CMPE320 - Prof. McLeod

13 CISC/CMPE320 - Prof. McLeod
textfileio.cpp, Cont. The constructor: TextFileIO::TextFileIO(const string& fname) : filename(fname) {} Note the “shortcut” notation in the initialization section. You can still do things the old-fashioned way, especially if you are going to check the arguments for legality. Fall 2018 CISC/CMPE320 - Prof. McLeod

14 CISC/CMPE320 - Prof. McLeod
textfileio.cpp, Cont. Also note the membership operator :: It allows you to associate the member with the class. You can implement members or non-member functions for any header file that you have included. Fall 2018 CISC/CMPE320 - Prof. McLeod

15 Aside - private Members
Member function definitions and their implementations can access private members – even if this is in a different file. Non-member functions cannot access private members, only public ones. Fall 2018 CISC/CMPE320 - Prof. McLeod

16 CISC/CMPE320 - Prof. McLeod
TestFileIOClass.cpp Some class in your project must have a main function, or your application will not run. (But only one main function per project!) TextFileIO is instantiated on the run-time stack (more about this later) in the usual way. You can only access public: members from here. Fall 2018 CISC/CMPE320 - Prof. McLeod

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

18 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

19 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

20 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

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

22 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

23 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

24 CISC/CMPE320 - Prof. McLeod
Back to Classes 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


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google