Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computer Science II Exam I Review Monday, February 6, 2006."— Presentation transcript:

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

2 Material Coverage - Lectures LectureDateCoverageReading 11/17 Intro & Background K&M: Chap 0 Malik: see Lec Notes 21/20 Background II Chap 0 31/24 Strings Chap 1,2 41/27 Vectors Chap 3 51/31 C++ Classes I Chap 4.2-4.4, Chap 9 62/3 C++ Classes II Chap 4.2-4.4, Chap 9

3 Material Coverage - Lab/HWDateCoverage Lab 11/18 Getting Started Lab 21/25 Strings, Files, and Command Line Arguments Lab 32/1 C++ Classes HW 1 Due 1/26 Getting Started HW 2 Due 2/2 Moire Strings Labs & HW

4 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 // asks compiler for parts of std lib int main( ) { std::cout << “Hello, world!” << std::endl; return 0; }

5 Statement single statement ending with ; structured statement (i.e. else if) compound statement delimited by {…} Review statements, expressions, and assignments

6 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

7 Functions and Arguments Why use them? each has parameters and a return type (including main!) order of parameters matters

8 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

9 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?

10 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

11 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

12 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

13 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(n 2 ) O(n k ) O(2 n )

14 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

15 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

16 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

17 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

18 Templated Container Class Vectors are an example <> are used to specify type of object (template type) to be stored in vector vector grades; vector names; vector temperatures;

19 Some member functions push_back() O(1) on average size() begin() end() [] operator (like with arrays)

20 More about vectors Constructing vectors By default an empty vector vector a; With a specified number of instances of a single object vector b(100, 3.14); With a specified number of instances with no initial value vector c(100); From another vector (of same type!) vector d(b);

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

22 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

23 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

24 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

25 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

26 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

27 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

28 Struct What is it? Class where default protection is public, not private

29 Non-member operators not declared/defined in class scope but the arguments to the function are class objects Examples: operator< operator+ IsEarlierThan

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

31 Questions?


Download ppt "Computer Science II Exam I Review Monday, February 6, 2006."

Similar presentations


Ads by Google