1 / 38

Review

Review. Bernard Chen Spring 2006. Syntax vs. Semantics. Syntax: the form of the present ex: if( condition ) { } Semantics: the meaning of the present. Function calls. Call by value: The actual argument is copied into the parameter ex: int findMax(vector<int> a);

Download Presentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review Bernard Chen Spring 2006

  2. Syntax vs. Semantics • Syntax: the form of the present ex: if( condition ) { } • Semantics: the meaning of the present

  3. Function calls • Call by value: The actual argument is copied into the parameter ex: int findMax(vector<int> a); • Call by reference: avoids copy, it allows change of the parameter int findMax(vector<int> &a);

  4. Function calls • Call by constant reference: avoids copy and guarantees that actual parameter will not be change ex. int findMax(const vector<int> &a);

  5. 1.3 Pointers syntax in C++ • How to Declare a pointer int *ptr; • & : unary operator that returns the address of the object. int x=5; int *ptr; ptr=&x; cout << ptr << endl; // output: 0013FF7C

  6. Pointer cont. • * : unary operator which can access data through a pointer *ptr = 10; Example: int x=5; int *ptr=&x; //*ptr=5 ptr= 0013FF7C *ptr=10 //*ptr=10 ptr=same x=10 • Illegal: ptr=x //x is not an address

  7. Legal Pointer Syntax int x=10 Declare a pointer: int *ptr=&x or int *ptr ptr=&x After declare: *ptr=15

  8. Illegal Pointer Syntax • int *ptr //run time error *ptr=&x • ptr=x or int *ptr=x

  9. 1.4 Dynamic memory management • newoperator allocates memory and returns a pointer to the newly created object. • When an object that is allocated by newis no longer referenced, applied deleteoperator on this object, through its pointer, to avoid “memory leakage”

  10. Cont. • Example: string *str_ptr; str_ptr = new string("Hello"); cout<< *str_ptr << endl; //Hellow cout<< (*str_ptr).length() <<endl; //5 delete str_ptr;

  11. Structures • Struct Student { string firstName; string lastName; … }; • Student a, *sPtr; // declaring Student • a.firstName= “Mary”;//modifying member data • sPtr= &a; • (*sPtr).lastName= “Smith”; • sPtr->lastName= “Smith”; // same as above

  12. Copying objects • Shallow copy: a copy of pointers rather than data being pointed at • Deep copy: a copy of data being pointed at rather than the pointers.

  13. 2.1 What is OO programming? • Object: an atomic unit that has structure and state • Information hiding: Black-box analogy • Encapsulation: grouping of data and functions • Inheritance: mechanism allows extending functionality of an object.

  14. Constructors • Member functions that describe how an object is declared and initialized. • If no constructor defined, compilers will generate one called default constructor. • Explicit constructors prevent automatic type conversion.

  15. Big three: Destructor, Copy Constructor, and Operator = •Destructor tells how an object is destroyed and freesdresources when it exists scope. ~IntCell(); •Copy Constructor allows a new object construct using the data in an existing one. IntCella(5); // a new IntCellcall a IntCellb(a); // another IntCellcall b •Operator = copy assignment, copies data members using = by default.=> may cause shallow copying.

  16. 3.1 What is a Template • A mechanism for writing routines that work for arbitrary types w/o knowing these types (type independent). • Most likely be seen in generic algorithm implementations, i.e. find max, sorting & searching.

  17. 3.2 Function Templates • A Function template is a design or pattern for a function which allows processors to generate an actual function from this design. • A function template is not an actual function, instead it’s a design or a pattern, for what could become an actual function.

  18. Swap routine used in main function: int main(){ int x =5, y = 7; double a = 2, b = 4; swap (x,y); // swap(int,int) swap(x,y); //reuse previous instantiation swap(a,b); //swap(double, double) //swap(x, b); // illegal: no match return 0; } // figure 3.3

  19. 3.3 A Sorting Function Template template <class Comparable> void insertionSort(vector<Comparable> &a) { for( int p = 1; p < a.size() ; p++) { comparable tmp = a[p]; int j; for ( j=p ; j>0 ; j-- ) { if (temp < a[j-1]) { a[j]=a[j-1]; a[j-1]=tmp;} } } }

  20. 3.4 Class Templates • A class can be a template. • Example: vector is a class template • When possible, constant reference should be used instead of call by value because if object is a large object, making a copy could be inefficient. (or illegal if copy constructor is disable or not defined)

  21. 4.1 What is Inheritance? • Another mechanism for code reuse. • A process of deriving classes from a base class w/o disturbing the implementation of the base class. • Ex: Vehicleis a class Car is a Vehicle => Car derived from Vehicle

  22. 4.2 Inheritance Basics • Public inheritance: all public members of the base class remain public in the derived class =>models is-a relationship =>mostly used. • Private inheritance: hidden all inherited members from the public => models has-a relationship.

  23. Access rules

  24. Bindings • Static binding: the decision about which function/type to use to resolve an overload is made at compile time. • Dynamic binding: the decision must be made at run time.

  25. Virtual • If a function is redefined in a derived class, it should be declared virtualin the base class. • Example: class Worker{ public: virtual void doWork(); };

  26. Virtual Cont… • Constructors are “NEVER” virtual • Destructors should be virtual for base class => Let the computer know which one to call for deallocation.

  27. Abstract • Abstract method, pure virtual function, has no meaningful definition in the base class and must always be defined in derived classes. • Abstract class: a class containing any abstract method = > can’t be instantiated and must be inherited. • Example: shape class

  28. General rules • Nonvirtual functions: Overloading is resolved at compile time. • Virtual functions: Overloading is resolved at run-time (Base class provide a default implementation) • Pure virtual functions Overloading is resolved at run-time. (Base class provide no default implementation)

  29. Ch 5. • What is Design Pattern • Functor • Wrapper • Adapter • Iterator • Observer

  30. Big Oh Notation • Big Oh notation is used to capture the most dominant term in a function, and to represent the growth rate. • Also called asymptotic upper bound. Ex: 100n3 + 30000n =>O(n3) 100n3 + 2n5+ 30000n =>O(n5)

  31. 6.3 The Max. Contiguous Subsequence • Given (possibly negative) integers A1, A2, .., An, find (and identify the sequence corresponding to) the max. value of sum of Ak where k = i -> j. The max. contiguous sequence sum is zero if all the integer are negative. • {-2, 11, -4, 13, -5, 2} =>20 • {1, -3, 4, -2, -1, 6} => 7

  32. Figure 6.9

  33. Worst-case vs. Average-case • A worst-case bound is a guarantee over all inputs of size N. • In an average-case bound, the running time is measured as an average over all of the possible inputs of size N. • We will mainly focus on worst-case analysis, but sometimes it is useful to do average one.

  34. 6.6 Static Searching problem • Static Searching Problem Given an integer X and an array A, return the position of X in A or an indication that it is not present. If X occurs more than once, return any occurrence. The array A is never altered.

  35. Binary Search • If the array has been sorted, we can use binary search, which is performed from the middle of the array rather than the end. • We keep track of low_end and high_end, which delimit the portion of the array in which an item, if present, must reside. • If low_end is larger than high_end, we know the item is not present.

  36. How stack works Empty stack push(a) push(b) pop(b) tos=1 tos=0 tos=0 tos= -1

  37. Circular Example Both Front and Back wraparound as needed. b c d e f Front Back g b c d e f Back Front

More Related