1 / 52

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++. CLASS 2. Declare, initialize and use a pointer and an array of pointers Create a string Use built-in string functions. Objectives.

chynna
Télécharger la présentation

Object-Oriented Programming Using C++

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. Object-Oriented Programming Using C++ CLASS 2

  2. Declare, initialize and use a pointer and an array of pointers • Create a string • Use built-in string functions Objectives

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

  4. // Cube a variable using call-by-value #include <iostream>using namespace std;int cubeByValue( int ); // prototypeint main() {int number = 5; cout << "The original value of number is” << number;number =cubeByValue( number );cout << "\nThe new value of number is “ << number << endl; return 0; }

  5. int cubeByValue( int n ) { // n is a local variable which is destroyed when the // the function ends return n * n * n; // cube local variable n }

  6. // Cube a variable using call by // reference with a pointer argumentvoid cubeByReference( int * ); int main(){ int number = 5; cout << "The original value of number” << “ is " << number; cubeByReference(&number); cout << "\nThe new value of number is” << number << endl; return 0; }

  7. // call to the function inside maincubeByReference(&number); void cubeByReference(int *nPtr) { *nPtr = *nPtr * *nPtr * *nPtr; } 5 number (at 3fe0) 3fe0 nPtr

  8. // Converting lowercase letters to uppercase #include <iostream>using namespace std;#include <cctype>void convertToUppercase(char *);int main(){ char string[ ] = "characters and $32.98"; cout << "The string before conversion is: “ << string; convertToUppercase(string); // string is a pointer cout << "\nThe string after conversion is:” << string << endl;return 0; }

  9. void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { if ( islower( *sPtr ) ) *sPtr = toupper( *sPtr ); ++sPtr; // move sPtr to the next character } } Contains the address of the “c” sPtr characters and $32.98 string inside main

  10. void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { if ( islower( *sPtr ) ) *sPtr = toupper( *sPtr ); ++sPtr; // move sPtr to the next character } } Contains the address of the “h” sPtr Characters and $32.98 string inside main

  11. void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { if ( islower( *sPtr ) ) *sPtr = toupper( *sPtr ); ++sPtr; // move sPtr to the next character } } Contains the address of the “a” sPtr CHaracters and $32.98 string inside main

  12. void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { if ( islower( *sPtr ) ) *sPtr = toupper( *sPtr ); ++sPtr; // move sPtr to the next character } } Contains the address of the “r” sPtr CHAracters and $32.98 string inside main

  13. // Attempting to modify a constant pointer to// constant data. int main() { int x = 5, y; const int *const ptr = &x; cout << *ptr << endl; *ptr = 7; ptr = &y; return 0; }

  14. // Attempting to modify a constant pointer to// constant data. int main() { int x = 5, y; const int *const ptr = &x; cout << *ptr << endl; *ptr = 7; // not legal because ptr = &y; // not legal because return 0; }

  15. // Using subscripting and pointer// notations with arrays int main() { int b[ ] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘ // i equals 0<< *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';

  16. // Using subscripting and pointer// notations with arrays int main() { int b[] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n'; // i equals 0 << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';

  17. // Using subscripting and pointer// notations with arrays int main() { int b[] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n';<< bPtr[ i ] << '\n'; // i equals 0 << *( bPtr + i ) << '\n';

  18. // Using subscripting and pointer// notations with arrays int main() { int b[ ] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n'; << bPtr[ i ] << '\n';<< *( bPtr + i ) << '\n'; // i equals 0

  19. // Fig. 5.20: fig05_20.cpp// Using subscripting and pointer// notations with arrays int main() { int b[ ] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘ // i equals 1<< *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';

  20. // Using subscripting and pointer// notations with arrays int main() { int b[] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n'; // i equals 1 << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';

  21. // Using subscripting and pointer// notations with arrays int main() { int b[] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n';<< bPtr[ i ] << '\n'; // i equals 1 << *( bPtr + i ) << '\n';

  22. // Using subscripting and pointer// notations with arrays int main() { int b[ ] = { 10, 20, 30, 40 }, i, offset; int *bPtr = b; // set bPtr to point to array b for ( i = 0; i < 4; i++ ) cout << b[ i ] << '\n‘<< *( b + i ) << '\n'; << bPtr[ i ] << '\n';<< *( bPtr + i ) << '\n'; // i equals 1

  23. // Copying a string using array notation// and pointer notation.#include <iostream>using namespace std;void copy1( char *, const char * );void copy2( char *, const char * );int main(){ char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";copy1( string1, string2 ); cout << "string1 = " << string1 << endl; copy2( string3, string4 ); cout << "string3 = " << string3 << endl; return 0; }

  24. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";copy1( string1, string2 ); // in main_________________________________void copy1( char *s1, const char *s2 ){ for ( int i = 0; (s1[ i ] = s2[ i ]) != '\0'; i++ ) ; // do nothing in body}

  25. // in main_________________________________ char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy1( string1, string2 ); // in main_________________________________void copy1( char *s1, const char *s2 ){ for ( int i = 0; (s1[ i ] = s2[ i ]) != '\0'; i++ ) ; // do nothing in body} H e l l o \0

  26. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";copy1( string1, string2 ); // in main_________________________________void copy1( char *s1, const char *s2 ){ for ( int i = 0; (s1[ i ] = s2[ i ]) !='\0'; i++ ) ; // do nothing in body} H e l l o \0 // when i equals 5

  27. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G

  28. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o

  29. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o

  30. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d

  31. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d

  32. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d B

  33. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d B y

  34. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d B y e

  35. // in main_________________________________char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye"; copy2( string3, string4 ); // in main_________________________________// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body} G o o d B y e \0

  36. const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] suit[3] sizeof (suit) is 16 if pointers are 4 bytes 8 if pointers are 2 bytes

  37. const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] suit[3] cout << suit[0]; Hearts arts cout << (suit[0] + 2); d cout << *(suit[3] + 3); Clubs // same as suit[2] cout << *(suit + 2);

  38. String functions review: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 string1strcat(string1,string2);// appends the entire string2 string to the end// of the string1 beginning at the null

  39. const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] suit[3] char newstring[50]; srand(time(0)); // randomize numbers with clock time // needs include files cstdlib and ctime int n = rand() % 4; strcpy(newstring,suit[n]); n = rand() % 4; strcat(newstring,suit[n]);

  40. const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] suit[3] char newstring[50]; C l u b s \0 srand(time(0)); // randomize numbers with clock time // needs include files cstdlib and ctime int n = rand() % 4; strcpy(newstring,suit[n]); // if n is 2 Clubs is copied

  41. const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] suit[1] suit[2] suit[3] char newstring[50]; C l u b s H e a r t s \0 srand(time(0)); // randomize numbers with clock time // needs include files cstdlib and ctime int n = rand() % 4; strcpy(newstring,suit[n]); // if n is 2 Clubs is copied n = rand() % 4; strcat(newstring,suit[n]); // if n is 0 Hearts is appended

  42. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (s1 = = s2) cout << “strings are equal”; else cout << “strings are not equal”;

  43. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (s1 = = s2) cout << “strings are equal”; else cout << “strings are not equal”; // why?

  44. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (strcmp(s1,s2) == 0) cout << “strings are equal”; else cout << “strings are not equal”;

  45. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (strncmp(s1 + 1,s3 + 1,4) == 0) cout << “strings are equal”; else cout << “strings are not equal”;

  46. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (strncmp(s1,s3 ,6) == 0) cout << “strings are equal”; else cout << “strings are not equal”;

  47. String compare: char *s1 = “Happy New Year”;char *s2 = “Happy New Year”; char *s3 = “Happy Holidays”; if (strcmp(s1,s3 ) == 0) cout << “strings are equal”;else if ((strcmp s1,s3 ) < 0) cout << “string1 is less than string3); else cout << “string1 is greater than string3”;

  48. String compare:warning:strcmp returns a negative value if string1 is less than string2 (not a –1)strcmp returns a positive value if string1 is greater than string2 (not a +1)strcmp returns a 0 if the strings are equal

  49. strtok breaking a string into tokens • First call in a sequence of code to the function strtok contains two arguments • A string to be tokenized • A string containing characters that separate the tokens (delimeters) char string[ ]= “This is a sentence,with 7 tokens”;char *tokenPtr;tokenPtr = (string,” ,“); // a space and a comma as // delimeters

  50. strtok breaking a string into tokens • Subsequent calls in a sequence of code to the function strtok contain two arguments • A NULL string • A string containing characters that separate the tokens (delimeters) char string[ ]= “This is a sentence,with 7 tokens”;char *tokenPtr;tokenPtr = (string,” ,“); // returns a pointer to the T // and places a \0 in place // of the space saving another // pointer to the i in the word istokenPtr = strtok(NULL, “ ,”);

More Related