EDay Auburn Engineerings Annual Open House Friday Feb
E-Day: Auburn Engineering's Annual Open House Friday, Feb 26, 2016 No COMP 2710 Class on E-Day Student Volunteers Sydney Schultz, David Harris, Xiaopu Peng
How to compare two strings? Method 1 2
How to compare two strings? Method 2 Demo string_compare. cpp 3
COMP 2710 Software Construction Structures Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu
Structure Types • Define struct globally (typically) • No memory is allocated – Just a "placeholder" for what our struct will "look like" • Definition: struct CDAccount. V 1 Name of new struct "type" { double balance; member names double interest. Rate; int term; }; 6 -5
Accessing Structure Members • Dot Operator to access members account. balance account. interest. Rate account. term • Called "member variables" – The "parts" of the structure variable – Different structs can have same name member variables • No conflicts 6 -6
Demo: cdaccount. cpp A Structure Definition (1 of 3) 6 -7
6 -8
6 -9
Exercise 1 1 -10
Exercise 2 1 -11
Structure Assignments • Given structure named Crop. Yield • Declare two structure variables: Crop. Yield apples, oranges; – Both are variables of "struct type Crop. Yield" – Simple assignments are legal: apples = oranges; – Simply copies each member variable from apples into member variables from oranges 6 -12
COMP 2710 Software Construction Pointers Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu
Pointer Introduction • Pointer definition: – Memory address of a variable • Recall: memory divided – Numbered memory locations – Addresses used as name for variable • You’ve used pointers already! – Call-by-reference parameters • Address of actual argument was passed 14
Pointer Variables • Pointers are "typed" – Can store pointer in variable – Not int, double, etc. • Instead: A POINTER to int, double, etc. ! • Example: double *p; – p is declared a "pointer to double" variable – Can hold pointers to variables of type double • Not other types! 15
Exercise 3. Declare two pointers • Declare a pointer to int. The pointer’s name is int_ptr int *int_ptr; • Declare a student structure containing (1) student name and (2) student grade. struct student_t { string name; char grade; }; • Declare a pointer called student_ptr pointing at the student structure student_t *student_ptr; 16
Pointers vs. Addresses • Pointer is an address • Address is an integer • Pointer is NOT an integer! – Not crazy abstraction! • C++ forces pointers be used as addresses – Cannot be used as numbers – Even though it "is a" number 10 -17
An Example: Pointing to … • Example: int *p 1, *p 2, v 1, v 2; //how to let p 1 point to v 1? p 1 = &v 1; – Sets pointer variable p 1 to "point to" int variable v 1 • Operator, &: "address of" variable Read like: – "p 1 equals address of v 1" – Or "p 1 points to v 1" 10 -18
How to use a pointer? • Recall: int *p 1, *p 2, v 1, v 2; p 1 = &v 1; • Two ways to refer to v 1 now: – Variable v 1 itself: cout << v 1; – Via pointer p 1: cout << *p 1; • Dereference operator, * – Pointer variable "derereferenced" – Means: "Get data that p 1 points to" 10 -19
Exercise 4. What is the output of the following code? v 1 = 0; p 1 = &v 1; *p 1 = 42; cout << v 1 << endl; cout << *p 1 << endl; 42 42 • p 1 and v 1 refer to same variable 20
The new Operator • No "real" need to have a standard identifier • Operator new can dynamically allocate variables – No identifiers to refer to them – Just a pointer! • Creates new "nameless" variable p 1 = new int; – assigns p 1 to "point to" it – Can access with *p 1 – Use just like ordinary variable 21
COMP 2710 Software Construction Linked List Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu
Nodes and Pointers • Linked list – Simple example of "dynamic data structure" – Composed of nodes • Each "node" is variable of struct or class type that’s dynamically created with new – Nodes also contain pointers to other nodes – Provide "links" 17 -23
Node Definition struct list_node_t { string item; int count; list_node_t *link; }; typedef list_node_t* list_node_ptr_t; • Order here is important! – List_node_t defined 1 st, since used in typedef • Also notice "circularity" 24
Head Pointer • Box labeled "head" not a node: list_node_ptr_t head; – A simple pointer to a node – Set to point to 1 st node in list • Head used to "maintain" start of list • Also used as argument to functions 17 -25
Example Node Access struct list_node_t { string item; int count; list_node_t *link; }; typedef list_node_t* list_node_ptr_t; (*head). count = 12; list_node_ptr_t head; head->count = 12; • Alternate operator, -> – Called "arrow operator" – Shorthand notation that combines * and. head = new list_node_t; head = new list_node_ptr_t; /* which one of the above is correct? */ • cin >> head->item – Assigns entered string to item member 26
Exercise 5. 1. Define a structure named student_t, where there are three items – (1) name, its data type is string; (2) grade, its data type is char; and (2) a pointer called next_student pointing to the next node of student_t 2. Using typedef to define a new type, student_ptr_t, which is a pointer pointing to a structure of student_t 3. Create a pointer called junior_ptr using student_ptr_t 4. Allocate memory resource pointed by junior_ptr 5. Assign your name to the name field, A to the grade field, and NULL to pointer next_student of the structure pointed by 27 junior_ptr
Exercise 5. 1. Define a structure named student_t, where there are three items – (1) name, its data type is string; (2) grade, its data type is char; and (2) a pointer called next_student pointing to the next node of student_t struct student_t { string name; char grade; student_t *next_student; }; 28
Exercise 5. 2. Using typedef to define a new type, student_ptr_t, which is a pointer pointing to a structure of student_t typedef student_t* student_ptr_t; 3. Create a pointer called junior_ptr using student_ptr_t junior_ptr; 4. Allocate memory resource pointed by junior_ptr = new student_t; 29
Exercise 5. 5. Assign your name to the name field, A to the grade field, and NULL to pointer next_student of the structure pointed by junior_ptr->name = “Xiao Qin”; junior_ptr->grade = ‘A’; junior_ptr->next_student = NULL; 30
End Markers • Use NULL for node pointer – Considered "sentinel" for nodes – Indicates no further "links" after this node • Provides end marker similar to how we use partially-filled arrays 31
Accessing Node Data 17 -32
Summary • Structure is collection of different types • Pointer – Provides indirect reference to variable – Created and destroyed while program runs • Linked list – List of nodes where each node points to next 6 -33
- Slides: 33