1 / 7

Learning Objectives

Learning Objectives. String Class. Topic 1: String Class. Two string types: C-strings Array with base type char End of string marked with null, "" "Older" method inherited from C String class New method. Standard Class string. Defined in library: #include <string> using namespace std;

kerenw
Download Presentation

Learning Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning Objectives • String Class

  2. Topic 1: String Class • Two string types: • C-strings • Array with base type char • End of string marked with null, "\0" • "Older" method inherited from C • String class • New method

  3. Standard Class string • Defined in library:#include <string>using namespace std; • String variables and expressions • Treated much like simple data types • Can assign, compare, add:string s1, s2, s3; s1 = "Hello”; //Assignment s2 = “Mom! ” //Assignment s3 = s1 + “ ” + s2; //Concatenation

  4. I/O with Class string • Just like other types! • string s1, s2;cin >> s1;cin >> s2; • Results:User types in:May the hair on your toes grow long and curly! • Extraction still ignores whitespace:s1 receives value "May"s2 receives value "the"

  5. getline() with Class string • For complete lines:string line;cout << "Enter a line of input: ";getline(cin, line);cout << line << "\n"; • Dialogue produced:Enter a line of input: Do be do to you!Do be do to you!

  6. Class string Processing • Same operations available as c-strings: Over 100 members of standard string class • Some member functions: • .length() • Returns length of string variable • .at(i) • Returns reference to char at position I • .substr(P, L) • Returns a substring at position P and having length L

  7. C-string and string Object Conversions • Automatic type conversions • From c-string to string object:char s1[] = "My C-string";string s2;s2 = s1; • Perfectly legal and appropriate! • s1 = s2; • ILLEGAL! • Cannot auto-convert to c-string • Must use explicit conversion:strcpy(s1, s2.c_str());

More Related