Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.

Similar presentations


Presentation on theme: "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show."— Presentation transcript:

1 Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show how to use it. –More details on functions. (If we have time.)

2 From Tuesday - 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 2015CISC/CMPE320 - Prof. McLeod2

3 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 poor structure (and poor style). Fall 2015CISC/CMPE320 - Prof. McLeod3

4 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! Provided your structure does not change, changing implementation minimizes build time as only that file needs to be re-compiled (the implementation file is not #include d!). Fall 2015CISC/CMPE320 - Prof. McLeod4

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

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

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

8 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 2015CISC/CMPE320 - Prof. McLeod8

9 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 2015CISC/CMPE320 - Prof. McLeod9

10 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 2015CISC/CMPE320 - Prof. McLeod10

11 Fall 2015CISC/CMPE320 - Prof. McLeod11 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.

12 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 2015CISC/CMPE320 - Prof. McLeod12

13 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 2015CISC/CMPE320 - Prof. McLeod13

14 Exceptions C++ and C have many ways to indicate errors. The “modern” approach is to throw exceptions. The mechanism is 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 hierarchy. –You don’t need to annotate the function throwing the exception. –You don’t have to catch the exception – you always have a choice. Fall 2015CISC/CMPE320 - Prof. McLeod14

15 Exceptions, Cont. You can define your own exception class or use/extend one of the existing C++ exception types from : Fall 2015CISC/CMPE320 - Prof. McLeod15

16 Fall 2015CISC/CMPE320 - Prof. McLeod16 C++ Exception Types exception logic_errorbad_allocruntime_errorbad_cast invalid_argumentout_of_range domain_error length_errorrange_erroroverflow_error underflow_error

17 Exception Demo See: –textfileioe.h –textfileioe.cpp –TestFileIOException.cpp Declares an exception object. Passes the string filename by constant reference (instead of passing a constant pointer). Throws & catches exceptions. Fall 2015CISC/CMPE320 - Prof. McLeod17

18 Exceptions, Cont. The try/catch syntax is the same as in Java: TextFileIO testBad(badFile); try { vector testData(testBad.readFile()); } catch (TextFileException& e) { cerr << e.what() << endl; } Fall 2015CISC/CMPE320 - Prof. McLeod18

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

20 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 2015CISC/CMPE320 - Prof. McLeod20

21 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. McLeod21

22 Fall 2015CISC/CMPE320 - Prof. McLeod22 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! The returned value will not be defined. (However, you can return a pointer to an object created on the heap, using the “new” keyword. – Later…)

23 Fall 2015CISC/CMPE320 - Prof. McLeod23 Function Overloading You can overload functions by changing the parameter list, not by changing the return type. Each overloaded function must still have its own prototype.

24 Fall 2015CISC/CMPE320 - Prof. McLeod24 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. Avoid!

25 Fall 2015CISC/CMPE320 - Prof. McLeod25 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 program in debugger?


Download ppt "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show."

Similar presentations


Ads by Google