Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review Bernard Chen Spring 2006.

Similar presentations


Presentation on theme: "Review Bernard Chen Spring 2006."— 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; 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= //*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

10 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”

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

12 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

13 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.

14 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.

15 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.

16 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.

17 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.

18 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.

19 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

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

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

22 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: Vehicle is a class Car is a Vehicle => Car derived from Vehicle

23 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.

24 Access rules

25 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.

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

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

28 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

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

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

31 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: 100n n =>O(n3) 100n3 + 2n n =>O(n5)

32 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

33 Figure 6.9 Mathematical Expression Relative Rates of Growth
T(n) = O(F(n)) Growth of T(n) <= growth of F(n) T(n) = Ω(F(n)) Growth of T(n) >= growth of F(n) T(n) = Θ(F(n)) Growth of T(n) = growth of F(n) T(n) = o(F(n)) Growth of T(n) < growth of F(n)

34 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.

35 6.6 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.

36 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.

37 How stack works a b a a Empty stack push(a) push(b) pop(b) tos=1 tos=0

38 Circular Example Both Front and Back wraparound as needed. b c d Front
g


Download ppt "Review Bernard Chen Spring 2006."

Similar presentations


Ads by Google