fall 2015cisc/cmpe320 - prof. mcleod1 cisc/cmpe320 today: –review declaration, implementation,...
DESCRIPTION
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. McLeod3TRANSCRIPT
Fall 2015 CISC/CMPE320 - Prof. McLeod 1
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.)
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 2015 CISC/CMPE320 - Prof. McLeod 2
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 2015 CISC/CMPE320 - Prof. McLeod 3
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 #included!).
Fall 2015 CISC/CMPE320 - Prof. McLeod 4
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 2015 CISC/CMPE320 - Prof. McLeod 5
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 2015 CISC/CMPE320 - Prof. McLeod 6
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 2015 CISC/CMPE320 - Prof. McLeod 7
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 2015 CISC/CMPE320 - Prof. McLeod 8
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 2015 CISC/CMPE320 - Prof. McLeod 9
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 2015 CISC/CMPE320 - Prof. McLeod 10
Fall 2015 CISC/CMPE320 - Prof. McLeod 11
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.
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 2015 CISC/CMPE320 - Prof. McLeod 12
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 2015 CISC/CMPE320 - Prof. McLeod 13
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 2015 CISC/CMPE320 - Prof. McLeod 14
Exceptions, Cont.• You can define your own exception class or
use/extend one of the existing C++ exception types from <stdexcept>:
Fall 2015 CISC/CMPE320 - Prof. McLeod 15
Fall 2015 CISC/CMPE320 - Prof. McLeod 16
C++ Exception Typesexception
logic_errorbad_alloc runtime_error bad_cast
invalid_argument out_of_range
domain_error
length_error range_error overflow_error
underflow_error
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 2015 CISC/CMPE320 - Prof. McLeod 17
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;}
Fall 2015 CISC/CMPE320 - Prof. McLeod 18
Fall 2015 CISC/CMPE320 - Prof. McLeod 19
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…
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 2015 CISC/CMPE320 - Prof. McLeod 20
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 2015 CISC/CMPE320 - Prof. McLeod 21
Fall 2015 CISC/CMPE320 - Prof. McLeod 22
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…)
Fall 2015 CISC/CMPE320 - Prof. McLeod 23
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.
Fall 2015 CISC/CMPE320 - Prof. McLeod 24
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!
Fall 2015 CISC/CMPE320 - Prof. McLeod 25
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?