CSE 250: Data Structures Week 3 – February 1, 2008 January 28

advertisement
CSE 250: Data Structures
Week 3
January 28 – February 1, 2008
Monday - Announcements
Homework 1 due Friday
Project 1 due 2/15
Monday – Game Plan
Finish review of linear structures
Go over implementation of linear
structures and C++ details
Monday
Queue
LIFO
Typically built upon another structure
enqueue, dequeue, peek only methods user
needs
Monday
C++ stuff
Class definitions end in a ;
No public modifier for class header
needed
private/public sections of the class are
headers, each thing is not individually
marked
Monday
Typically, classes are declared in .h files
and then implemented in .cpp files
You can not define functions in .cpp files
that have not been declared in .h
However, you can not define functions in
.cpp files that have been declared in .h
Monday
#ifndef
#define
#endif
Should be in all .h files to avoid the
problem of #including the same definition
twice (could happen in larger projects)
Name after #ifndef should be unique
Monday
explicit – does not allow for type
conversions when calling constructor
Constructors
Constructor takes an initial size, but if
none passed in, the initial size is set by
default to 0 (in this code)
C++ constructors can not call each other
Monday
Notice the weird : after the parameter list
in the constructor, this is an initializer list
It is sort of a shortcut for initialization of the
private data of the class
Should be used especially if the private
data is not of primitive type
Monday
Parameter Passing
Java uses call-by-value: a copy of the value of
the parameter is passed into the method
C++ can use call-by-value as well, but it
also provides support for call-by-reference
Call-by-reference allows the function to modify
the actual values of the parameters passed in
Use & in front of parameter name in
parameter list
Monday
Call-by-Constant Reference
Putting const in front of a reference parameter
makes a promise that the function will not
change the parameter that is passed in.
Wednesday - Announcements
Homework 1 due Friday
Project 1 due 2/15
Wednesday – Game Plan
More C++ implementation issues with
class vector
Why is it all in the .h? and better explanations
about stuff from last class
Copy constructor
Destructor
operator=
Brief look at lists as well
Wednesday
Const in the function definition, after
params before body makes the function an
accessor.
Return by reference: & before the return is
a return by reference.
Return by value is the default, a copy of the
value is returned, return by reference returns
the reference instead.
Wednesday
Exceptions exist in C++, notice the throw
in the code. Same purpose as Java. Will
get into them more later if needed.
operator= can be redefined to create a
deep copy (an actual duplicate) of the
object. Useful operator overloading for a
copy constructor, or just to be able to
create a duplicate.
Wednesday
Destructor – in C++, you must give back
the memory you allocate, destructors help
us do that. Keyword delete to give the
space back.
enum lets us define constants, or can be
used to define a unique type.
Wednesday
List class
Structs – An aggregate, record type. A way to
carry around a bunch of data. No functions
defined on a struct. You can access the data
and change the data stored only.
This list implementation shows
inheritance, more on that Friday.
Friday Announcements
Homework 1 due today!
Project 1 due 2/15
Friday – Game Plan
List code
Friday
friend – allows another object access to
the function or data.
Inheritance in C++
Multiple inheritance allowed
Inherits public functions and data
Download