Review - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Review

Description:

template class Comparable void insertionSort(vector Comparable &a) ... A process of deriving classes from a base class w/o disturbing the ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 39
Provided by: csG7
Learn more at: http://www.cs.gsu.edu
Category:
Tags: review

less

Transcript and Presenter's Notes

Title: Review


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(vectorltintgt a)
  • Call by reference avoids copy, it allows change
    of the parameter
  • int findMax(vectorltintgt a)

4
Function calls
  • Call by constant reference avoids copy and
    guarantees that actual parameter will not be
    change
  • ex. int findMax(const vectorltintgt 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 x5
  • int ptr
  • ptrx
  • cout ltlt ptr ltlt endl // output 0013FF7C

6
Pointer cont.
  • unary operator which can access data through
    a pointer
  • ptr 10
  • Example
  • int x5
  • int ptrx //ptr5 ptr 0013FF7C
  • ptr10 //ptr10 ptrsame x10
  • Illegal ptrx //x is not an address

7
Legal Pointer Syntax
  • int x10
  • Declare a pointer
  • int ptrx or int ptr
  • ptrx
  • After declare
  • ptr15

8
Illegal Pointer Syntax
  • int ptr //run time error
  • ptrx
  • ptrx or int ptrx

9
(No Transcript)
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")
  • coutltlt str_ptr ltlt endl //Hellow
  • coutltlt (str_ptr).length() ltltendl //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-gtlastName 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.gt 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 its 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 ltclass Comparablegt
  • void insertionSort(vectorltComparablegt a)
  • for( int p 1 p lt a.size() p)
  • comparable tmp ap
  • int j
  • for ( jp jgt0 j-- )
  • if (temp lt aj-1) ajaj-1
    aj-1tmp

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 gt Car derived from Vehicle

23
4.2 Inheritance Basics
  • Public inheritance all public members of the
    base class remain public in the derived class
    gtmodels is-a relationship gtmostly used.
  • Private inheritance hidden all inherited
    members from the public gt 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 gt
    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 gt cant 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 100n3 30000n gtO(n3)
  • 100n3 2n5 30000n gtO(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 -gt j.
    The max. contiguous sequence sum is zero if all
    the integer are negative.
  • -2, 11, -4, 13, -5, 2 gt20
  • 1, -3, 4, -2, -1, 6 gt 7

33
Figure 6.9
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
  • 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
pop(b)
push(a)
push(b)
Empty stack
tos1
tos0
tos0
tos -1
38
Circular Example
  • Both Front and Back wraparound as needed.
Write a Comment
User Comments (0)
About PowerShow.com