1 / 32

namespaces

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 }

parmijo
Download Presentation

namespaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. 1 2 // Demonstrating namespaces. 3 #include <iostream> 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 doublePI = 3.14159; 14 const doubleE = 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. Create a new namespace, Example. Note that it has a variable integer1, different from the global integer1. Note the nested namespace Inner.

  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 return0; 52 53 } // end main Create an unnamed namespace. Its variables are global.

  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. 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 fig22_03.cppoutput (1 of 1)

  7. count countPtr count 7 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;

  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. yptr y y 5 yPtr 500000 600000 600000 5 address of y is value of yptr 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

  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. 1 2 // Using the & and * operators. 3 #include <iostream> 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. 26 return0; // indicates successful termination 27 28 } // end main * and & are inverses; same result when both applied to aPtr 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

  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. 1 2 // This program puts values into an array, sorts the values into 3 // ascending order, and prints the resulting array. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 void bubbleSort( int *, constint ); // prototype 14 void swap( int * const, int * const ); // prototype 15 16 int main() 17 { 18 const intarraySize = 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. 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 return0; // 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 ] ); Receives size of array as argument; declared const to ensure size not modified. Declare as int *array (rather than int array[]) to indicate function bubbleSort receives single-subscripted array.

  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 Pass arguments by reference, allowing function to swap values at memory locations. 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

  17. location 3000 3004 3008 3012 3016 pointer variable vPtr v[0] v[1] v[2] v[4] v[3] 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 ints • 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 ]

  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. 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. 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. ’\0’ ’\0’ ’\0’ ’\0’ ’n’ ’d’ ’o’ ’u’ ’a’ ’s’ ’d’ ’b’ ’m’ ’H’ ’s’ ’a’ ’D’ ’i’ ’a’ ’s’ ’l’ ’C’ ’r’ ’s’ ’S’ ’p’ ’e’ ’t’ ’e’ suit[0] suit[1] suit[2] suit[3] 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

  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. 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. 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. 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. String Manipulation Functions of the String-handling Library • String handling library <cstring> provides functions to • Manipulate string data • Compare strings • Search strings for characters and other strings • Tokenize strings (separate strings into logical pieces)

  27. String Manipulation Functions of the String-handling Library

  28. String Manipulation Functions of the String-handling Library

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

More Related