Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 2341 Object Oriented Programming with C++ Note Set #2.

Similar presentations


Presentation on theme: "1 CSE 2341 Object Oriented Programming with C++ Note Set #2."— Presentation transcript:

1 1 CSE 2341 Object Oriented Programming with C++ Note Set #2

2 2 Quick Look Review of –Declaration, Initialization and use of pointers and arrays of pointers –Creation of c-strings –Use of built-in string functions

3 3 Address Operator - & int amount = 25; &amount; & returns the memory address of where variable amount is stored in the computers memory cout << &amount would print the memory address to the console

4 4 Pointer Declaration T* name; Declares a pointer variable that can store the address of a memory location that stores data of type T. Any previously defined data type Indicates declaration of pointer variable Note: T is used to represent any valid data type in a great deal of C++ literature.

5 5 Example int main() { int a; // a is an integer int *aPtr; // aPtr is a pointer to //an integer a = 7; aPtr = &a; // aPtr set to address of a cout << a; // displays 7 cout << aPtr; // displays the memory // location of a cout << *aPtr; // displays 7 return 0; }

6 6 Pass by Reference with Pointer Parameter void cubeByRef(int *); int main() { int number = 5; cout << “Original Value = “ << number << endl; cubeByRef(&number); cout << “New Value = “ << number << endl; return 0; }

7 7 cubeByRef(int *) void cubeByRef(int* pVar) { *pVar = *pVar * *pVar * *pVar; } Function Call From Main: cubeByRef(&number); provides the address of number to pointer variable pVar. 5 number (stored at 3fe0) 3fe0 pVar (stored at 3fe0)

8 8 Converting Upper- to Lowercase void convertUp(char *); int main() { char str[] = “chars and $32.22”; cout << “Before: “ << str << endl; convertUp(str); cout << “After: “ << str << endl; return 0; }

9 9 convertUp(char *) void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ sPtr chars and $32.22 str from main Chars and $32.22 address of ‘h’

10 10 convertUp(char *) void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ sPtr Chars and $32.22 str from main CHars and $32.22 address of ‘a’

11 11 convertUp(char *) void convertUp(char * sPtr) { while(*sPtr != ‘\0’) { if(islower(*sPtr)) *sPtr = toupper(*sPtr) ++sPtr; //move to next character } } address of ‘c’ sPtr CHars and $32.22 str from main CHArs and $32.22 address of ‘r’

12 12 const-ness with pointers const disallows changes to values checked by the compiler int x = 5, y; const int * const myPtr = &x; *myPtr = 7; ptr = &y; //Illegal

13 13 Const-ness with pointers int x = 5, y; const int * const myPtr = &x; *myPtr = 7; ptr = &y; Disallows modification of the value pointed to by myPtr Disallows modification of the address stored in myPtr

14 14 Arrays and Pointers int arr[] = {10, 11, 12, 13}; int* ptr; arr holds the address of the first element of the array (10) ptr = arr; Now, ptr and arr both point to the first element of the array (10)

15 15 Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; ptr //address of 10 ptr +2 //address of ____ *(ptr + 2) //value ____

16 16 Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; for (int i = 0; i < 4; i++) { cout << arr[i] << endl; cout << *(arr + i) << endl; cout << *(ptr + i) << endl; cout << ptr[i] << endl; } 0 i Will all display 10

17 17 Subscript vs. Pointer Offset Notation int arr[] = {10, 11, 12, 13}; int* ptr; ptr = arr; for (int i = 0; i < 4; i++) { cout << arr[i] << endl; cout << *(arr + i) << endl; cout << *(ptr + i) << endl; cout << ptr[i] << endl; } 1 i Will all display 11

18 18 Copying Strings void copy1(char*, const char*); void copy2(char*, const char*); int main() { char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); cout << “s1 = “ << s1 << endl; copy2(s3, s4); cout << “s3 = “ << s3 << endl; return 0; }

19 19 copy1(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); void copy1(char* a, const char* b) { for(int i=0; (a[i]=b[i])!=‘\0’; i++); } Body of for loop is empty. Action happens in the condition check.

20 20 copy1(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy1(s1, s2); void copy1(char* a, const char* b) { for(int i=0; (a[i]=b[i])!=‘\0’; i++); } ??????????Hello\0

21 21 void copy1(char* a, const char* b) { for(; (*a = *b)!=‘\0’; a++, b++); } copy2(char*, const char*) //FROM MAIN: char s1[10], *s2 = “Hello”; char s3[10], s4[] = “Goodbye”; copy2(s3, s4); ??????????Goodbye\0

22 22 Array of Pointers to Strings const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” }; Hearts \0 Clubs Diamon ds Spades suit cout << suit[2]+4; _________ cout << *(*suit + 1); _________

23 23 Arrays of Pointers to Strings cout << suit[2]+4; _________ cout << *(*suit + 1); _________ cout << suit[0]; _________ cout << *(suit[3] + 3); _________ const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” };

24 24 String Functions strcpy(string1,string2); // copies entire contents(including the null) of // string2 to string1, replacing anything that is // in string1; strncpy(string1,string2,5); // copies at most 5 characters from string2 to // the beginning of string1; null is not copied; // programmer must place the null in string1 strcat(string1,string2); // appends the entire string2 string to the end // of the string1 beginning at the null

25 25 String Functions const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” }; char str[25]; char * strPtr; strcpy(str, suit[1]); strcat(str, suit[3]); cout << str << endl; strncpy(strPtr, suit[0], 3); cout << strPtr << endl; Any problems with this code? What will be displayed?

