Presentation is loading. Please wait.

Presentation is loading. Please wait.

From C-strings to C++ strings: Abstraction at Work

Similar presentations


Presentation on theme: "From C-strings to C++ strings: Abstraction at Work"— Presentation transcript:

1 From C-strings to C++ strings: Abstraction at Work
Chapter 5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

2 Agenda Review: C++ I/O and <string> classes
Where it began: C-strings and their problems Burying the problem: a simple MString class (M is for MPC!) Finding the Algorithm for string operations Syntax forms for MString operators Using MString in a typical application Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

3 Chapter 5 Contents— Read it, it’s good!
5.1 The C++ Standard I/O Classes 5.2 The C++ String Types 5.3 Case Study: Text Editing 5.4 Introduction to Pattern Matching (optional) 5.5 Introduction to Data Encryption (optional) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

4 REVIEW: The C++ Standard I/O Classes
Input viewed as a stream of characters Flowing from some source into an executing program Output viewed as a stream of characters Flowing from program to output device Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

5 The C++ Standard I/O Classes
C++ has <iostream> library istream for input ostream for output These are classes that model data streams Therefore, objects of these classes can be passed to functions Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

6 cin is a standard istream object
The istream Class Models flow of characters from input device to program Characters enter an istream object Object transmits characters from device to program Input operator >> Extraction operator istreamObject >> variable; Stream states accessed with functions .good(), .bad(), .fail(), .eof() Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

7 The ostream Class Models flow of characters from executing program to output device Characters enter ostream object Transmitted to specified output device Output operator << Insertion operator ostreamObject << expression; Note Table 5-3 in text for Output Stream Operations and Methods Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

8 The ostream Class Standard ostream objects cout for normal output
cerr and clog for error and diagnostic messages Buffered streams (cout and clog) Held in the stream until it is flushed Contrast cerr which is sent to the output device immediately Can cause confusion when debugging Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

9 File I/O: ifstream, ofstream Classes
FileStream object must be constructed for purpose of receiving/sending I/O to files Called opening a stream to the file Stream activities Declaring ifstream inStream; Opening ifStream.open(file_name); Closing ifStream.close(); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

10 Output File Stream Example
#include<fstream> #include<iostream> int main() Create a new file stream newfile { ofstream newfile; Connect newfile to the file on disk newfile.open("students.txt"); newfile<<“this is written to the file”; cout<<“this is written to the monitor”; newfile.close( ); return 0; Disconnect newfile stream }

