Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)"— Presentation transcript:

1 ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)

2 Lecture outline Announcements / reminders  Lab 6 due next Monday (10/29) Today  Quiz (10 mins)  Project specification  Review: Strings  Microsoft Visio  Midterm Survey 4/17/2015 ECE 264: Lecture 20 2

3 Project specification Text-based adventure game Should contain  A group of rooms, each of which may contain One or more items One or more characters  An “adventurer” who can Pick up and store items Discard items Interact with environment (largely up to you to specify how) Should include functionality beyond basics Must have way to configure environment  Typical: read input file specifying configuration 4/17/2015 ECE 264: Lecture 13 3

4 Project deliverables/grading Design (20%, due 11/09)  High-level design, including requirements, class diagrams, program flow, and division of work Demonstration (10%, either 12/03 or 12/07)  10 minute demo during lab/lecture  Do not have to have code complete but should be able to demonstrate most functionality Code (25%, due 12/10) User’s guide (20%, due 5:00 PM on 12/10)  Explains how to play game Technical report (25%, due 5:00 PM on 12/10)  Describes all functionality in code (required & extra)  Rough estimate of division of work  Description of each program module  Description of testing plan  Should include final class diagrams/program flow 4/17/2015 ECE 264: Lecture 13 4

5 Review: Strings New concepts  Relational operators: ==, !=,, = Character-by-character comparison using ASCII  Concatenation: +, +=  Choosing single character: [], at() at() provides boundary checking  Substrings: substr() function If s1 = “ECE 264”  s1.substr(0,3) = “ECE”  3 chars starting at position 0  s1.substr(4) = “ 264”  all chars from position 4 to end of string 4/17/2015 ECE 264: Lecture 20 5

6 Example: Strings & functions int main() { string s1( "happy" ); string s2( " birthday" ); string s3; // test overloaded equality and relational operators cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 << "\"; s3 is \"" << s3 << '\"' << "\n\nThe results of comparing s2 and s1:" << "\ns2 == s1 yields " << ( s2 == s1 ? "true" : "false" ) << "\ns2 != s1 yields " << ( s2 != s1 ? "true" : "false" ) s1 yields " s1 ? "true" : "false" ) << "\ns2 < s1 yields " << ( s2 < s1 ? "true" : "false" ) = s1 yields " = s1 ? "true" : "false" ) << "\ns2 <= s1 yields " << ( s2 <= s1 ? "true" : "false" ); 4/17/2015 ECE 264: Lecture 20 6

7 Example (cont.) 4/17/2015 ECE 264: Lecture 20 7 Output from previous slide: s1 is “happy”; s2 is “ birthday”; s3 is “” The results of comparing s1 and s2: s2 == s1 yields false s2 != s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true

8 Example (cont.) // test string member function empty cout << "\n\nTesting s3.empty():" << endl; if ( s3.empty() ) { cout << "s3 is empty; assigning s1 to s3;" << endl; s3 = s1; // assign s1 to s3 cout << "s3 is \"" << s3 << "\""; } // end if // test overloaded string concatenation operator cout << "\n\ns1 += s2 yields s1 = "; s1 += s2; // test overloaded concatenation cout << s1; // test concatenation operator with C-style string cout << "\n\ns1 += \" to you\" yields" << endl; s1 += " to you"; cout << "s1 = " << s1 << "\n\n"; 4/17/2015 ECE 264: Lecture 20 8

9 Example (cont.) 4/17/2015 ECE 264: Lecture 20 9 Output from previous slide: Testing s3.empty(): s3 is empty; assigning s1 to s3; s3 is “happy” s1 += s2 yields s1 = happy birthday s1 += “ to you” yields s1 = happy birthday to you

10 Example (cont.) // test string member function substr cout << "The substring of s1 starting at location 0 for\n" << "14 characters, s1.substr(0, 14), is:\n" << s1.substr( 0, 14 ) << "\n\n"; // test substr "to-end-of-string" option cout << "The substring of s1 starting at\n" << "location 15, s1.substr(15), is:\n" << s1.substr( 15 ) << endl; // test using subscript operator to create lvalue s1[ 0 ] = 'H'; s1[ 6 ] = 'B'; cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " << s1 << "\n\n"; // test subscript out of range with string member function "at" cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; s1.at( 30 ) = 'd'; // ERROR: subscript out of range return 0; } // end main 4/17/2015 ECE 264: Lecture 20 10

11 Example (cont.) 4/17/2015 ECE 264: Lecture 20 11 Output from previous slide: The substring of s1 starting at location 0 for 14 characters, s1.substr(0, 14), is: happy birthday The substring of s1 starting at location 15, s1.substr(15), is: to you s1 after s1[0] = ‘H’ and s1[6] = ‘B’ is: Happy Birthday to you Attempt to assign ‘d’ to s1.at(30) yields abnormal program completion

12 Microsoft Visio See the Visio Manual 4/17/2015 ECE 264: Lecture 20 12

13 Final notes Next time  Start pointer review (time permitting) Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 4/17/2015 ECE 264: Lecture 20 13


Download ppt "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)"

Similar presentations


Ads by Google