View
226
Download
2
Tags:
Embed Size (px)
Citation preview
Computer Science IIExam I ReviewMonday, February 6, 2006
Material Coverage - LecturesLecture Date Coverage Reading
1 1/17Intro & Background
K&M: Chap 0
Malik: see Lec Notes
2 1/20 Background II Chap 0
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
6 2/3 C++ Classes II Chap 4.2-4.4, Chap 9
Material Coverage -Lab/HW
Date Coverage
Lab 1 1/18 Getting Started
Lab 2 1/25Strings, Files, and Command Line Arguments
Lab 3 2/1 C++ Classes
HW 1Due 1/26
Getting Started
HW 2Due 2/2
Moire Strings
Labs & HW
Background Review
Know basic syntax comments included files (why?) main is necessary for all programs expressions (Chap 2 of Malik book) variables, objects, types, constants
#include <iostream> // asks compiler for parts of std lib
int main( )
{std::cout << “Hello, world!” << std::endl; return 0; }
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 name;
With a specified number of instances of a single character
string 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> b(100, 3.14);
With a specified number of instances with no initial value
vector<int> 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?