Fall 2013CISC2200 Yanjun Li1 Review. Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Advertisements

5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Arrays Chapter 6.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Dynamic Structures & Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Plus Data Structures Data Design and Implementation
Chapter 2 Data Design and Implementation. Data The representation of information in a manner suitable for communication or analysis by humans or machines.
CISC Data Structures C/C++ Review Fall
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 2 Data Design and Implementation Chapter 2 Data Design and Implementation.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Windows Programming Lecture 03. Pointers and Arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Computer Programming BCT 1113
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
7 Arrays.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
7 Arrays.
Arrays Arrays A few types Structures of related data items
Presentation transcript:

Fall 2013CISC2200 Yanjun Li1 Review

Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming

Fall 2013CISC2200 Yanjun Li3 Array Arrays are data structures containing related data items of same type. An array is a consecutive group of memory locations.

Fall 2013CISC2200 Yanjun Li4 Elements of An Array To refer to a particular location or element in the array, we specify the name of the array and the index of the element. The index must be integer or integer expression, its value must be larger than or equal to 0. First element has index zero. The element could be used as a variable. Example C[ 0 ] += 2; C[ a + b ] = 3;

Fall 2013CISC2200 Yanjun Li5 Example An array c has 12 elements ( c[ 0 ], c[ 1 ], … c[ 11 ] ); the value of c[ 0 ] is –45.

Fall 2013CISC2200 Yanjun Li6 Declare An Array Declaration of an array: type arrayName[ arraySize ]; Example int c[ 12 ]; Array’s size must be an integer constant greater than zero. Arrays can be declared to contain values of data types like int, float, double, char, etc.

Fall 2013CISC2200 Yanjun Li7 Initialize An Array (1) With an initializer list –Initializer list Items enclosed in braces ( {} ) and separated by commas. –Examples int n[ 5 ] = { 10, 20, 30, 40, 50 }; int m[ 5 ] = { 10, 20, 30 }; –The remaining elements are initialized to zero. int p[ ] = { 10, 20, 30, 40, 50 }; –Because array size is omitted in the declaration, the compiler determines the size of the array based on the size of the initializer list.

Fall 2013CISC2200 Yanjun Li8 Initialize An Array (2) Using a loop to initialize the array’s elements –Declare array, specify number of elements –Use repetition statement to loop for each element Example: int n[10]; for ( int i = 0; i < 10; i++) { n[ i ] = 0; }

Fall 2013CISC2200 Yanjun Li9 Using An Array Usually use for loop to access each element of an array. C++ has no array bounds checking –Referring to an element outside the array bounds is an execution-time logic error. It is not a syntax error. –You need to provide the correct array size.

Fall 2013CISC2200 Yanjun Li10 Using An Array – Example 1 Summing the elements of an array Example: const int arraySize = 6; int a [ arraySize ] = { 87, 69, 45, 45, 43, 21 }; int total = 0; for ( int i = 0; i < arraySize; i++) { total += a[i]; } cout <<“Total of array elements is ” <<total<<endl;

Fall 2013CISC2200 Yanjun Li11 Passing Arrays as Parameters In C++, arrays are always passed by reference, and & is not used with the formal parameter type. Whenever an array is passed as a parameter, its base address is sent to the called function. Example: // prototype float SumValues ( float values [ ], int numOfValues );

Fall 2013CISC2200 Yanjun Li12 CALLING BLOCK FUNCTION CALLED Pass-by-value sends a copy of the contents of the actual parameter SO, the actual parameter cannot be changed by the function 12 C++ Tips

Fall 2013CISC2200 Yanjun Li13 C++ Tips CALLING BLOCK FUNCTION CALLED 13 Pass-by-reference sends the location (memory address) of the actual parameter SO, the actual parameter can be changed by the function

Fall 2013CISC2200 Yanjun Li14 const array parameter Because arrays are always passed as reference parameters, you can protect the actual parameter from unintentional changes by using const in formal parameter list and function prototype. FOR EXAMPLE... // prototype float SumValues(const float values[ ], int numOfValues );

Fall 2013CISC2200 Yanjun Li15 // Pre: values[0] through values[numOfValues-1] // have been assigned // Returns the sum of values[0] through // values[numOfValues-1] float SumValues (const float values[ ], int numOfValues ) { float sum = 0; for ( int index = 0; index < numOfValues; index++ ) { sum += values [ index ] ; } return sum; }

