Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace defines scope –Place identifiers and variables within namespace –Access with namespace_name::member –Note guaranteed to be unique namespace Name { contents } –Unnamed namespaces are global Need no qualification –Namespaces can be nested

2  2003 Prentice Hall, Inc. All rights reserved. 2 namespaces using statement –using namespace namespace_name; –Members of that namespace can be used without preceding namespace_name:: Can also be used with individual member –Examples using namespace std –Discouraged by some programmers, because includes entire contents of std using namespace std::cout –Can write cout instead of std::cout

3  2003 Prentice Hall, Inc. All rights reserved. Outline 3 1 2 // Demonstrating namespaces. 3 #include 4 5 using namespace std; // use std namespace 6 7 int integer1 = 98; // global variable 8 9 // create namespace Example 10 namespace Example { 11 12 // declare two constants and one variable 13 const double PI = 3.14159; 14 const double E = 2.71828; 15 int integer1 = 8; 16 17 void printValues(); // prototype 18 19 // nested namespace 20 namespace Inner { 21 22 // define enumeration 23 enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 }; 24 25 } // end Inner 26 27 } // end Example Note the using statement. This includes all of std, allowing us to use cout and endl. Note the nested namespace Inner. Create a new namespace, Example. Note that it has a variable integer1, different from the global integer1.

4  2003 Prentice Hall, Inc. All rights reserved. Outline 4 28 29 // create unnamed namespace 30 namespace { 31 double doubleInUnnamed = 88.22; // declare variable 32 33 } // end unnamed namespace 34 35 int main() 36 { 37 // output value doubleInUnnamed of unnamed namespace 38 cout << "doubleInUnnamed = " << doubleInUnnamed; 39 40 // output global variable 41 cout << "\n(global) integer1 = " << integer1; 42 43 // output values of Example namespace 44 cout << "\nPI = " << Example::PI << "\nE = " 45 << Example::E << "\ninteger1 = " 46 << Example::integer1 << "\nFISCAL3 = " 47 << Example::Inner::FISCAL3 << endl; 48 49 Example::printValues(); // invoke printValues function 50 51 return 0; 52 53 } // end main Create an unnamed namespace. Its variables are global.

5  2003 Prentice Hall, Inc. All rights reserved. Outline 5 54 55 // display variable and constant values 56 void Example::printValues() 57 { 58 cout << "\nIn printValues:\ninteger1 = " 59 << integer1 << "\nPI = " << PI << "\nE = " 60 << E << "\ndoubleInUnnamed = " << doubleInUnnamed 61 << "\n(global) integer1 = " << ::integer1 62 << "\nFISCAL3 = " << Inner::FISCAL3 << endl; 63 64 } // end printValues

6  2003 Prentice Hall, Inc. All rights reserved. Outline 6 fig22_03.cpp output (1 of 1) doubleInUnnamed = 88.22 (global) integer1 = 98 PI = 3.14159 E = 2.71828 integer1 = 8 FISCAL3 = 1992 In printValues: integer1 = 8 PI = 3.14159 E = 2.71828 doubleInUnnamed = 88.22 (global) integer1 = 98 FISCAL3 = 1992

7  2003 Prentice Hall, Inc. All rights reserved. 7 Pointers Pointer variables –Contain memory addresses as values –Normally, variable contains specific value (direct reference) –Pointers contain address of variable that has specific value (indirect reference) Indirection –Referencing value through pointer Pointer declarations –* indicates variable is pointer int *myPtr; declares pointer to int, pointer of type int * –Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; count 7 countPtr count 7

8  2003 Prentice Hall, Inc. All rights reserved. 8 Pointer Variable Declarations and Initialization Can declare pointers to any data type Pointer initialization –Initialized to 0, NULL, or address 0 or NULL points to nothing

9  2003 Prentice Hall, Inc. All rights reserved. 9 Pointer Operators & (address operator) –Returns memory address of its operand –Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y –yPtr “points to” y yPtr y 5 yptr 500000600000 y 5 address of y is value of yptr

10  2003 Prentice Hall, Inc. All rights reserved. 10 Pointer Operators * (indirection/dereferencing operator) –Returns synonym for object its pointer operand points to –*yPtr returns y (because yPtr points to y ). –dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y * and & are inverses of each other

11  2003 Prentice Hall, Inc. All rights reserved. Outline 11 1 2 // Using the & and * operators. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer 12 13 a = 7; 14 aPtr = &a; // aPtr assigned address of a 15 16 cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; 18 19 cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; 21 22 cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 * and & are inverses of each other

