Title: Intro to Classes via the C String Class
1Intro to Classes via the C String Class
- November 18, 2002
- CSE103 - Penn State University
- Online at http//www.personal.psu.edu/djh300/cse10
3/string.ppt - Prepared by Doug Hogan
2Announcements
- Late drop deadline is Wednesday, 11/20
- Review scores
- If you do not earn a C or better, youll 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
3Overview
- String class
- Headers
- Creating strings
- Manipulating and comparing strings
- Motivation for Object Oriented Programming
- Strings as objects
- Terminology and theory
- Problems
4The string type
- Alternative to character arrays
- Hides many details
- Easier to manipulate
- Required headers
- include ltstringgt
- using namespace std
- string is part of the C standard library
5Declaring 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)
6Input/Output
- cin and cout
- cin stops at whitespace
- getline can be used for reading in strings with
spaces included - getline(stream, receivingString)
- example
- cout ltlt Enter a stringgetline(cin, str1)
7Manipulating 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 ltlt myString0
- prints a
- myString0 A
- changes myString to A string
8Exercises
- Create a string called testString that is
initially Its 8 a.m. and hes expecting me to
think - string testString(Its 8 a.m. and hes expecting
me to think) - Change the 8 to a 9.
- testString5 9
- Output the string
- cout ltlt testString
9Operators and strings
- The string class lets you use the following
operators - Assignment
- Comparison gt, gt, lt, lt
- Equality , !
- Concatenation
- Example
- if(string1 lt string2) cout ltlt string1 ltlt
is before ltlt string2 ltlt endl
10Problem (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
11A bit of terminology before the fun part
- Well 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
12length() message
- length( ) returns the length of the string its
called on - ex
- string hello(Hello)
- cout ltlt hello.length() ltlt endl
- prints 5
- Dont forget the parentheses!!
- Must give the string object, then the dot
operator!!
13Practice
- Given
- string noun
- cin gtgt noun
- Output the length of noun.
- cout ltlt noun.length()
14find() message
- find( ) takes a string as an argument
- returns the index where the argument is found in
the object its called on - ex
- string hello(Hello)
- cout ltlt hello.find(ll) ltlt endl
- prints 2
- if the string isnt found, find( ) returns -1
15Substrings 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 ltlt hello.substr(6, 5) ltlt endl
- prints World
- goes up to strings length if 2nd argument is too
short
16Problems
- 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)
17Given 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.
18Given 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
- s.substr(2)
- not enough arguments
- s.substr(2, 5)
- y str
- s.substr(tri)
- wrong arguments
- s.find(tri)
- 5
Modified Self-Check 4-8 from Mercer, Rick.
Computing Fundamentals with C. Wilsonville, OR
Franklin, 1999.
19More string Messages
- The ones weve discussed are the most useful
- See page 593 of your book for more
20Motivation 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
21Access 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
22Information 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, MAHeath, 1994.
23Information Hiding Applied
- Client can access the public methods of an object
- Sending a message
- Methods can access private data
24An example of a class bankAccount
- Data
- name
- balance
- Operations
- create an account
- withdraw
- deposit
- check balance
25Summary
- string class
- created with string keyword
- operators , gt, lt, defined
- messages length(), find(str), substr(start,
length) defined - Object Oriented Programming
- classes and objects
- information hiding
26Homework 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.
27Homework for next time
- Implement the following nonmember function
- string returnedExpression(string inputString)
- // PRE inputString is a line of valid C code
// (lt80 chars) containing the
return// keyword and ending with a
semicolon - // POST FCTVAL the expression that follows
// the return keyword, not
including the// semicolon - Hint youll need to (and should) use all of the
string member functions we discussed today. - Ex returnedExpression( return
afoo(a-1)) - returns afoo(a-1)
- This PowerPoint is online at http//www.personal.p
su.edu/djh300/cse103/string.ppt