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

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

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

1

CSE 2341Object Oriented Programming

with C++

Note Set #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

Address Operator - &

int amount = 25;

&amount;

• & returns the memory address of where variable amount is stored in the computers memory• cout << &amount would print thememory address to the console

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 defineddata type

Indicates declarationof pointer variable

Note: T is used to represent any valid data typein a great deal of C++ literature.

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

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

cubeByRef(int *)

void cubeByRef(int* pVar){ *pVar = *pVar * *pVar * *pVar;}

Function Call From Main:cubeByRef(&number);

provides the address of number to pointer variablepVar.

5number

(stored at 3fe0)

3fe0pVar

(stored at 3fe0)

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

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.22str from main

Chars and $32.22

address of ‘h’

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.22str from main

CHars and $32.22

address of ‘a’

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.22str from main

CHArs and $32.22

address of ‘r’

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

//Illegal

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

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

Subscript vs. Pointer Offset Notation

int arr[] = {10, 11, 12, 13};int* ptr;ptr = arr;

ptr //address of 10ptr +2 //address of ____*(ptr + 2) //value ____

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

0i

Will alldisplay 10

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

1i

Will alldisplay 11

18

Copying Stringsvoid 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

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

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

??????? ???H e l l o \0

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

??????? ???G o o d b y e \0

22

Array of Pointers to Strings

const char* suit[4] = {“Hearts”, “Clubs”, “Diamonds”, “Spades” };

H e a r t s \0

C l u b s \0

D i a m o n \0d s

S p a d e s \0

suit

cout << suit[2]+4; _________

cout << *(*suit + 1); _________

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

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

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

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 equalOutput:

WHY??

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 equalOutput:

WHY??

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

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

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

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 << “S1 > S3” << endl;

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

strtok

• String tokenizing means to break a string up into component tokens using a delimiter– Ex: string = helloworld

delimiter = w tokens = hello, orld

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

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, “ ,”); }

Thisisasentencewith7tokens

Output:

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 1st 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

Fini

?