Download presentation
Presentation is loading. Please wait.
Published byRichard Wade Modified over 9 years ago
1
Chapter 13 – C++ String Class
2
String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region to store text as needed u Can use operators to perform some string manipulations Lesson 13.1
3
Declaring string Objects u Use the class name string and list object names u Example: string s1, s2, s3; –s1, s2, s3 would be string objects u String header needed to declare string objects u Do NOT specify size (Note: no brackets)
4
Initializing string Objects u Can initialize with the = –C++ automatically reserves sufficient memory u Can also place in ( ) but still need " " u Null character not required for string text u Data member of string class stores size of text Lesson 13.1 s1 = "This is an example.";
5
Operators + string Objects Lesson 13.1 Type OperatorAction Assignment=Stores string +=Concatenates and stores Comparison==True if strings identical !=True if strings not identical >True if first string greater than second <True if first string is less than second >=True if first string greater or equal than second <=True if first string less or equal than second Input/Output>>For input and string objects <<For output and string objects Character[ ]To access individual characters access Concatenation +Connects two strings
6
C Strings vs. string Objects u Older code uses C strings u C strings more basic – faster execution u String class improves ability to manipulate text safely –Sized automatically –No null necessary –Easier modification in program Lesson 13.1
7
Some Member Functions u More actions needed than operators can provide u Calling member function involves using object name with dot operator and function name –Invoking object t One that is modified Lesson 13.2
8
find Function u Searches for a string within a string u Basic form of call to find ob1.find (ob2); –finds first occurrence of string ob2 within ob1 u Returns position Lesson 13.2 s1 = "This is an example."; s2 = "exam"; n = s1.find (s2); 0 1 2 3 4 5 6 7 8 9 0 1 The return value is 11
9
Overloaded find Function u Another version has two arguments u Basic form: ob1.find (ob2, index); –index represents integer value for beginning of search u C++ performs automatic type conversion from C strings to string objects when C strings are in string function call u String not found returns -1 Lesson 13.2
10
replace Function u Replaces characters within a string object with another string u Returns reference to invoking object u Basic form: ob1.replace (index, num, ob2); –index represents position to begin –num is the number of characters to replace –ob2 what is to be used to replace (can be C string) u Overloaded: ob1.replace (index1, num1, ob2, index2, num2); Lesson 13.2 portion of ob2 to use
11
erase Function u Eliminates characters within string object u Basic form: ob1.erase (index, num); –num represents number of characters to erase –index is the beginning position u Returns reference to the invoking object Lesson 13.2 s1 = "This is an example."; s1.erase (8,3); example.";
12
insert Function u Adds characters to a string object u Basic form: ob1.insert (index, ob2); –index is beginning position –ob2 represents what is to be inserted t Can be a C string u Returns reference to the invoking function Lesson 13.2 s1 = "This is an example."; s1.insert (8,"just "); just an example.";
13
Other string Functions u Many other functions available –Examples: compare, append, resize, etc. u Described in text, table 13.2 –List of functions –Sample syntax –What they return –Description of purpose Lesson 13.2
14
Reading a Single Word u From keyboard using cin cin >> s1; –Reads characters until whitespace typed –Whitespace includes space and "Enter" key u Amount of memory for s1 automatically made sufficient Lesson 13.3
15
Reading Multiple Lines u Use function getline –Contained in u General form: getline (cin, ob1, 'terminator'); –Ob1 is name of string object –Terminator is terminating character t Read but not included in ob1 object t Default value is '\n' u Read single line of input: getline (cin, ob1); Lesson 13.3
16
Reading an Input File u Use getline for complete file –Read each line into 1-D array of string objects u Use for loop u General form: for ( int j = 0; j < num_lines; j++) { getline (infile, array_element); } Lesson 13.3
17
Strings and Functions u Return type for function is string object u string objects passed like other objects –Copy is passed when type string is argument u Keyword const prevents array elements from being modified u & indicates a reference Lesson 13.4 string function1 (string, const string[ ], string&, string [ ]);
18
Class Definition: Example Lesson 13.5 class Class1 { private: char aa [20]; string s1; public: char* get_aa( ); string get_s1 ( ); void read_data ( ); }; Both C strings and string objects can be members of a class "get" functions return private data member Function reads both data members
19
Member Functions Lesson 13.5 class Class1 { private: char aa [20]; string s1; public: char* get_aa( ); string get_s1 ( ); void read_data ( ); }; void Class1 :: read_data ( ) { cout << "Enter your name." << endl; getline (cin,s1); cout<< "Enter phone number." <<endl; cin.getline (aa) } char* Class1 :: get_aa ( ) { return aa; } string Class1 :: get_s1 ( ) { return s1; }
20
Working With a Class u Object of class declared – allows member functions to be called u Example: Class1 ob1; ob1.read_data ( ); cout <<ob1.get_s1<<endl<<ob1.get_aa ( ) <<endl; Lesson 13.5
21
Summary u Create strings with the C++ string class u Manipulate strings with string functions u Read a word, or multiple lines from keyboard u Use the string class in programs Learned how to:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.