Title: CSc212AB Data Structures Lecture 2
1CSc212AB Data StructuresLecture 2
- Matthew P. Johnson
- City College of New York
- Spring, 2005
2Personnel
- Instructor Matthew P. Johnson
- mpjohnson_at_gmail.com
- Please prepend CSc212 to subject line!
- Office hours
- MW 1040-1140
- NAC 8-202b (share with Prof. Grossberg)
- please visit!
3Communications
- Web page
- http//www-cs.ccny.cuny.edu/mjohnson/212/
- Tentative syllabus
- course policies
- Homeworks
- Readings to prepare for class
- Dont fall behind!
- Check regularly!
4Textbook
- Data Structures and Other Objects Using C
- by Michael Main, Walter Savitch
- 3 edition (October 12, 2004)
- Available
- CCNY bookstore
- Amazon/BN (may be cheaper)
- Amazon.co.uk (may be cheaper still)
- Searchable on Amazon
- Links on class page
5C references
- Course taught in C
- All programming homeworks done in C
- Will assume prior knowledge
- But will review some topics
- See http//www.columbia.edu/mpj9/3101-2/ for
links to C courses and notes - Links to my notes on C in that pages READING
section
6Mini topic common series
- SUM(i) n(n1)/2
- Guass
- Definitely know this one
- Rest may sometimes be helpful
- SUM(2i) 2(n1) 1
- SUM(Ai) A(n1) 1 / (A-1)
- Hn SUM(1/i) ln n
- Harmonic numbers
7Mini topic logs
- very important
- memorize properties if havent already
- 2. df logbX y ? by x
- intuit Y is number of bs multiplied together
yield X - i.e., log is inverse of exponentiation function
- 3. in CS, log base is almost always 2 log x or
lg x - elsewhere, log x sometimes means base 10
- ln x means base e Eulers number 2.71
8Log properties may be helpful
- Xaxb x(ab)
- Xa/xb x(a-b)
- (Xa)b x(ab)
- Xnxn 2xn
- 2n2n 2(n1)
- Log(A/B) logA logB
- Log(AB) BlogA
- 0 0
- Log1 0, log 2 1, log 1024 10
- Log(million) 20
- logbX O(logcX) for any bases b and c
9Common exponents
- 210 1024 k
- 28 256
- 216 64k
- 220 1 meg
- 230 1 gig
- 2n (23)(n/3.0)
- (23)(n/3)
10A Quiz
Big-O notation O(n2) O(n2) O(n2) O(n) O(log n)
Number of operations n25n 100nn2 (n7)(n-2) n10
0 number of digits in 2n
11Big-O Notation
- The order of an algorithm generally is more
important than the speed of the processor
12Canonical complexity e.g.
13Time Analysis of C Functions
- Example
- Printout all item in an integer array of size N
- Frequent linear pattern
- A loop that does a fixed amount of operations N
times requires O(N) time
14Time Analysis of C Functions
- Quiz
- Printout char one by one in a string of length N
- What is a single operation?
- If the function calls do complex things, then
count the operation carried out there - Put a function call outside the loop if you can!
for (i0 istri cout
O(N2)!
15Time Analysis of C Functions
- Quiz
- Printout char one by one in a string of length N
- What is a single operation?
- If the function calls do complex things, then
count the operation carried out there - Put a function call outside the loop if you can!
N strlen(str) for (i0 istri cout
O(N)!
16Time Analysis of C Functions
- Worst case, average case and best case
- search a number m in an integer array a of size
N - Can you provide an exact number of operations?
- Best case 121
- Worst case 13N1
- Average case 13N/21
for (i0 (iai
17New topic Complexity Settings
- Can measure complexity in diff. settings
- Best, ave, worse
- Best case sometimes easy to figure
- but not very interesting
- easy to modify slow alg above to look for simple
in-a-row case - ? for that, algs very fast
- Ave case representative
- the expected time, averaged over all inputs
- often hard to figure
- sometimes doesnt make sense eg?
- Worst-case what we almost always want
- strong guarantee
- can promise your boss will never be worse than
this
18New topic RAM model
- The abstract model we find algorithms for
- not TM!
- Consider adding two numbers
- on a TM, would take log time just to write one
down - on a real computer, would happen in essentially
one instruction - very fast
- multiplying takes a little longer, but still very
fast - 3. on RAM model, each simple operation takes
one unit of time - 4. real life is slightly more complicated
- mult takes longer but not much!
- Adding billions takes same time as adding 22, if
using ints - Data types have fixed sizes! 32 bits
- 5. Same w/ memory access, calling a function,
etc. - 6. But loops do matter
- For I 1 to 1010 (do something), takes longer
than just do something - Time for loop time for body num. of
repetitions - 7. RAM model is Flat Earth Model
19Ideas for measuring complexity
- measure actual RTs
- Hw eg find, implemented fast sort alg
- Print total time, hand in number
- Prob sorting NYC p.b. always slower than sorting
Jersey City p.b. - measure RT ftn of input
- Might make sense if inputnumber
- Prob interp sorting files as sorting
filenames - measure RT as ftn of input size
- Prob some computers faster
- standardize machine
- Prob log on late at night
- measure actual ops in abstract RAM model as ftn
of input size - Prob too messy
- measure approximate ops in abstract RAM model
as ftn of input size
20Testing and Debugging
- Test run a program and observe its behavior
- input - expected output?
- how long ?
- software engineering issues
- Choosing Test Data two techniques
- boundary values
- fully exercising code (tool profiler)
- Debugging find the bug after an error is found
- rule never change if you are not sure whats
the error - tool debugger
21Summary
- Often ask yourselves FOUR questions
- WHAT, WHY, WHERE HOW
- Topics DSs, C, STL, basic algorithms
- Data Structure experts
- Schedule 23 lectures, 7 assignments, 3 exams
- some credits (10) for attending the class
- Information website
- Remember and apply two things (Ch 1)
- Basic design strategy
- Pre-conditions and post-conditions
- Running time analysis
- Testing and Debugging (reading 1.3)
22Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
23Object Oriented Programming
- Chapter 2 introduces Object Oriented Programming.
- OOP is a relatively new approach to programming
which supports the creation of new data types and
operations to manipulate those types. - This lecture gives a review of C Classes and
introduces ADTs.
24C Classes and ADTs
- Class
- Mechanism to create objects and member functions
- Support information hiding
- Abstract Date Types (ADTs)
- mathematical data type
- Class as an ADT that programmers can use without
knowing how the member functions are implemented
- i.e. with information hiding
25A point ADT
- A data type to store and manipulate a single
point on a plane - Manipulations
- Initialize
- Retrieval
- Shift
26A point ADT
- A data type to store and manipulate a single
point on a plane - Manipulations
- Initialize
- Retrieval coordinates
- Shift
(-1, 0.8)
27A point ADT
- A data type to store and manipulate a single
point on a plane - Manipulations
- Initialize
- Retrieval coordinates
- Shift
p1
x
0.8
-1.0
28A point ADT
- A data type to store and manipulate a single
point on a plane - Manipulations
- Initialize
- Retrieval coordinates
- Shift by
p1
x
p2
(1.3, -1.4)
(0.3, -0.6)
29Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
30point Definition
- We can implement the point object using a data
type called a class.
class point . . .
Dont forget the semicolon at the end
31point Definition
- The class will have two components called x and
y. These components are the x and y coordinates
of this point. - Using a class permits two new features . . .
class point . . . double x
double y
32point Definition
- The two components will be private member
variables. This ensures that nobody can directly
access this information. The only access is
through functions that we provide for the class.
class point . . . private double
x double y
33point Definition
- In a class, the functions which manipulate the
class are also listed.
class point public . . . private
double x double y
Prototypes for the point functions go here, after
the word public
34point Definition
- In a class, the functions which manipulate the
class are also listed.
class point public . . . private
double x double y
Prototypes for the point member functions go here
35point Definition
Our point has at least four member functions
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x() const double get_y( )
const private double x double y
Function bodies will be elsewhere.
36point Definition
The keyword const appears after two prototypes
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
This means that these functions will not
change the data stored in a point ADT.
37Files for the point ADT
- The point class definition, which we have just
seen, is placed with documentation in a file
called point.h, outlined here. - The implementations of the
three member functions will be
placed in a separate file called point.cxx, which
we will examine in a few minutes.
Documentation (Preconditions and Postconditions)
- Class definition
- point class
- definition which we
- have already seen
38Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
39Using the point ADT
- A program that wants to use the point ADT must
include the point.h header file (along with its
other header inclusions). - File pointmain1.cxx
include include include
point.h" ...
40Using the point ADT
- Just for illustration, the example program will
declare two point variables named p1 and p2.
include include include
point.h" int main( ) point p1 point
p2
41Using the point ADT
- Just for illustration, the example program will
declare two point objects named p1 and p2.
include include include
point.h" int main( ) point p1
point p2
42Using the point ADT
- The program starts by calling the
initialize member function for p1.
include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
43Using the point ADT
- The program starts by activating the
initialize member function for p1.
include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
44Using the point ADT
- The member function activation consists of four
parts, starting with the object name.
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
Name of the object
45Using the point ADT
- The instance (object) name is followed by a
period.
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
A Period
46Using the point ADT
- After the period is the name of the member
function that you are activating.
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
Name of the Function
47Using the point ADT
- Finally, the arguments for the member function.
In this example the first argument (x coordinate)
is - -1.0 and the second argument (y coordinate)
is 0.8
include "thinker.h" int main( ) point
p1 point p2 p1.initialize(-1.0, 0.8)
Arguments
48A Quiz
- How would you activate p1's get_x member
function ? - What would be the output of p1's get_x member
function at this point in the program ?
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
49A Quiz
- Notice that the get_x member function has no
arguments. - At this point, activating p1.get_x will return a
double value - -1.0.
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout )
50A Quiz
- Trace through this program, and tell me the
complete output.
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout p1.get_x( ) p2.initialize(p1.get_x(), p1.get_y()) cout
p2.shift(1.3, 1.4) cout p2.get_y()
51A Quiz
- -1.0 0.8
- -1.0 0.8
- 0.3 2.2
int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout p1.get_x( ) p2.initialize(p1.get_x(), p1.get_y()) cout
p2.shift(1.3, 1.4) cout p2.get_y()
52What you know about Objects
- Class Data Member Functions.
- You know how to define a new class type, and
place the definition in a header file. - You know how to use the header file in a program
which declares instances of the class type. - You know how to activate member functions.
- But you still need to learn how to write the
bodies of a classs member functions.
53Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
54point Implementation
Remember that the member functions bodies
generally appear in a separate point.cxx file.
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
Function bodies will be in .cxx file.
55point Implementation
We will look at the body of intialize, which must
assign its two arguments to the two private
member variables.
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
56point Implementation
For the most part, the functions body is no
different than any other function body.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
- But there are two special features about a member
functions body . . .
57point Implementation
- In the heading, the function's name is preceded
by the class name and - otherwise C won't
realize this is a classs member function.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
58point Implementation
- Within the body of the function, the classs
member variables and other member functions may
all be accessed.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
59point Implementation
- Within the body of the function, the classs
member variables and other member functions may
all be accessed.
But, whose member variables are these? Are
they p1.x p1.y p2.x p2.y
void pointinitialize(double init_x, double
init_y) x init_x y init_y
?
60point Implementation
- Within the body of the function, the classs
member variables and other member functions may
all be accessed.
If we activate p1.initialize p1.x p1.y
61point Implementation
- Within the body of the function, the classs
member variables and other member functions may
all be accessed.
If we activate p2.initialize p2.x p2.y
62point Implementation
- Here is the implementation of the get_x member
function, which return the x coordinate
double pointget_x() return x
63point Implementation
- Here is the implementation of the get_x member
function, which return the x coordinate
double pointget_x() return x
Notice how this member function implementation
uses the member variable x of the point object.
64point Implementation
- Member functions may activate other member
functions
void pointorigin() x 0.0 y
0.0
Notice this member function implementation still
directly assign the member variables x and y.
65point Implementation
- Member functions may activate other member
functions
void pointorigin() initialize(0.0,
0.0)
Without object name
Notice how this member function implementation
uses the member function initialize.
66A Common Pattern
- Often, one or more member functions will place
data in the member variables...
Initialize shift
get_x get_y
- ...so that other member functions may use that
data.
67 Summary of classes
- Classes have member variables and member
functions. An object is a variable where the data
type is a class. - You should know how to declare a new class type,
how to implement its member functions, how to use
the class type. - Frequently, the member functions of an class type
place information in the member variables, or use
information that's already in the member
variables. - Next we will see more features of OOP and classes.
68Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
69Constructors point Initialization
- The program starts by activating the
initialize member function for p1.
include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
First improvement automatic initialization
without activating the initialize function
70Constructors point Initialization
We can provide a normal member function initialize
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x() const double get_y( )
const private double x double y
71Constructors point Initialization
Or use a constructor that is automatically called
class point public point(double init_x,
double init_y) void shift(double dx, double
dy) double get_x() const double
get_y( ) const private double x
double y
- function name same as class name
- no return type, even no void !
72Constructors Implementation
For the most part, the constructor is no
different than any other member functions.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
- We only need to replace initialize with point
73Constructors Implementation
For the most part, the constructor is no
different than any other member functions.
pointpoint(double init_x, double init_y)
x init_x y init_y
- But there are three special features about
constructor . . .
74Constructors
- Constructor is a member function which
- automatically called whenever a variable of the
class is declared - the name must be the same as the class name
- arguments must be given after the variable name
(when declared) - A way to improve the initialize function
- by providing an initialization function that is
guaranteed to be called
75Constructors point Initialization
- Automatically called when declared.
- Parameters after the object names
include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
First improvement automatic initialization
without explicitly activating an initialize
function
76Constructors point Initialization
- Automatically called when declared.
- Parameters after the object names
include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2(0.3, 0.6)
First improvement automatic initialization
without explicitly activating an initialize
function
77Default Constructors
- Automatically called when declared.
- Parameters after the object names
include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2(0.3, 0.6)
Sometime we want to define an object with no
parameters
78Default Constructors
- Automatically called when declared.
- NO parameters after the object name p2
include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2
not even a pair of parentheses
79Default Constructors
We could provide a second constructor with no
parameters
class point public point()
point(double init_x, double init_y)
private double x double y
Implementation pointpoint() x 0.0
y 0.0
80Constructors Function Overloading
- You may declare as many constructors as you like
one for each different way of initializing an
object - Each constructor must have a distinct parameter
list so that the compiler can tell them part - Question How many default constructor is
allowed?
81Constructors automatic default constructor
- What happens if you write a class without any
constructors? - The compiler automatically creates a simple
default constructor - which only calls the default constructors for
the member variables that are objects of some
other classes - Programming Tip Always provide your own
constructors, and better with a default
constructor
82Value Semantics of a Class
- Value semantics determines how values are copied
from one object to another - Consists of two operations in C
- The assignment operator
- The copy constructor
- Document the value semantics
- When you implement an ADT, the document should
include a comment indicating that the value
semantics is safe to use.
83Value Semantics assignment operator
- Automatic assignment operator
- For a new class, C normally carries out
assignment by simply copying each variable from
the object on the right to that on the left - our new class point can use automatic assignment
operator - When automatic assignment fails
- we will see examples in Lecture 4 (pointers and
dynamic arrays)
point p1(-1.0, 0.8), p2 p2 p1 cout p2.get_x()
84Value Semantics copy constructor
- A copy constructor
- is a constructor with exactly one parameter whose
data type is the same as the constructors class - is to initialize a new object as an exact copy
of an existing object - An example
point p1(-1.0, 0.8) point p2 (p1) cout p2.get_x()
85Value Semantics copy constructor
- A copy constructor
- is a constructor with exactly one parameter whose
data type is the same as the constructors class - is to initialize a new object as an exact copy
of an existing object - An alternative syntax
point p1(-1.0, 0.8) point p2 p1 cout p2.get_x()
86Value Semantics discussion
- point p2 p1 versus p2 p1
- The assignment p2 p1 merely copies p1 to the
already existing object p2 using the assignment
operator. - The syntax point p2 p1 looks like an
assignment statement, but actually a declaration
that both declare a new object, and calls the
copy constructor to initialize p2 as a copy of
p1. - p2 will be the same iff the assignment operator
and the copy constructor do the same things
87Copy Constructor Implementation
- You may write a copy constructor much like any
other constructor - Lecture 4 and later
- Take advantage of a C feature
- automatic copy constructor
- similar to assignment, the automatic copy
constructor initializes a new object by merely
copy all the member variables from the existing
object. - Automatic versions may fail!
A Demo
88Constructors, etc. a summary
- Constructor is a member function
- define your own constructors (including a
default) - automatic default constructor
- inline member functions ( Ch 2.2)
- Place a function definition inside the class
definition - for time efficiency
- value semantics of a class
- assignment operators and copy constructor
- automatic assignment op and copy constructor
89Outline
- A Review of C Classes (Lecture 2)
- OOP, ADTs and Classes
- Class Definition, Implementation and Use
- Constructors and Value Semantics
- More on Classes (Lecture 3)
- Namespace and Documentation
- Classes and Parameters
- Operator Overloading
90Assignments - draft
- Reading
- Chapter 2.3-2.5
- Programming assignment 1 - Due Sept. 14
- Projects 2 3 in Chapter 2 (pp 86)
- Need all of chapter 2 to finish, but you can
start doing it - Requirements and guidelines have been posted on
the course web site