IDLN Template - Powerpoint 1997

Download Report

Transcript IDLN Template - Powerpoint 1997

Object-Oriented
Programming
Using C++
CLASS 2
1
Objectives
•Declare, initialize and use a
pointer and an array of pointers
•Create a string
•Use built-in string functions
2
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
3
// Cube a variable using call-by-value
#include <iostream>
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; }
4
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
}
5
// 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;
}
6
// call to the function inside main
cubeByReference( &number );
5
number (at 3fe0)
void cubeByReference( int *nPtr )
3fe0
{
*nPtr = *nPtr * *nPtr * *nPtr;
nPtr
}
7
// 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; }
8
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
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 “h”
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 “a”
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 “r”
sPtr
CHAracters and $32.98
string inside main
12
// 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;
}
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; // not legal because
ptr = &y; // not legal because
return 0;
}
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 0
<< *( b + i ) << '\n';
<< bPtr[ i ] << '\n';
<< *( bPtr + i ) << '\n';
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 0
<< 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';
<< bPtr[ i ] << '\n'; // i equals 0
<< *( 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';
<< *( bPtr + i ) << '\n'; // i equals 0
18
// 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';
19
// 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';
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';
<< bPtr[ i ] << '\n'; // i equals 1
<< *( 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';
<< *( bPtr + i ) << '\n'; // i equals 1
22
// 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; }
23
// 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
}
24
// in main_________________________________
H e l l o \0
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_________________________________
H e l l o \0
char string1[ 10 ], *string2 = "Hello",
string3[ 10 ], string4[ ] = "Good Bye";
copy1( string1, string2 );
// in main_________________________________
void copy1( char *s1, const char *s2 )
{
// when i equals 5
for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ )
; // do nothing in body
}
26
// in main_________________________________
G
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
}
27
// in main_________________________________
G o
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
}
28
// in main_________________________________
G o o
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
}
29
// in main_________________________________
G o o d
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
}
30
// in main_________________________________
G o o d
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
}
31
// in main_________________________________
G o o d
B
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
}
32
// in main_________________________________
G o o d
B y
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
}
33
// in main_________________________________
G o o d
B y e
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
}
34
// in main_________________________________
G o o d
B y e \0
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
}
35
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
36
const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
suit[0] suit[1] suit[2] suit[3]
cout << suit[0];
Hearts
cout << (suit[0] + 2);
arts
cout << *(suit[3] + 3);
d
cout << *(suit + 2);
Clubs
// same as suit[2]
37
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 string1
strcat(string1,string2);
// appends the entire string2 string to the end
// of the string1 beginning at the null
38
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]);
39
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
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
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
41
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”;
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”;
// why?
43
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”;
44
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”;
45
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”;
46
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”;
47
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
48
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
49
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 is
tokenPtr = strtok(NULL, “ ,”);
50
char string[ ]= “This is a sentence,with 7 tokens”;
char *tokenPtr;
tokenPtr = strtok(string,” ,“);
while (tokenPtr != NULL)
{ cout << tokenPtr << ‘\n’;
tokenPtr = strtok(NULL, “ ,”); }
This
is
a
sentence
with
7
tokens
51
Q&A
52