Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at

Similar presentations


Presentation on theme: "Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at"— Presentation transcript:

1 Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at http://www.personal.psu.edu/djh300/cse103/string.ppt Prepared by Doug Hogan

2 2 Announcements  Late drop deadline is Wednesday, 11/20  Review scores  If you do not earn a C or better, you’ll need to retake the class for the major.  4-credit D – GPA impact?  Dr. Quick has normal office hours this week.  HW5.dat was modified on Wednesday, 11/13 (leading 0 in an ID)  HW5 due next Monday

3 3 Overview  String class  Headers  Creating strings  Manipulating and comparing strings  Motivation for Object Oriented Programming  Strings as objects  Terminology and theory  Problems

4 4 The string type  Alternative to character arrays  Hides many details  Easier to manipulate  Required headers  #include  #include  using namespace std;  string is part of the C++ standard library

5 5 Declaring strings  Uninitialized:  Like primitive data types  e.g. string myString;  Can then use assignment operator  e.g. myString = “this is a string”;  Initialized:  Use string keyword, name, and initial value in parentheses  e.g. string myString(“a string”);

6 6 Input/Output  cin and cout  cin stops at whitespace  getline can be used for reading in strings with spaces included:  getline(stream, receivingString);  example:  cout << “Enter a string”; getline(cin, str1);

7 7 Manipulating Characters  Exactly the same as with arrays of characters!  Use an index in brackets to get or manipulate that character.  string myString(“a string”);  cout << myString[0];  prints “a”  myString[0] = “A”;  changes myString to “A string”

8 8 Exercises  Create a string called testString that is initially “It’s 8 a.m. and he’s expecting me to think”  string testString(“It’s 8 a.m. and he’s expecting me to think”);  Change the “8” to a “9.”  testString[5] = ‘9’;  Output the string  cout << testString;

9 9 Operators and strings  The string class lets you use the following operators:  Assignment: =  Comparison: >=, >, =, >, <, <=  Equality: ==, !=  Concatenation: +  Example:  if(string1 < string2) { cout << string1 << “ is before “ << string2 << endl; }

10 10 Problem (should be familiar…)  Suppose you have these declarations:  string str1 = “I love ”;  string str2 = “computer programming!”;  Create a string called str3 from these two strings that reads “I love computer programming!”  string str3 = str1 + str2;

11 11 A bit of terminology before the fun part…  We’ll call string variables objects.  We can operate on strings with functions  use dot notation  e.g. objectName.operation();  said to be sending a message to the string object

12 12 length() message  length( ) returns the length of the string it’s called on  ex:  string hello(“Hello”);  cout << hello.length() << endl;  prints 5  Don’t forget the parentheses!!  Must give the string object, then the dot operator!!

13 13 Practice  Given  string noun;  cin >> noun;  Output the length of noun.  cout << noun.length();

14 14 find() message  find( ) takes a string as an argument  returns the index where the argument is found in the object it’s called on  ex:  string hello(“Hello”);  cout << hello.find(“ll”); << endl;  prints 2  if the string isn’t found, find( ) returns -1

15 15 Substrings: substr() message  Takes two integer arguments:  first is starting character  second is length  returns a substring of the given length  string hello(“Hello World”);  cout << hello.substr(6, 5); << endl;  prints “World”  goes up to string’s length if 2 nd argument is too short

16 16 Problems  Given string s1(“abcdefghi”);  string s2(s1.substr(4, 3)); What is stored in s2?  Answer: efg  Write a line of code to store the location of the letter “d” from s1 in the following int:  int d;  Answer: d = s1.find(“d”);

17 17 Given string s(“Any string”); Give the result of each message or what is wrong with it.  length(s)  s.length  s(length)  s.length()  find(“Any”)  s.find(“ “)  s.substr(2)  s.substr(2, 5)  s.substr(“tri”)  s.find(“tri”) Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.

18 18 Given string s(“Any string”); Give the result of each message or what is wrong with it.  length(s)  no dot notation  length takes no argument  s.length  no parentheses  s(length)  parentheses misplaced  s.length()  10  find(“Any”)  no object  s.find(“ “) 33  s.substr(2)  not enough arguments  s.substr(2, 5)  y str  s.substr(“tri”)  wrong arguments  s.find(“tri”) 55 Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.

19 19 More string Messages  The ones we’ve discussed are the most useful  See page 593 of your book for more

20 20 Motivation for classes  Object-Oriented Programming (OOP)  Package together a set of related data and operations ( encapsulation )  Define a class (abstract data type), or a new data type with its operations  One instance of a class is called an object  The data and operations of a class are called its members.  string is an example of a class

21 21 Access rights in OOP  Classes are similar to structs  Add the notion of access rights  class member data and operations can be  public – accessible to anyone  private – accessible only to the object  usually  data are private  operations are public

22 22 Information Hiding  Client (user) has only the information needed to use the software.  Implementer has only the information needed to implement the software.  Communication through pre/post. Graphic from: Headington, Mark A. and David Riley. Data Abstraction and Structures using C++. Lexington, MA:Heath, 1994.

23 23 Information Hiding Applied  Client can access the public methods of an object  Sending a message  Methods can access private data

24 24 An example of a class: bankAccount  Data:  name  balance  Operations:  create an account  withdraw  deposit  check balance

25 25 Summary  string class  created with string keyword  operators =, >,, <, == defined  messages length(), find( str ), substr( start, length ) defined  Object Oriented Programming  classes and objects  information hiding

26 26 Homework for next time  Another motivation for classes is the notion of abstraction.  Find out what the word means.  Come up with an example of how we use abstraction in everyday life.  Write down an example of a bankAccount object.

27 27 Homework for next time  Implement the following nonmember function:  string returnedExpression(string inputString)  // PRE: inputString is a line of valid C++ code // (<=80 chars) containing the “return” // keyword and ending with a semicolon  // POST: FCTVAL == the expression that follows // the return keyword, not including the // semicolon  Hint: you’ll need to (and should) use all of the string member functions we discussed today.  Ex: returnedExpression(“ return a+foo(a-1);”);  returns “a+foo(a-1)”  returns “a+foo(a-1)”  This PowerPoint is online at http://www.personal.psu.edu/djh300/cse103/string.ppt


Download ppt "Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at"

Similar presentations


Ads by Google