Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object-Oriented Programming Using C++ A tutorial for pointers.

Similar presentations


Presentation on theme: "1 Object-Oriented Programming Using C++ A tutorial for pointers."— Presentation transcript:

1 1 Object-Oriented Programming Using C++ A tutorial for pointers

2 2 Objectives Read chapter 8 Declare, initialize and use pointers Demonstrate the relationship between pointers and built-in arrays.

3 3 int main() { int a; // a is an integer int *aPtr; // aPtr is a pointer to an integer a = 7; // assign the value 7 to the variable a aPtr = &a; // aPtr set to memory address // of the variable a cout << a; // displays the contents of variable a // displays a 7 cout << aPtr; // displays the contents of // variable aPtr which contains the // memory address of the variable a cout << *aPtr; // dereference operator * // says get the data that aPtr points to // displays a 7 (the contents of a)

4 4 // Cube a variable using call-by-value #include using namespace std; int cubeByValue( int ); // prototype int 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 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 6 // Cube a variable using call by // reference with a pointer argument void 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 7 // call to the function inside main cubeByReference( &number ); void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } number (at 3fe0) 5 nPtr 3fe0

8 8 // 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; }

9 9 // 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; }

10 10 // Relationship between pointers and built-in arrays // Using subscripting and pointer // notations with arrays int main() { int b[ ] = { 10, 20, 30, 40 }, i; 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';

11 11 // Using subscripting and pointer // notations with arrays int main() { int b[] = { 10, 20, 30, 40 }, i; 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';

12 12 // 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';

13 13 // 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

14 14 // 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';

15 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‘ << *( b + i ) << '\n'; // i equals 1 << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';

16 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'; << bPtr[ i ] << '\n'; // i equals 1 << *( bPtr + i ) << '\n';

17 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'; << *( bPtr + i ) << '\n'; // i equals 1

18 18 // Copying a string using array notation // and pointer notation. #include 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; }

19 19 // 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 }

20 20 // 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 } Hello\0

21 21 // 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 } Hello\0 // when i equals 5

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

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

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

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

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

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

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

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

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

31 31 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 An array of pointers to strings Each element of the array contains the address of the first character in each string

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

33 33 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”;

34 34 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?

35 35 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 using strcmp within # include

36 36 String compare using strcmp within # include 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”;

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

38 38 Do self-review exercises 8.1, 8.2, 8.3,8.4 pages 364-365 Check your answers on page 366


Download ppt "1 Object-Oriented Programming Using C++ A tutorial for pointers."

Similar presentations


Ads by Google