31
Computer Science II Exam I Review Monday, February 6, 2006

Computer Science II Exam I Review Monday, February 6, 2006

  • View
    226

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Computer Science II Exam I Review Monday, February 6, 2006

Computer Science IIExam I ReviewMonday, February 6, 2006

Page 2: Computer Science II Exam I Review Monday, 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

Page 3: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 4: Computer Science II Exam I Review Monday, February 6, 2006

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; }

Page 5: Computer Science II Exam I Review Monday, February 6, 2006

Statement

single statement ending with ; structured statement (i.e. else if) compound statement delimited

by {…}

Review statements, expressions, and assignments

Page 6: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 7: Computer Science II Exam I Review Monday, February 6, 2006

Functions and Arguments

Why use them? each has parameters and a

return type (including main!) order of parameters matters

Page 8: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 9: Computer Science II Exam I Review Monday, February 6, 2006

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?

Page 10: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 11: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 12: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 13: Computer Science II Exam I Review Monday, February 6, 2006

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)

Page 14: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 15: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 16: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 17: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 18: Computer Science II Exam I Review Monday, February 6, 2006

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;

Page 19: Computer Science II Exam I Review Monday, February 6, 2006

Some member functions

push_back() O(1) on average

size() begin() end() [] operator (like with arrays)

Page 20: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 21: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 22: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 23: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 24: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 25: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 26: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 27: Computer Science II Exam I Review Monday, February 6, 2006

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

Page 28: Computer Science II Exam I Review Monday, February 6, 2006

Struct

What is it? Class where default protection is

public, not private

Page 29: Computer Science II Exam I Review Monday, February 6, 2006

Non-member operators

not declared/defined in class scope but the arguments to the function are class objects

Examples: operator< operator+ IsEarlierThan

Page 30: Computer Science II Exam I Review Monday, February 6, 2006

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!!

Page 31: Computer Science II Exam I Review Monday, February 6, 2006

Questions?