Review Bernard Chen Spring 2006.

Slides:



Advertisements
Similar presentations
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Advertisements

. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 4 Inheritance.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
Pointers OVERVIEW.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Algorithm Analysis O Ω.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Overview of C++ Polymorphism
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
CS 215 Final Review Ismail abumuhfouz Fall 2014.
C++ Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Motivation and Overview
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
Pointers, Polymorphism, and Memory Allocation
Introduction to C++ and Algorithm Analysis
Pointers Revisited What is variable address, name, value?
CS212: Object Oriented Analysis and Design
Chapter 2 Objects and Classes
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
9-10 Classes: A Deeper Look.
Overview of C++ Polymorphism
Templates I CMSC 202.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

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

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

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

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

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

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

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”

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

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

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.

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.

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.

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.

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.

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.

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

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

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)

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

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.

Access rules

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.

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

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

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

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)

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

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)

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

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)

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.

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.

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.

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

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