Professional Documents
Culture Documents
Lecture 9 - Introduction On OOP - 2
Lecture 9 - Introduction On OOP - 2
Concordia University
10/24/2022 2
Specification – Implementation
• Any other cpp file that you create can use the class Time if you
include both time.h and time.cpp
3
Definition of Class: Cylinder
4
Implementation of Class Cylinder
5
Using the Class Cylinder
6
Class string
7
Using Strings
8
Using Strings (cont.)
• Range checking
• s3.at( index );
• Returns character at index
• [] has no range checking
• Concatenation
• s3.append( "pet" );
• s3 += "pet";
• Both add "pet" to end of s3
• s3.append( s1, start, N );
• Appends N characters from s1, beginning at index start
9
Using Strings (cont.)
Output
Line 182, 186: String initialization and assignment.
string1: cat
string2: cat
string3: cat
10
Using Strings (cont.)
11
Using Strings (cont.)
string1: cat
string2: cat
string3: cat
After concatenation:
string1: catacomb
string2: rat
string3: carpet
string4: catapult
string5: comb
12
Comparing Strings
13
Comparing Strings (cont.)
14
Comparing Strings (cont.)
Note use of
compare.
15
Comparing Strings (cont.)
16
Comparing Strings (cont.)
17
Substrings
18
Substrings (cont.)
plane
19
Swapping Strings
20
Swapping Strings (cont.)
Before swap:
first one
Second two
After swap:
first two
Second one
21
String Characteristics
• Member functions
• s1.size() and s1.length()
• Number of characters in string
• s1.capacity()
• Number of elements that can be stored without reallocation
• s1.max_size()
• Maximum possible string size
• s1.empty()
• Returns true if empty
• s1.resize(newlength)
• Resizes string to newlength
22
String Characteristics (cont.)
23
String Characteristics (cont.)
Resize string.
24
String Characteristics (cont.)
Statistics before input:
capacity: 0
max size: 4294967293
size: 0
length: 0
empty: true
• Find functions
• If found, index returned
• If not found, string::npos returned
• Public static constant in class string
• s1.find( s2 )
• s1.rfind( s2 )
• Searches right-to-left
• s1.find_first_of( s2 )
• Returns first occurrence of any character in s2
s1.find_first_of( "abcd" )
• Returns index of first 'a', 'b', 'c' or 'd'
26
Finding Strings and Characters (cont.)
• Find functions
• s1.find_last_of( s2 )
• Finds last occurrence of any character in s2
• s1.find_first_not_of( s2 )
• Finds first character NOT in s2
• s1.find_last_not_of( s2 )
• Finds last character NOT in s2
27
Finding Strings and Characters (cont.)
28
Finding Strings and Characters (cont.)
string::npos (public
static constant defined in
class string) is returned.
29
Finding Strings and Characters (cont.)
Original string:
noon is 12 pm; midnight is not.
(find_first_not_of) '1' is not contained in "noi spm" and was found at:8
(find_first_not_of) '.' is not contained in " noon is 12 pm; midnight is not. " and was found
at:13
30
String Manipulation Summary
31
String Manipulation Summary (cont.)
32
String Manipulation Summary (cont.)
33