26 26 Comparing Strings char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(s1 == s2) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl; Strings not equal Output: WHY??

27 27 Comparing Strings char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(s1 == s2) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl; Strings not equal Output: WHY??

28 28 String Compare - strcmp strcmp(s1, s2) returns –a negative number if s1 is smaller than s2 –a positive number if s1 is larger than s2 –0 if s1 is the same as s2 char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strcmp(s1, s2) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;

29 29 String Compare - strncmp strncmp(s1, s2, x) returns –a negative number if s1 is smaller than s2 for x characters –a positive number if s1 is larger than s2 for x characters –0 if s1 is the same as s2 for x characters char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strncmp(s1+1, s3+1, 4) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;

30 30 String Compare - strncmp char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strncmp(s1, s3, 6) == 0) cout << “Strings equal” << endl; else cout << “Strings not equal” << endl;

31 31 String Compare - strncmp char* s1 = “Happy New Year”; char* s2 = “Happy New Year”; char* s3 = “Happy Holidays”; if(strcmp(s1, s3) == 0) cout << “Strings equal” << endl; else if(strcmp(s1, s3) < 0) cout << “S1 < S3” << endl; else cout S3” << endl;

32 32 strcmp Review: –strcmp returns a negative number if string1 is less than string2 (not a -1) –Strcmp returns a positive number is string1 is greater than string2 (not a +1 –Strcmp returns 0 if string1 is equal to string2

33 33 strtok String tokenizing means to break a string up into component tokens using a delimiter –Ex: string = helloworld delimiter = w tokens = hello, orld

34 34 strtok First call in a sequence of code to the function strtok contains two arguments –The string to be tokenized –A string containing characters that separate the tokens (delimiters) char* str = “This is a sentence, with 7 tokens”; char* delim = “,”; char* tokenPtr; tokenPtr = strtok(str, delim);

35 35 strtok When strtok can find no more tokens, it will return NULL char* string=“This is a sentence,with 7 tokens”; char *tokenPtr; tokenPtr = (string,”,“); while (tokenPtr != NULL) { cout << tokenPtr << ‘\n’; tokenPtr = strtok(NULL, “,”); } This is a sentence with 7 tokens Output:

36 36 strtok Subsequent calls to strtok –arg1: NULL –arg2: string of delimiters (does not have to be the same as the set of delims for the 1 st call) char* str = “This is a sentence, with 7 tokens”; char* delim = “,”; char* tokenPtr; tokenPtr = strtok(str, delim); cout << tokenPtr << endl; tokenPtr = strtok(NULL, delim); cout << tokenPtr << endl;

37 37 Fini ?


Download ppt "1 CSE 2341 Object Oriented Programming with C++ Note Set #2."

Similar presentations


Ads by Google