12  2003 Prentice Hall, Inc. All rights reserved. Outline 12 26 return 0; // indicates successful termination 27 28 } // end main The address of a is 0012FED4 The value of aPtr is 0012FED4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012FED4 *&aPtr = 0012FED4 * and & are inverses; same result when both applied to aPtr

13  2003 Prentice Hall, Inc. All rights reserved. 13 5.6 Bubble Sort Using Pass-by-Reference Implement bubbleSort using pointers –Want function swap to access array elements Individual array elements: scalars –Passed by value by default Pass by reference using address operator &

14  2003 Prentice Hall, Inc. All rights reserved. Outline 14 1 2 // This program puts values into an array, sorts the values into 3 // ascending order, and prints the resulting array. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 #include 10 11 using std::setw; 12 13 void bubbleSort( int *, const int ); // prototype 14 void swap( int * const, int * const ); // prototype 15 16 int main() 17 { 18 const int arraySize = 10; 19 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 20 21 cout << "Data items in original order\n"; 22 23 for ( int i = 0; i < arraySize; i++ ) 24 cout << setw( 4 ) << a[ i ]; 25

15  2003 Prentice Hall, Inc. All rights reserved. Outline 15 26 bubbleSort( a, arraySize ); // sort the array 27 28 cout << "\nData items in ascending order\n"; 29 30 for ( int j = 0; j < arraySize; j++ ) 31 cout << setw( 4 ) << a[ j ]; 32 33 cout << endl; 34 35 return 0; // indicates successful termination 36 37 } // end main 38 39 // sort an array of integers using bubble sort algorithm 40 void bubbleSort( int *array, const int size ) 41 { 42 // loop to control passes 43 for ( int pass = 0; pass < size - 1; pass++ ) 44 45 // loop to control comparisons during each pass 46 for ( int k = 0; k < size - 1; k++ ) 47 48 // swap adjacent elements if they are out of order 49 if ( array[ k ] > array[ k + 1 ] ) 50 swap( &array[ k ], &array[ k + 1 ] ); Declare as int *array (rather than int array[] ) to indicate function bubbleSort receives single-subscripted array. Receives size of array as argument; declared const to ensure size not modified.

16  2003 Prentice Hall, Inc. All rights reserved. Outline 16 51 52 } // end function bubbleSort 53 54 // swap values at memory locations to which 55 // element1Ptr and element2Ptr point 56 void swap( int * const element1Ptr, int * const element2Ptr ) 57 { 58 int hold = *element1Ptr; 59 *element1Ptr = *element2Ptr; 60 *element2Ptr = hold; 61 62 } // end function swap Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 Pass arguments by reference, allowing function to swap values at memory locations.

17  2003 Prentice Hall, Inc. All rights reserved. 17 Pointer Expressions and Pointer Arithmetic Pointer arithmetic –Increment/decrement pointer (++ or -- ) –Add/subtract an integer to/from a pointer( + or +=, - or -= ) –Pointers may be subtracted from each other –Pointer arithmetic meaningless unless performed on pointer to array 5 element int array on a machine using 4 byte int s –vPtr points to first element v[ 0 ], which is at location 3000 vPtr = 3000 –vPtr += 2 ; sets vPtr to 3008 vPtr points to v[ 2 ] pointer variable vPtr v[0]v[1]v[2]v[4]v[3] 30003004300830123016 location

18  2003 Prentice Hall, Inc. All rights reserved. 18 Pointer Expressions and Pointer Arithmetic Subtracting pointers –Returns number of elements between two addresses vPtr2 = v[ 2 ]; vPtr = v[ 0 ]; vPtr2 - vPtr == 2 Pointer assignment –Pointer can be assigned to another pointer if both of same type –If not same type, cast operator must be used –Exception: pointer to void (type void * ) Generic pointer, represents any type No casting needed to convert pointer to void pointer void pointers cannot be dereferenced

19  2003 Prentice Hall, Inc. All rights reserved. 19 Pointer Expressions and Pointer Arithmetic Pointer comparison –Use equality and relational operators –Comparisons meaningless unless pointers point to members of same array –Compare addresses stored in pointers –Example: could show that one pointer points to higher numbered element of array than other pointer –Common use to determine whether pointer is 0 (does not point to anything)

