EECE 3220 Data Structures Instructor Dr Michael Geiger










![Standard Library Class string (Cont. ) n Overloaded [] q q n Access one Standard Library Class string (Cont. ) n Overloaded [] q q n Access one](https://slidetodoc.com/presentation_image_h2/18271f85c2f6b42670af6b8db30157ed/image-11.jpg)








- Slides: 19
EECE. 3220 Data Structures Instructor: Dr. Michael Geiger Fall 2019 Lecture 3: Functions in C++
Lecture outline n Announcements/reminders q Textbook activities for Chapters 1 -4 due Monday, 9/16 n n n q Program 1 to be posted; due Friday, 9/20 n n n Primarily “review” that allows you to work on C++ syntax Remember to follow Blackboard link to complete activities No late submissions for text activities Collection of four short programs Covers basic C++ syntax; other material from early lectures This lecture (will cover next 2 -3 classes) q Functions in C++ n n q Declarations Argument passing: by value, by address, and by reference Strings in C++ 12/18/2021 Data Structures: Lec. 3 & 4 2
Functions n Used to break programs into smaller pieces q n Functions have: q q q n n Useful when code sequences repeated An optional return value A name Optional arguments Must be prototyped or written completely prior to use C++ supports three forms of argument passing q q q Pass by value (also supported in C) Pass by address (also supported in C) Pass by reference 12/18/2021 Data Structures: Lec. 3 & 4 3
Function examples n All examples below are function prototypes q Contain information about how to call function n n q Return type, name, and argument list Only arg types required, but good practice to list names No details on operation of function (definition) int f 1(); double f 2(int x, int y); void f 3(int *p 1, int *p 2); void f 4(int &r 1, int &r 2); n What’s the * in f 3()? q n Argument is passed by address What’s the & in f 4()? q Argument is passed by reference 12/18/2021 Data Structures: Lec. 3 & 4 4
Review: pointers n Pointer: address of a variable q q Can get address of existing object using & Can get value of existing pointer using * Can assign pointers of same type to each other Pointer declaration: <base type>* <pointer name> n n Base type determines how reference is interpreted Use pointers as function arguments pass by address 12/18/2021 Data Structures: Lec. 3 & 4 5
Pass by address n Pointer(s) as function argument(s) q n Ex. : p 1 and p 2 in: void f 3(int *p 1, int *p 2); Function can modify contents of data at address Example: say f 3() is swap routine: void f 3(int *p 1, int *p 2) { int temp = *p 1; *p 1 = *p 2; *p 2 = temp; } q n Function call requires passing pointers: int v 1 = 32; int v 2 = 20; f 3(&v 1, &v 2); 12/18/2021 // v 1 = 20, v 2 = 32 // after f 3 is done Data Structures: Lec. 3 & 4 6
Pass by reference (C++ only) n n Similar to pass by address—gives function ability to modify contents of arguments References are aliases of arguments q q n Can refer to argument by name—no need for extra operators Not as obvious function can modify arguments Example: void f 4(int &r 1, int &r 2) { int temp = r 1; r 1 = r 2; r 2 = temp; } n Function call requires passing pointers: int v 1 = 32; int v 2 = 20; f 4(v 1, v 2); 12/18/2021 // v 1 = 20, v 2 = 32 // after f 4 is done Data Structures: Lec. 3 & 4 7
Function example #include <iostream> using namespace std; double f 1(int v 1, int v 2) { return (v 1 + v 2) / 2. 0; } double f 1(int v 1, int v 2); void f 2(int *ptr 1, int *ptr 2); void f 3(int &ref 1, int &ref 2); void f 2(int *ptr 1, int *ptr 2) { while (*ptr 1 > 5) { *ptr 2 -= 3; (*ptr 1)--; } } int main() { int foo = 10; int bar = 57; double baz; baz = f 1(foo, bar); cout << "After f 1(), foo = " << foo << ", bar = " << bar << ", baz = " << baz << "n"; f 2(&foo, &bar); cout << "After f 2(), foo = " << foo << ", bar = " << bar << "n"; f 3(foo, bar); cout << "After f 3(), foo = " << foo << ", bar = " << bar << "n"; void f 3(int &ref 1, int &ref 2) { if (ref 1 == 5 && ref 2 >= 45) { ref 1++; ref 2 --; } else if (ref 1 == 5) { ref 1 --; ref 2++; } else { ref 1 = ref 2 - 10; ref 2 = ref 1 + 10; } } return 0; } 12/18/2021 Data Structures: Lec. 3 & 4 8
Example output After f 1(), foo = 10, bar = 57, baz = 33. 5 After f 2(), foo = 5, bar = 42 After f 3(), foo = 4, bar = 43 12/18/2021 Data Structures: Lec. 3 & 4 9
Standard Library Class string n Class string q q Header <string>, namespace std Basic uses: n n n q Initialization: string s 1( "hi" ); Input/output (as in cout << s 1) Assignment: s 1 = "hi"; Can also use: n Relational operators: ==, !=, >, <=, < q n Concatenation: += q 10 12/18/2021 Perform char-by-char comparison using ASCII values E. g. : s 1 += “lly” s 1 = “hilly” Data Structures: Lec. 3 & 4
Standard Library Class string (Cont. ) n Overloaded [] q q n Access one character No range checking (if subscript invalid) Member function at q Accesses one character n Example q q n Has bounds checking, throws an exception if subscript is invalid Member function empty q n s 1. at( 10 ); Returns true if no characters in string Member function length q q Returns number of characters in string s = “Hello”; s. length() = 5 12/18/2021 Data Structures: Lec. 3 & 4 11
Standard Library Class string (Cont. ) n Substring member function substr q s 1. substr( 0, 14 ); n q s 1. substr( 15 ); n n Starts at location 0, gets 14 characters Substring beginning at location 15, to the end Member function find q Returns index of first occurrence of substring/char or string: : npos if substring doesn’t exist in string n q Can specify optional starting position Given string s 2 = "Data structures" n n s 2. find("at") returns 1 s 2. find('c’) returns 9 s 2. find("dog") returns string: : npos s 2. find('t', 3) returns 6 q 12/18/2021 Position of first 't', starting search at position 3 Data Structures: Lec. 3 & 4 12
Example: Strings & functions int main() { string s 1( "happy" ); string s 2( " birthday" ); string s 3; // test overloaded equality and relational operators cout << "s 1 is "" << s 1 << ""; s 2 is "" << s 2 << ""; s 3 is "" << s 3 << '"' << "nn. The results of comparing s 2 and s 1: " << "ns 2 == s 1 yields " << ( s 2 == s 1 ? "true" : "false" ) << "ns 2 != s 1 yields " << ( s 2 != s 1 ? "true" : "false" ) << "ns 2 > s 1 yields " << ( s 2 > s 1 ? "true" : "false" ) << "ns 2 < s 1 yields " << ( s 2 < s 1 ? "true" : "false" ) << "ns 2 >= s 1 yields " << ( s 2 >= s 1 ? "true" : "false" ) << "ns 2 <= s 1 yields " << ( s 2 <= s 1 ? "true" : "false" ); 12/18/2021 Data Structures: Lec. 3 & 4 13
Example (cont. ) Output from previous slide: s 1 is “happy”; s 2 is “ birthday”; s 3 is “” The results of comparing s 1 and s 2: s 2 == s 1 yields false s 2 != s 1 yields true s 2 > s 1 yields false s 2 < s 1 yields true s 2 >= s 1 yields false s 2 <= s 1 yields true 12/18/2021 Data Structures: Lec. 3 & 4 14
Example (cont. ) // test string member function empty cout << "nn. Testing s 3. empty(): " << endl; if ( s 3. empty() ) { cout << "s 3 is empty; assigning s 1 to s 3; " << endl; s 3 = s 1; // assign s 1 to s 3 cout << "s 3 is "" << s 3 << """; } // end if // test overloaded string concatenation operator cout << "nns 1 += s 2 yields s 1 = "; s 1 += s 2; // test overloaded concatenation cout << s 1; // test cout << s 1 += " cout << 12/18/2021 concatenation operator with C-style string "nns 1 += " to you" yields" << endl; to you"; "s 1 = " << s 1 << "nn"; Data Structures: Lec. 3 & 4 15
Example (cont. ) Output from previous slide: Testing s 3. empty(): s 3 is empty; assigning s 1 to s 3; s 3 is “happy” s 1 += s 2 yields s 1 = happy birthday s 1 += “ to you” yields s 1 = happy birthday to you 12/18/2021 Data Structures: Lec. 3 & 4 16
Example (cont. ) // test string member function substr cout << "The substring of s 1 starting at location 0 forn" << "14 characters, s 1. substr(0, 14), is: n" << s 1. substr( 0, 14 ) << "nn"; // test substr "to-end-of-string" option cout << "The substring of s 1 starting atn" << "location 15, s 1. substr(15), is: n" << s 1. substr( 15 ) << endl; // test using subscript operator to create lvalue s 1[ 0 ] = 'H'; s 1[ 6 ] = 'B'; cout << "ns 1 after s 1[0] = 'H' and s 1[6] = 'B' is: " << s 1 << "nn"; // test subscript out of range with string member function "at" cout << "Attempt to assign 'd' to s 1. at( 30 ) yields: " << endl; s 1. at( 30 ) = 'd'; // ERROR: subscript out of range return 0; } // end main 12/18/2021 Data Structures: Lec. 3 & 4 17
Example (cont. ) Output from previous slide: The substring of s 1 starting at location 0 for 14 characters, s 1. substr(0, 14), is: happy birthday The substring of s 1 starting at location 15, s 1. substr(15), is: to you s 1 after s 1[0] = ‘H’ and s 1[6] = ‘B’ is: Happy Birthday to you Attempt to assign ‘d’ to s 1. at(30) yields abnormal program completion 12/18/2021 Data Structures: Lec. 3 & 4 18
Final notes n Next topic(s) q n Algorithmic complexity Reminders: q Textbook activities for Chapters 1 -4 due Monday, 9/16 n n n q Primarily “review” that allows you to work on C++ syntax Remember to follow Blackboard link to complete activities No late submissions for text activities Program 1 to be posted; due Friday, 9/20 n n 12/18/2021 Collection of four short programs Covers basic C++ syntax; other material from early lectures Data Structures: Lec. 3 & 4 19