Exam Review (ppt)
Download
Report
Transcript Exam Review (ppt)
Computer Science II
Exam I Review
Monday, February 6, 2006
Material Coverage - Lectures
Lecture Date
2
Coverage
Intro &
1/17
Background
1/20 Background II
3
1/24 Strings
Chap 1,2
4
1/27 Vectors
Chap 3
5
1/31 C++ Classes I
Chap 4.2-4.4, Chap 9
1
6
2/3
C++ Classes II
Reading
K&M: Chap 0
Malik: see Lec Notes
Chap 0
Chap 4.2-4.4, Chap 9
Material Coverage Lab/HW Date
Lab 1
Lab 2
Lab 3
HW 1
HW 2
1/18
Labs &
HW
Coverage
Getting Started
Strings, Files, and
1/25
Command Line Arguments
C++ Classes
2/1
Due Getting Started
1/26
Due Moire Strings
2/2
Background Review
#include <iostream> // asks compiler for parts of std lib
int main( )
{
std::cout << “Hello, world!” << std::endl;
return 0;
}
Know basic syntax
comments
included files (why?)
main is necessary for all programs
expressions (Chap 2 of Malik book)
variables, objects, types, constants
Statement
single statement ending with ;
structured statement (i.e. else if)
compound statement delimited by
{…}
Review statements, expressions, and
assignments
Arrays
fixed consecutive sequence of objects
all of the same type
indexing starts at 0
are a fixed size
programmer’s responsibility to
prevent overflow
constant arrays are similar to
constant variables – once initialized
they cannot be altered
Functions and Arguments
Why use them?
each has parameters and a return
type (including main!)
order of parameters matters
More Background Review
Loop syntax for:
for loops
while loops
Loop invariant
logical assertion that is true at start of
each iteration of a loop
stated as comment helps analyzing code
switch statements
If and else statements
&&, ||, %, and other operators
Background Review II
What is the difference between a
function prototype (declaration) and
definition?
What is one way to return multiple
results from a function?
Parameters to Functions
Value parameter
local variable in function
initial value is copy of argument value
changes in function do not affect
corresponding argument in calling
function
Reference parameter
alias for corresponding argument (not a
new variable)
changes do affect corresponding
variables in calling function
Arrays are tricky
Changes made to an array are
permanent, even if passed as value
parameters
Why?
Pass by value the memory location of
the base address of the array
Scope
of an identifier (name) is the part of
the program in which it has meaning
{ } establishes a new scope
scopes may be nested
identifiers may be reused as long as
they are in different scopes
those with same name in inner scope
hide those in outer scope
:: establishes scope as well
Order Notation
Can compare why one algorithm is
better than another (usually using
worst case analysis)
O(1)
O(log n)
O(n)
O(n log n)
O(n2)
O(nk)
O(2n)
Strings
object type defined in std lib to
contain sequence of characters
like all types, string type defines an
interface
construction (initialization)
operations
functions (methods)
other types
More about strings
Constructing string objects
By default (create an empty string)
string
With a specified number of instances of
a single character
string
name;
stars(10, ‘*’);
From another string
string
sentence = “I see stars ” + stars;
string name(“Bettina”);
L-value vs. R-value
Some member functions
size()
length()
size_type vs. unsigned int vs. int
+ operator concatenates two strings to
create a third
= assignment operation overwrites current
contents of string
strings behave like arrays when using []
operator
Vector
Ideal for storage of items when you don’t
know how many values there will be in
advance
std lib container class
acts like a dynamically-size 1D array
holds objects of any type (must all be same
type)
starts empty unless otherwise specified
no limit on size
can use subscripting [] operator
no automatic checking of [] bounds
Templated Container Class
Vectors are an example
<> are used to specify type of object
(template type) to be stored in vector
vector<int> grades;
vector<string> names;
vector<float> temperatures;
Some member functions
push_back()
O(1) on average
size()
begin()
end()
[] operator (like with arrays)
More about vectors
Constructing vectors
By default an empty vector
vector<int>
a;
With a specified number of instances of
a single object
vector<double>
With a specified number of instances
with no initial value
vector<int>
b(100, 3.14);
c(100);
From another vector (of same type!)
vector<int>
d(b);
Sort
std lib function that deals with
container classes
Sorts values from least to greatest if
no sorting function supplied
sort(a.begin(), a.end());
uses < operator on objects in vector
Can specify your own comparison
function
sort(a.begin(), a.end(), IsEarlierThan);
sort(a.begin(), a.end(), IsLaterThan);
Vectors and Strings as Parameters
Options:
pass by reference
pass by value (expensive!)
pass by constant reference
Note: This is unlike arrays which are
never pass by value
Classes
Defining a new type
Structure of memory within each class
object
Set of operations defined
C++ classes consist of:
collection of member variables (private)
collection of member functions (public)
public: can be accessed directly from
outside the class
private: can only be accessed indirectly
through public member functions
Classes Continued
Each object of a class created has its own
distinct member variables
Call member functions using dot notation
tomorrow.print();
main.cpp
.cpp file for member function definitions
(implementation file)
.h file for class declaration (header file)
include .h file in the two .cpp files above
More on classes
class scope ::
member functions and variables are
accessible without name of object
constructors
special functions that initialize values of
member variables
only
called once per object
default constructor has no arguments
multiple allowed
copy constructor automatically created
Member functions
Defined within class scope
Same as before but modify object’s
member variables
Can call member functions without
using object name
Functions which are not members of
the class must use public member
functions wrt an object of that class
Constant Member Functions
Member functions that do not change
the member variables should be
declared as this
bool Date::isEqual(const Date &date2) const;
const must appear in both class declaration
(.h) and member function definition (.cpp)
const objects can only use const member
functions
Struct
What is it?
Class where default protection is
public, not private
Non-member operators
not declared/defined in class scope
but the arguments to the function are
class objects
Examples:
operator<
operator+
IsEarlierThan
Exam 1
Tomorrow, Tuesday February 7
10 – 11:50am
West Hall Auditorium
Closed book and closed notes
except for 1 sheet of 8.5 x 11 paper
Bring your RPI ID!!
Questions?