Electrical Engineering - Chapter 13: C++ String Class

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 Can use operators to perform some string manipulations

ppt21 trang | Chia sẻ: thuongdt324 | Lượt xem: 404 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 13: C++ String Class, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 13 – C++ String ClassString objectsDo not need to specify size of string objectC++ keeps track of size of textC++ expands memory region to store text as neededCan use operators to perform some string manipulationsLesson 13.1Declaring string ObjectsUse the class name string and list object namesExample: string s1, s2, s3;s1, s2, s3 would be string objectsString header needed to declare string objectsDo NOT specify size (Note: no brackets)Initializing string ObjectsCan initialize with the =C++ automatically reserves sufficient memoryCan also place in ( ) but still need " "Null character not required for string textData member of string class stores size of textLesson 13.1s1 = "This is an example.";Operators + string ObjectsLesson 13.1Type Operator ActionAssignment = Stores string += Concatenates and storesComparison == True if strings identical != True if strings not identical > True if first string greater than second = True if first string greater or equal than second > For input and string objects > s1;Reads characters until whitespace typed Whitespace includes space and "Enter" keyAmount of memory for s1 automatically made sufficientLesson 13.3Reading Multiple LinesUse function getlineContained in General form: getline (cin, ob1, 'terminator');Ob1 is name of string objectTerminator is terminating characterRead but not included in ob1 objectDefault value is '\n'Read single line of input: getline (cin, ob1);Lesson 13.3Reading an Input FileUse getline for complete fileRead each line into 1-D array of string objectsUse for loopGeneral form: for ( int j = 0; j < num_lines; j++) { getline (infile, array_element); }Lesson 13.3Strings and FunctionsReturn type for function is string objectstring objects passed like other objectsCopy is passed when type string is argumentKeyword const prevents array elements from being modified& indicates a referenceLesson 13.4string function1 (string, const string[ ], string&, string [ ]);Class Definition: ExampleLesson 13.5class 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 returnprivate data memberFunction reads bothdata membersMember FunctionsLesson 13.5class 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; }Working With a ClassObject of class declared – allows member functions to be calledExample: Class1 ob1; ob1.read_data ( ); cout <<ob1.get_s1<<endl<<ob1.get_aa ( ) <<endl;Lesson 13.5SummaryCreate strings with the C++ string classManipulate strings with string functionsRead a word, or multiple lines from keyboardUse the string class in programsLearned how to: