270 likes | 414 Views
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. Announcements. Late drop deadline is Wednesday, 11/20 Review scores
E N D
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
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
Overview • String class • Headers • Creating strings • Manipulating and comparing strings • Motivation for Object Oriented Programming • Strings as objects • Terminology and theory • Problems
The string type • Alternative to character arrays • Hides many details • Easier to manipulate • Required headers • #include <string> • using namespace std; • string is part of the C++ standard library
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”);
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);
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”
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;
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;}
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;
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
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!!
Practice • Given • string noun; • cin >> noun; • Output the length of noun. • cout << noun.length();
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
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 2nd argument is too short
Problems • Givenstring 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”);
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”) Givenstring s(“Any string”);Give the result of each message or what is wrong with it. Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.
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 Givenstring s(“Any string”);Give the result of each message or what is wrong with it. Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.
More string Messages • The ones we’ve discussed are the most useful • See page 593 of your book for more
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
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
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.
Information Hiding Applied • Client can access the public methods of an object • Sending a message • Methods can access private data
An example of a class: bankAccount • Data: • name • balance • Operations: • create an account • withdraw • deposit • check balance
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
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.
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)” • This PowerPoint is online at http://www.personal.psu.edu/djh300/cse103/string.ppt