1 object-oriented programming using c++ a tutorial for pointers

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

Upload: violet-carol-french

Post on 18-Jan-2018

239 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

1

Object-Oriented Programming

Using C++

A tutorial for pointers

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

2

Obj

ectiv

es •Read chapter 8

•Declare, initialize and use pointers

• Demonstrate the relationship between pointers and built-in arrays.

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

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)

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

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

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

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

}

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

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;

}

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

7

// call to the function inside maincubeByReference( &number );

void cubeByReference( int *nPtr )

{

*nPtr = *nPtr * *nPtr * *nPtr;

}

number (at 3fe0)

5

nPtr

3fe0

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

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;

}

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

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;

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

18

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

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

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}

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

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}

H e l l o \0

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

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}

H e l l o \0

// when i equals 5

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

22

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

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

23

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

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

24

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

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

25

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

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

26

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

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

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 o o d B

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

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 o d B y

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

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 d B y e

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

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 B y e \0

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

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 stringsEach element of the array contains the address of the first character in each string

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

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);

Heartsarts

d

Clubs // same as suit[2]

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

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

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

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?

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

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

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

36

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

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

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

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

38

Do self-review exercises 8.1, 8.2, 8.3,8.4 pages 364-365

Check your answers on page 366