25
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.)

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

Embed Size (px)

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. McLeod3

Citation preview

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

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.)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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…

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

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

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

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

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

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…)

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

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.

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

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!

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

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?