review

38
Review Bernard Chen Spring 2006

Upload: fallon-pittman

Post on 31-Dec-2015

19 views

Category:

Documents


0 download

DESCRIPTION

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 a); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Review

Review

Bernard ChenSpring 2006

Page 2: Review

Syntax vs. Semantics

Syntax: the form of the present ex: if( condition ) { }

Semantics: the meaning of the present

Page 3: Review

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

Page 4: Review

Function calls

Call by constant reference: avoids copy and guarantees that actual parameter will not be change

ex. int findMax(const vector<int> &a);

Page 5: Review

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

Page 6: Review

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

Page 7: Review

Legal Pointer Syntax

int x=10Declare a pointer: int *ptr=&x or int *ptr ptr=&x After declare: *ptr=15

Page 8: Review

Illegal Pointer Syntax

int *ptr //run time error

*ptr=&x

ptr=x or int *ptr=x

Page 9: Review
Page 10: Review

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”

Page 11: Review

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

cout<< (*str_ptr).length() <<endl; //5

delete str_ptr;

Page 12: Review

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

Page 13: Review

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.

Page 14: Review

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.

Page 15: Review

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.

Page 16: Review

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 aIntCellb(a); // another IntCellcall b

•Operator = copy assignment, copies data members using = by default.=> may cause shallow copying.

Page 17: Review

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.

Page 18: Review

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.

Page 19: Review

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 matchreturn 0;

} // figure 3.3

Page 20: Review

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

}}

Page 21: Review

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)

Page 22: Review

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

Page 23: Review

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.

Page 24: Review

Access rules

Page 25: Review

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.

Page 26: Review

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

};

Page 27: Review

Virtual Cont…

Constructors are “NEVER” virtual

Destructors should be virtual for base class => Let the computer know which one to call for deallocation.

Page 28: Review

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

Page 29: Review

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)

Page 30: Review

Ch 5.

What is Design Pattern Functor Wrapper Adapter Iterator Observer

Page 31: Review

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)

Page 32: Review

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

Page 33: Review

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)

Page 34: Review

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.

Page 35: Review

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.

Page 36: Review

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.

Page 37: Review

How stack works

a

b

a a

tos= -1

tos=0

tos=1

pop(b)push(a) push(b)Empty stack

tos=0

Page 38: Review

Circular Example

Both Front and Back wraparound as needed.

b c d

Front Back

b c d

FrontBack

e f

e fg