20  2003 Prentice Hall, Inc. All rights reserved. 20 Relationship Between Pointers and Arrays Arrays and pointers closely related –Array name is a constant pointer –Pointers can do array subscripting operations Accessing array elements with pointers –Element b[ n ] can be accessed by *( bPtr + n ) Called pointer/offset notation –Addresses &b[ 3 ] same as bPtr + 3 –Array name can be treated as pointer b[ 3 ] same as *( b + 3 ) –Pointers can be subscripted (pointer/subscript notation) bPtr[ 3 ] same as b[ 3 ]

21  2003 Prentice Hall, Inc. All rights reserved. 21 Arrays of Pointers Arrays can contain pointers –Commonly used to store array of strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; –Each element of suit points to char * (a string) –Array does not store strings, only pointers to strings –suit array has fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’

22  2003 Prentice Hall, Inc. All rights reserved. 22 Fundamentals of Characters and Strings Character constant –Integer value represented as character in single quotes –'z' is integer value of z 122 in ASCII String –Series of characters treated as single unit –Can include letters, digits, special characters +, -, *... –String literal (string constants) Enclosed in double quotes, for example: "I like C++" –Array of characters, ends with null character '\0' –String is constant pointer Pointer to string’s first character –Like arrays

23  2003 Prentice Hall, Inc. All rights reserved. 23 Fundamentals of Characters and Strings String assignment –Character array char color[] = "blue"; –Creates 5 element char array color last element is '\0' –Variable of type char * char *colorPtr = "blue"; –Creates pointer colorPtr to letter b in string “blue” “blue” somewhere in memory –Alternative for character array char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };

24  2003 Prentice Hall, Inc. All rights reserved. 24 Fundamentals of Characters and Strings Reading strings –Assign input to character array word[ 20 ] cin >> word Reads characters until whitespace or EOF String could exceed array size cin >> setw( 20 ) >> word; Reads 19 characters (space reserved for '\0' )

25  2003 Prentice Hall, Inc. All rights reserved. 25 Fundamentals of Characters and Strings cin.getline –Read line of text –cin.getline( array, size, delimiter ); –Copies input into specified array until either One less than size is reached delimiter character is input –Example char sentence[ 80 ]; cin.getline( sentence, 80, '\n' );

26  2003 Prentice Hall, Inc. All rights reserved. 26 String Manipulation Functions of the String-handling Library String handling library provides functions to –Manipulate string data –Compare strings –Search strings for characters and other strings –Tokenize strings (separate strings into logical pieces)

27  2003 Prentice Hall, Inc. All rights reserved. 27 String Manipulation Functions of the String-handling Library char *strcpy( char *s1, const char *s2 ); Copies the string s2 into the character array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ); Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ); Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ); Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively.

28  2003 Prentice Hall, Inc. All rights reserved. 28 String Manipulation Functions of the String-handling Library int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. char *strtok( char *s1, const char *s2 ); A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—delimited by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current to­ken is returned by each call. If there are no more tokens when the function is called, NULL is returned. size_t strlen( const char *s ); Determines the length of string s. The number of characters preceding the terminating null character is returned.

29  2003 Prentice Hall, Inc. All rights reserved. 29 String Manipulation Functions of the String-handling Library Copying strings –char *strcpy( char *s1, const char *s2 ) Copies second argument into first argument –First argument must be large enough to store string and terminating null character –char *strncpy( char *s1, const char *s2, size_t n ) Specifies number of characters to be copied from string into array Does not necessarily copy terminating null character

30  2003 Prentice Hall, Inc. All rights reserved. 30 String Manipulation Functions of the String-handling Library Concatenating strings –char *strcat( char *s1, const char *s2 ) Appends second argument to first argument First character of second argument replaces null character terminating first argument Ensure first argument large enough to store concatenated result and null character –char *strncat( char *s1, const char *s2, size_t n ) Appends specified number of characters from second argument to first argument Appends terminating null character to result

31  2003 Prentice Hall, Inc. All rights reserved. 31 String Manipulation Functions of the String-handling Library Comparing strings –int strcmp( const char *s1, const char *s2 ) Compares character by character Returns –Zero if strings equal –Negative value if first string less than second string –Positive value if first string greater than second string –int strncmp( const char *s1, const char *s2, size_t n ) Compares up to specified number of characters Stops comparing if reaches null character in one of arguments

32  2003 Prentice Hall, Inc. All rights reserved. 32 String Manipulation Functions of the String-handling Library Determining string lengths –size_t strlen( const char *s ) Returns number of characters in string –Terminating null character not included in length


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace."

Similar presentations


Ads by Google