520 likes | 591 Views
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.
E N D
Declare, initialize and use a pointer and an array of pointers • Create a string • Use built-in string functions Objectives
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
// 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; }
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 }
// 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; }
// call to the function inside maincubeByReference(&number); void cubeByReference(int *nPtr) { *nPtr = *nPtr * *nPtr * *nPtr; } 5 number (at 3fe0) 3fe0 nPtr
// 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; }
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
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
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
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
// 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; }
// 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; }
// 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';
// 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';
// 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';
// 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
// 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';
// 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';
// 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';
// 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
// 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; }
// 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}
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
// 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
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
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);
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
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]);
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
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
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”;
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?
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”;
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”;
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”;
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”;
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
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
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, “ ,”);