Fall 2013CISC2200 Yanjun Li16 Multidimensional Array Multidimensional arrays with two dimensions –Called two dimensional or 2-D arrays –Represent tables of values with rows and columns –Elements referenced with two subscripts ( [x][y] ) –In general, an array with m rows and n columns is called an m-by-n array Multidimensional arrays can have more than two dimensions

Fall 2013CISC2200 Yanjun Li17 Declaring & Initializing Two-Dimensional Arrays Declaring two-dimensional array b –int b[ 3 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[ 0 ][ 0 ] and b[ 0 ][ 1 ] –3 and 4 initialize b[ 1 ][ 0 ] and b[ 1 ][ 1 ] –0 and 0 initialize b[ 2 ][ 0 ] and b[ 2 ][ 1 ] (implicitly). Column 0Column 1 Row Row Row 2 0 0

Fall 2013CISC2200 Yanjun Li18 Two-Dimensional Array

Fall 2013CISC2200 Yanjun Li19 Passing Multidimensional Arrays to Functions Multidimensional array parameters –Size of first dimension is not required As with a one-dimensional array –Size of subsequent dimensions are required Compiler must know how many elements to skip to move to the second element in the first dimension –Example void printArray( const int a[][ 3 ] ); –Function will skip row 0 ’s 3 elements to access row 1 ’s elements ( a[ 1 ][ x ] )

Fall 2013CISC2200 Yanjun Li20 Implementation Level View stateHighs[ 0 ] [ 0 ] stateHighs[ 0 ] [ 1 ] stateHighs[ 0 ] [ 2 ] stateHighs[ 0 ] [ 3 ] stateHighs[ 0 ] [ 4 ] stateHighs[ 0 ] [ 5 ] stateHighs[ 0 ] [ 6 ] stateHighs[ 0 ] [ 7 ] stateHighs[ 0 ] [ 8 ] stateHighs[ 0 ] [ 9 ] stateHighs[ 0 ] [10 ] stateHighs[ 0 ] [11 ] stateHighs[ 1 ] [ 0 ] stateHighs[ 1 ] [ 1 ] stateHighs[ 1 ] [ 2 ] stateHighs[ 1 ] [ 3 ]. To locate an element such as stateHighs [ 2 ] [ 7] the compiler needs to know that there are 12 columns in this two-dimensional array. At what address will stateHighs [ 2 ] [ 7 ] be found? Assume 2 bytes for type int. Base Address 8000

Fall 2013CISC2200 Yanjun Li21 Multidimensional-Array Manipulations Commonly performed with for statements –Example Modify all elements in a row for ( int col = 0; col < 4; col++ ) a[ 2 ][ col ] = 0; –Example Total all elements total = 0; for ( int row = 0; row < 3; row++ ) for ( int col = 0; col < 4; col++ ) total += a[ row ][ col ];

Fall 2013CISC2200 Yanjun Li22 Pointer Types Pointer variable –A variable whose value is the address of a location in memory int* intPointer

Fall 2013CISC2200 Yanjun Li23 Pointer Types int alpha; int* intPointer; intPointer = α If alpha is at address 33, memory looks like this

Fall 2013CISC2200 Yanjun Li24 Pointer Types int x; x = 12; int* ptr; ptr = &x; NOTE: Because ptr holds the address of x, we say that ptr “points to” x x ptr

Fall 2013CISC2200 Yanjun Li25 Pointer Types Dereference operator (*) –An operator that, when applied to a pointer variable, denotes the variable to which the pointer points Dynamic allocation (new operator) –Allocation of memory space for a variable at run time (as opposed to static allocation at compile time)

Fall 2013CISC2200 Yanjun Li x ptr int x; x = 12; int* ptr; ptr = &x; std::cout << *ptr; *ptr is the value in the place to which ptr points Pointer Types

Fall 2013CISC2200 Yanjun Li27 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at adddress ptr to 5 Pointer Types x ptr

Fall 2013CISC2200 Yanjun Li28 char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // the right side has value 4000 // now p and q both point to ch Pointer Types 4000 A Z ch q p

Fall 2013CISC2200 Yanjun Li29 Pointer Types int * intPointer; intPointer = new int;

Fall 2013CISC2200 Yanjun Li30 Pointer Types NULL Pointer –A pointer that points to nothing; available in –NULL is defined to be 0; –But NULL is not memory address 0; int * intPtr = NULL; Float * money = NULL; intPtrmoney

Fall 2013CISC2200 Yanjun Li31 Pointer Types Memory Leak –The loss of available memory space that occurs when memory is allocated dynamically but never deallocated float * myMoney = new float; float * money = new float; myMoney = money; //wrong Garbage –Memory locations that can no longer be accessed To avoid memory leaks, use delete. delete myMoney; //correct myMoney = money;

Fall 2013CISC2200 Yanjun Li32 Object-Oriented Programming objects, sending a message, methods, instance variables…. Object –An instance of a class Method –A public member function of a class Instance variable (Data Member) –A private data member of a class

Fall 2013CISC2200 Yanjun Li33 C++ class data type A class is an unstructured type that encapsulates a fixed number of data components (data members) with the functions (called member functions) that manipulate them.

Fall 2013CISC2200 Yanjun Li34 class DateType Specification // SPECIFICATION FILE( datetype.h ) class DateType// declares a class data type { public : // 4 public member functions DateType (int newMonth,int newDay,int newYear);//constructor int getYear( ) const ; // returns year int getMonth( ) const ; // returns month int getDay( ) const ; // returns day private : // 3 private data members int year ; int month ; int day ; } ; 34

Fall 2013CISC2200 Yanjun Li35 2 separate files for class type // SPECIFICATION FILE ( datetype.h ) // Specifies the data and function members. class DateType { public:... private:... } ; // IMPLEMENTATION FILE ( datetype.cpp ) // Implements the DateType member functions....

Fall 2013CISC2200 Yanjun Li36 Code Using DateType #include “datetype.h” //includes specification of the class #include using namespace std; int main ( ) { // declares 2 objects of DateType DateType startDate ( 6, 30, 1998 ) ; DateType endDate ( 10, 31, 2002 ) ; bool retired = false ; cout << startDate.getMonth( )<< “/” << startDate.getDay( ) << “/” << startDate.getYear( ) << endl; while ( ! retired ) { finishSomeTask( ) ;... } return 0; } 36

Fall 2013CISC2200 Yanjun Li37 Implementation of member functions // IMPLEMENTATION FILE (datetype.cpp) #include “datetype.h” // also must appear in client code DateType :: DateType ( int newMonth, int newDay, int newYear ) // Post: year is set to newYear. // month is set to newMonth. // day is set to newDay. { year = newYear ; month = newMonth ; day = newDay ; } 37

Fall 2013CISC2200 Yanjun Li38 int DateType :: getMonth ( ) const // Accessor function for data member month { return month ; } int DateType :: getYear ( ) const // Accessor function for data member year { return year ; } int DateType :: getDay ( ) const // Accessor function for data member day { return day ; } 38

Fall 2013CISC2200 Yanjun Li39 Object-Oriented Programming Three ingredients in any object-oriented language –encapsulation –inheritance –polymorphism Just as a capsule protects its contents, the class construct protects its data members, but what are inheritance and polymorphism ?

Fall 2013CISC2200 Yanjun Li40 Object-Oriented Programming Inheritance –A mechanism used with a hierarchy of classes in which each descendant class inherits the properties (data and operations) of its ancestor class Base class –The class being inherited from Derived class –the class that inherits

Fall 2013CISC2200 Yanjun Li41 Object-Oriented Programming Inheritance is an "is-a" relationship: a wheeled vehicle is a vehicle; a bicycle is a wheeled vehicle a four-door car is a car…

Fall 2013CISC2200 Yanjun Li42 Object-Oriented Programming Overriding Function with virtual keyword in base class. Derived classes override function as appropriate. An overridden function in a derived class has the same signature and return type (i.e. prototype) as the function it overrides in its base class. Polymorphism The ability to determine which of several operations with the same name is appropriate; a combination of static and dynamic binding What is the root of the word polymorphism?

Fall 2013CISC2200 Yanjun Li43 Object-Oriented Programming Person Employee Manager Each class has a method Print Person.Print just prints the name Employee.Print prints the name and job title Manager.Print prints name, job title, and department Print is overriden. Static binding is when the compiler can tell which Print to use; dynamic binding is when the determination cannot be made until run time

Fall 2013CISC2200 Yanjun Li44 Object-Oriented Programming Inheritance and polymorphism work together How? They combine to allow the programmer to build useful hierarchies of classes that can be put into a library to be reused in different applications

Fall 2013CISC2200 Yanjun Li45 Reference Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale. Reproduced by permission of Jones and Bartlett Publishers International.