Download presentation
Presentation is loading. Please wait.
1
CS 215 Final Review Ismail abumuhfouz Fall 2014
2
Exam format * true/false (10%) * multiple choices (45%)
* short answers and coding questions (45%) ◮ answering questions ◮ code reading and writing
3
Topics General topics it would be good to review for each chapter: 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.
4
Topics Midterm (Chapter 2-5). Vectors & Arrays (Chapter 6).
Pointers (Chapter 7). Classes (Chapter 9). Inheritance and polymorphism (Chapter 10). Recursion (Chapter 11). Time Complexity (Chapter 12) Lists, stacks, and queues (Chapter 13).
5
Chapter 2-5 Types and variables Conditionals and loops Functions
◮ int, long, double, char. ◮ Variable naming rule (letter case), scope, address, value. ◮ Variable should be declared and initialized before use. ◮ static cast<int>(myDouble): cast a double to int. Conditionals and loops ◮ ==, >=, <= ◮ if statement, for loop, while loop, and do-while loop. Functions ◮ Passing by value, passing by reference (&) ◮ Array parameter is passed by reference by default ◮ Everything else is passed by value by default
6
Chapter 6 Arrays Char arrays Vectors ◮ Declaration of arrays
◮ Array of pointers ◮ Array name is a pointer that points to the first element in this array ◮ int a[5]; What are the types of a, &a, *a, a[2], &(a[2])? Char arrays ◮ C string: char array. ◮ char* myStr1 = “Hello”; (myStr1 points to a const C string) ◮ char* myStr2 = new char[5]; (myStr2 points to a char array in heap memory) ◮ char myStr3[5]; (myStr3 points to a char array in stack memory) ◮ C++ string, aka string object: std::string strObj(“Hello”); ◮ Converting an string object to C string: strObj.c str(); ◮ What is strObj[2] ? Vectors ◮ push back(), pop back() ◮ vector<int>::iterator it; (vector<int> myVec;) ◮ for (it = myVec.begin(); it != myVec.end(); it++) ◮ for (int i = 0; i < myVec.size(); i++)
7
Pointers and dynamic allocation
Chapter 7 Pointers and dynamic allocation ◮ Declaration of pointers ◮ int myInt = 5; int* myIntPtr1 = &myInt; ◮ int* myIntPtr2 = new int[10]; ◮ int* myIntPtr3 = new int; ◮ delete[] myIntPtr2; ◮ delete myIntPtr3; ◮ Should NOT delete myIntPtr1; ◮ Array-pointer duality: a[i] = *(a + i) ◮ Pointer arithmetic: + 1 goes to next element in an array. Common errors ◮ Deferencing uninitialized / NULL pointer ◮ Memory leaks (didn’t free something). ◮ Dangling pointer (using already-deleted memory). ◮ Bounds error (going past the allocated array). ◮ Wrong deletion operator.
8
Chapter 9 Encapsulation: separating interface from implementation.
◮ All access to your class goes through the interface. ⋆ The public member functions (methods). ◮ Member variables (data) are private. ⋆ Only accessible by the methods. A class describes a set of objects with the same behavior. ◮ A class is a type. ◮ An object is a thing of that type. ◮ You use an object by calling its methods. Constructors ◮ Initialize an object’s private data. ◮ Same name as the class. ◮ When does a constructor get called? ◮ Difference between default constructor and overloaded constructor? Destructors ◮ Name is class name with a tilde before it: ∼className(). ◮ No return type, no parameters. ◮ Frees any data allocated in the class. ◮ Automatically called when the object ceases to exist. Xiwei (Jeffrey) Wang (UK CS) CS 215 Final Review
9
Chapter 10 Inheritance Inherited methods
◮ Implements the “is-a” relationship. What about aggregation? (“has- a”). ◮ Derived class inherits from the base class. ◮ C++ syntax: class Car : public Vehicle {...};. ◮ The derived class has all the members of the base class, plus any additional members declared in the derived class. ◮ Base-class private data is not directly accessible. ⋆ Use the base-class constructor to initialize the data: Derived::Derived(...) : Base(...) {...} ⋆ Use accessors to get their values. Inherited methods ◮ Methods are inherited: can call a base-class method on a derived- class object. ◮ You can override a method by redefining it in the derived class. ⋆ Can call the base class version: BaseClass ::method();.
10
Chapter 10 Slicing and polymorphism
◮ Putting a derived class object into a base-class variable slices off the derived parts. ⋆ This is usually not what you want. ◮ Base-class pointers and references can safely point to derived objects, without slicing. ⋆ Static type: declared type of the pointer (Person *) ⋆ Dynamic type: what it actually points to (Employee *) ⋆ Polymorphism is when these are different. virtual functions and abstract classes ◮ If you want to use polymorphism, you should declare the methods (the ones that will be overridden in derived classes) in based class as virtual. ◮ If a class contains at least one pure virtual function, it is an abstract class. ◮ You cannot instantiate an abstract class (creating objects of this class)
11
Chapter 11 Recursion: a function calling itself.
◮ The idea: solve a hard problem by first solving an easier version of the same problem ◮ which you solve by finding an even easier version ◮ until the problem is so easy you can solve it right away. Two components of a recursive function ◮ There must be special cases to handle the simplest tasks directly so that the function will stop calling itself. (Base case) ◮ Every recursive call must simplify the task in some way. (Recursive call )
12
Chapter 11 Example recursive definitions: Avoiding infinite recursion:
◮ Factorial: Base case: n <= 1 → n! = 1; General case: n > 1 Recursive formula: n! = (n − 1)! ∗ n ◮ Fibonacci: Base cases: n <= 1 → F (0) = F (1) = 1; General case: n > 1 Recursive formula: F (n) = F (n − 1) + F (n − 2) ◮ Merge sort: Base case: a one-element array is already sorted. Recursive call: cut in half, merge sort each half, then merge. Avoiding infinite recursion: ◮ Must check the base case first. ◮ No recursion in the base case! ◮ And the recursive call(s) must bring you closer to the base case. Mutual recursion: A calls B, B calls C, C calls A.
13
Chapter 12 Run-time complexity
◮ How does the time taken depend on the input size? ◮ Order of complexity: big O notation. ⋆ Calculate the number of operations performed. ⋆ Take only the fastest-growing term (highest exponent). ⋆ Remove any constant factors. ⋆ 3n2 + 6n becomes O(n2). ⋆ O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n )
14
Chapter 13 Stacks Queues Linked list
◮ A stack lets you insert and remove elements at one end only. ◮ Stack is a LIFO (Last-In, First-Out) data structure. Queues ◮ A queue lets you add items to one end of the queue (the back) and remove them from the other end of the queue (the front). ◮ Queue is a FIFO (First-In, First-Out) data structure. Linked list ◮ Linked list is a data structure that supports efficient addition and removal of elements in the middle of a sequence. ◮ A typical doubly linked list node has three fields: data, previous pointer, next pointer. ◮ A typical singly linked list node has two fields: data, and next pointer. ◮ STL linked list: std::list<string> myList; ◮ Iterator: list<string>::iterator it;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.