11 Input from file of unknown size
ifstream infile(“accounts.txt”); infile>>acntnum>>balance; while(infile) { // process last data // get next data } infile.close(); Check stream status after each read-if “true” process

12 A more compact version Process indefinite list in a file:
while(infile>>acntnum>>balance) { // process data } Read data and Check status after each read

13 The I/O Class Hierarchy
Characteristics of OOP hold Encapsulation Inheritance Polymorphism Note demo of file I/O, Fig 5-1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

14 Passing Streams to Functions
It may seem odd to pass cin,cout or a filestream to a function Needed to generalize moving of data Fairly unique to C++ Always pass by reference (use &) (so we don’t make a copy) Example: In some Student class… void Student::input(istream& in); void Student::output(ostream& out); in, out are placeholders for any input or output stream arguments Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

15 Flexibility gained… Student s1(333, "Rebold", 3.4), s2; ofstream fout("data.txt"); ifstream fin("enrollments.txt"); s1.output(cout);  send to screen s2.input(cin);  read from keyboard s1.output(fout);  send to file “data.txt” s2.input(fin);  input from “enrollments.txt” But the syntax looks clumsy, so we overload the >> and << operators Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

16 REVIEW: The C++ <string> Class
It’s a class…like you developed last week Variety of constructors provided for defining strings Define an empty string String s; Define a string initialized with another string String s("some other string"); Note further options in text, Table 5-7 String Constructors Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

17 C++ String Member Functions
Editing operations provided such as appending inserting erasing replacing swapping Note table 5-10 in text, String Editing Operations Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

18 Other C++ String Operations
Copiers – make copies of part or all of a string Operators = and += Accessing Individual Characters Use overloaded subscript operator [ ] String element accessors Functions such as find(), find_first_of() See Table 5-12 in text Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

19 Example <string> Operations
Code string a="the"; a = a + " truth"; a.insert(" whole",3); a.erase(4,1); a.replace("alf",5); Also a.length(); returns a.find("half") returns a.find("t",2) returns Contents of a the the truth the whole truth the hole truth the half truth 13 4 9 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

20 C++ <string> Comparisions
Comparisons Overloaded operators for <, >, ==, etc. string a="the", b="whole"; (a<b) is true (a==b) is false (a=="the") is true note that “the” is a string literal Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

21 Why are these useful? Essential for editing/processing documents
Find/replace operations Count the number of occurrences of a word Moving files on a website, changing URLs Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

22 Example: Managing a Website
When files are relocated on a server, all webpages linking them have to be changed or links are broken becomes: for mpc01c01 to mpc01c20…needs automation! Insert Insert Append Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

23 C-strings It wasn’t always this easy!
Before C++ and object oriented programming, strings in C were represented by cumbersome character arrays: char name[10]; char name[10] = "Sax Winderhaven" OOPS! Too big Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

24 C-string Problems Memory management Lack of Features
You always had to count letters to make sure the array was big enough Lack of Features Assignment, concatenation (append): yes Insert, Erase: tough luck! Comparision: yes, but confusing Frequent reading of documentation to clarify Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

25 Solving the C-string Problem
C++ with object oriented features allowed for the creation of a new class that works more logically and naturally. This class, <string>, is built on top of (using) the clumsy C-string operations This is another example of Abstraction Building an ideal/logical type that hides the messy details of an inferior, less effective set of tools. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

26 Today’s Lab Practice working with C-strings
Develop a very simple MString class (i.e. MPC String) using C-string operations Develop general insert/erase operations (fairly complex array manipulations) Code comparison and other operators Use MString to solve text processing applications Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

27 More on C-strings C-strings use a \0 or “Null character” to indicate end of string (literally a binary 0) char name[10]="Felix“ When sizing the array, you must leave one extra cell for the null character Max number of letters in name[] is 9 F e l i x \0 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

28 C-string Operations-- #include<cstring>
strcpy(a,b); copies string b into string a this is like a=b; strcat(a,b); appends string b to end of a this is like a=a+b; OR a+=b; strlen(s); returns number of letters in s does not count null character strcmp(a,b); returns code for <, > or == Negative number: a < b is true Positive number: a > b is true Zero a == b is true Note: any capital letter comes before any lowercase letter i.e.: Z < a = true! Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

29 C-string I/O Luckily, the << and >> operators are defined for C-strings: char str[10]; cout<<"Enter a c-string"<<endl; cin>>str; cout<<"you entered: "<<str<<endl; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

30 Hands On Time Practice using C-strings Download and expand week5.zip
Open Lab5Strings folder run Project.sln (currently with CStringDemo.cpp) Read and interpret output Add code where indicated Experiment with making the array sizes too small Watch for errors in output Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

31 The basic MString Class (M=MPC)
MString: a new class built using C-string Constructors: default and explicit Input and output read() and print() member functions insertion << and extraction >> operators Wrappers around read and print Private data: a char array (C-string) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

32 The class declaration This is also a C-string
class MString { public: constructors: MString(); default MString(char *newStr); explicit void print(ostream& out) const; void read(istream& in); private: char myText[100]; a c-string }; This is also a C-string Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

33 Examples using MString members
MString text; default cnstr MString a("hello"); explicit cnstr a.print(cout); prints a to cout cout<<"Enter a string"<<endl; text.read(cin); reads text from cin cout<<"text = "<<text<<endl; operator<< cout<<"enter a new value"<<endl; cin>>text; operator>> Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

34 MString Class Definition
const means print( ) will not change myText MString Class Definition MString::MString() { myText[0]=0; } MString::MString(char *newStr) strcpy(myText, newStr); void MString::print(ostream& out) const { out<<myText; } void MString::read(istream& in) in>>myText; Empty string Copy newStr to myText Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

35 The stream operators declared
These are “free functions” not part of MString Output: ostream& operator<<(ostream& out, MString& s); Input: istream& operator>>(istream& in, MString& s); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

36 Logic behind the operators
C++ treats operators as functions: The expression a+b is really operator+(a,b) This transformation is handled by the compiler Expression cout<<a is operator<<(cout,a) cout is passed to ostream& out a is passed to MString& s This explains the red part-- ostream& operator<<(ostream& out, MString& s); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

37 What about the return type?
When operations are chained: a + b + c Only one operator is processed at a time: a + b + c  operator+(a, b) operator+( , c) Or, think of it as operator+(operator+(a, b), c) operator+ must return same data type as a,b Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

38 Above logic also applies to <<,>>
When output operations are chained: cout<< a << b ; Only one operator is processed at a time: operator<<(cout, a) operator<<( , b) i.e operator<<(operator<<(cout, a), b) Operator<< must return cout (thus… ostream& out , alias cout, is returned) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

39 Operators <<, >> defined
Operators <<,>> are not MString members May not access myText Therefore we use the already defined read,print ostream& operator<<(ostream& out, MString& s) { s.print(out);  use the print function for s return out;  return the output stream } istream& operator>>(istream& in, MString& s) { s.read(in); return in; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

40 Hands On Time #2 Practice using MString Switch source files in Project
In Solution Explorer, R-Click CStringDemo choose remove, remove (not delete) R-Click SourceFiles > Add > Existing Item choose StringDemo.cpp, hit + on SourceFile Then open StringDemo.cpp and run Add code where indicated to complete <<,>> Is MString improved over the C-string inside it? Is MString vulnerable to overflow like C-string? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

41 The Improved MString class
We want to add some additional features: length append erase comparison operators (<,>,==) insert concatenation op ( + ) We will work on StringDemo2.cpp See Lab5Strings.doc for instructions Half of the functions are solved in StringDemoHALF.cpp (use as hints if needed) More hints follow  Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

42 Append Most functions in this lab come in pairs
One for MString, one for C-string MString b("yes"), c("hello"); c.append(“, sir"); // the version for C-Strings b.append(c); // the version for M-Strings Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

43 Prototypes for Append Similar pattern (C-string, MString) for most
These functions just use strcat to do the job Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

44 StringDemo2.cpp layout Class declaration, definition and main in same file No need to worry about multi-file compilation Function definitions provided as shells Some default definitions so compiler is happy Just complete the definition of the functions as you go thru the lab Do not modify main until you get to Part 7&8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

45 Length and EraseOneLetter
Example use of functions MString s("ABCCDEFGHIJKL"); cout<<"String s = "<<s<<" and it's length = " <<s.length()<<endl; s.eraseOneLetter(2); cout<<"After calling s.eraseOneLetter(2);"<<endl <<" s = "<<s<<" and is now length "<<s.length()<<endl; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

46 Prototypes Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

47 Algorithm Development
length() is easy (return strlen(myText);) What about eraseOneLetter(2) ?? Before, s= ABCCDEFGHIJKL After, s= ABCDEFGHIJKL Some kind of shifting required for loop If we try to guess it, we’ll most likely fail Is there a method to developing a correct alg.? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

48 Algorithm Method Start with a specific function call
With specific data & arguments, i.e. MString s("ABCCDEFGHIJKL"); s.eraseOneLetter(2); 2. Draw a picture of data structure Before and after function is called Required for any assistance from instructor!! Write out statements without any loop Generalize into loop that works for all args Add assert to prevent bad calls Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

49 Before & After: eraseOneLetter()
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

50 Non-looping Specific Solution
For our particular case, index (letter to erase) is 2 Statements to solve our example case: myText[2] = myText[3]; myText[3] = myText[4]; myText[4] = myText[5]; myText[12] = myText[13];  why is this the last copy? Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

51 Formulate the General Soln
We can now see that 2 in the first statement is really index. 13 in the last statement is length of the string Therefore we can derive: void MString::eraseOneLetter(int index) { for (int k=index; k < strlen(myText); k++ myText[k] = myText[k+1]; } And check desired statements are produced Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

52 Assert – easy debug check
For eraseOneLetter( ) we should verify: index >= 0 index < strlen(myText) This becomes first line of function: assert(index>=0 && index < strlen(myText)); Bad call: s.eraseOneLetter(-1) Aborts program with “assertion failed” message Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

53 erase() Example use: s.erase(2,3);
cout<<"After erasing 3 letters starting at cell 2,"<<endl <<" s = "<<s<<" and is now length "<<s.length()<<endl; Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

54 2 ways to erase() You can formulate the general solution using just a for loop as above Fastest execution speed You can use the eraseOneLetter function repeatedly in a for-loop Simpler code Slower to run 3. Either way you need a picture! Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

55 Continue thru Lab5Strings.doc
Solve for each member function Remember, hints are in StringDemo2HALF For 7 and 8, read files essay.txt and webpages.htm in same folder FYI: MString with Dynamic memory shown in StringDemo2Dynamic.cpp Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved


Download ppt "From C-strings to C++ strings: Abstraction at Work"

Similar presentations


Ads by Google