Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Fall 2013CISC2200 Yanjun Li1 Review. Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming."— Presentation transcript:

1 Fall 2013CISC2200 Yanjun Li1 Review

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

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

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

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

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

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

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

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

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

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

12 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

13 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

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

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

16 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

17 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 0 1 2 Row 1 3 4 Row 2 0 0

18 Fall 2013CISC2200 Yanjun Li18 Two-Dimensional Array

19 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 ] )

20 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

21 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 ];

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

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

24 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 2000 12 x 3000 2000 ptr

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

26 Fall 2013CISC2200 Yanjun Li26 2000 12 x 3000 2000 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

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

28 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 5000 6000 4000 4000 q p

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

30 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

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

32 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

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

34 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

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

36 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

37 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

38 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

39 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 ?

40 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

41 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…

42 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?

43 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

44 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

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


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

Similar presentations


Ads by Google