Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 215-401 Midterm Study Guide Fall 2014. General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.

Similar presentations


Presentation on theme: "CS 215-401 Midterm Study Guide Fall 2014. General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs."— Presentation transcript:

1 CS 215-401 Midterm Study Guide Fall 2014

2 General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs (reading code) System classes, functions, and header files. Common errors. Algorithms.

3 Topics Variables and types (chapter 2) Conditionals and Booleans (chapter 3) Loops (chapter 4) Functions (chapter 5) Arrays and vectors (chapter 6) Pointers and dynamic allocation (chapter 7) Streams and les (chapter 8) Classes and object-oriented design (chapter 9).

4 Chapter 2 Variables, Types & Operations Types: –int, double, char, bool, string. –Literals of each type. –When to use each type. –Differences between integers and floating point. Variables: –Identifier, type, scope, address, value. –Declaration and definition. (declare and initialize them before using them) –Scope of the Variable (Local vs. global variables). –Arithmetic operations Operations: –+ - * / % …. –Functions sqrt pow exp … –Operator precedence. –Integer division - when does it happen?

5 Chapter 3 & 4 Conditions & Loops Conditionals: –If, if-else, nested if… –Boolean expressions: && || ! –Common errors: extra semicolon. –Errors: tests that are always false or always true. Loops: –For, while, and do-while loops. –Differences between types of loops. –Equivalence of for and while loops. –Scope of loop variables. –For loop bounds: how many iterations?

6 Chapter 5Functions Syntax for functions definition (declaration and implementation). Syntax for prototypes. Parameters and arguments (what's the difference?) Return values and types (including void). The return statement. Deciding what parameters and return type a function needs. Call-by-value: copies the argument. Call-by-reference: use the argument itself (why and how?) const references.

7 Chapter 6 (Arrays) Syntax for arrays: e.g. int mylist[12]; Accessing: mylist[i].. Valid indexes i range from 0 to length - 1. Looping over array: for (int i = 0; i < length; i++) Limitations: capacity must be known in advance. Partially-filled arrays –Companion variable for the (current) size. –Capacity (maximum size) is still fixed. –Inserting and removing: shift the following elements. Arrays as function arguments. –Always call-by-reference. –Need a companion variable for array size. –Actually a pointer! –Cannot return an array, only a pointer. Two-dimensional arrays: e.g. int twod[rows][columns];

8 Chapter 7 Pointers Declaring a pointer: int *p; Operators –Address-of (&): int *p = &n; –Dereference (*): int n = *p; Pointer assignment: int *q; q = p; –Now they point to the same thing: changing *p also changes *q. NULL, uninitialized pointers. The name of an array is a pointer to its first element. Pointer arithmetic: + 1 goes to next element in an array. Array-pointer duality: a[i] = *(a + i). Int *p = &n; what is p, *p, and &p (and their types)?

9 Chapter 7 (Dynamic memory) Allocated from the heap. Use the New operator: int *p = new int; –Free with delete p; Or allocate a whole array: int *p = new int[n]; –Free with delete[] p; Common errors –Memory leaks (didn't free something). –Dangling pointer (using already-deleted memory). –Bounds error (going past the allocated array). –Wrong deletion operator

10 Chapter 7: C strings Char array: char [] vs. Pointer to an array of characters: char * End is marked by a null terminator (the character '\0') String literals are C strings. Iterating: –for (int i = 0; str[i] != ‘\0'; i++) – for (const char *p = str; *p != ‘\0’ ; p++) Length: number of characters not counting the null terminator. –Array size must be 1 more. –The null terminator is at index length Algorithms for length, copying, etc. Converting string objects to C strings: strobj.cstr()

11 Chapter 7: C strings Algorithms for length, copying, etc. Converting string objects to C strings: strobj.cstr() C string functions: strlen,, strcpy, strcmp,.... –In the cstring header. Algorithm for searching for special character in C string –Checking for digits (isdigit), letters (isalpha) …. – #include string objects / C++ strings string myString= “CS215”; Converting C strings to string objects

12 Chapter 8 : Streams Header files: iostream, iomanip, ifstream Insertion operator << –I/O manipulators: setw, scientific, endl Extraction operator >> –Skips initial whitespace. –Reads one item. –The rest is buffered, including the newline. –Sets a fail state on error. getline function: getline(cin, stringvar) get method: cin.get(ch) reads a single character. The fail state: cannot use the stream until it is fixed. –Detecting: fail method. –Clearing errors: clear and ignore Reading in a loop istringstream / ostringstream

13 Chapter 8 File Stream Types: ifstream, ofstream. Opening and closing. –ifstream infile; infile.open("outfile.txt"); –infile.close(); Reading from or writing to a file stream. Exactly the same as using cin or cout Just use the name of the stream variable: infile >> age What happens if a file e is missing? –ifstream : A fail state (check with.fail() ) –ofstream : No problem, the file is created. Stream parameters: must be call-by-reference. Sequential versus random access. Text versus binary files

14

15 Now lets answer some Problems and Questions


Download ppt "CS 215-401 Midterm Study Guide Fall 2014. General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs."

Similar presentations


Ads by Google