Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing."— Presentation transcript:

1 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing Pointers, References and Dynamic Data Structures Outline 16.1 Test-Driving the Shopping List Application 16.2 Introducing Pointers 16.3 Pointer Operators 16.4 Passing Arguments to Functions by Reference 16.5 Designing the Shopping List Application 16.6 Constructing the Shopping List Application 16.7 Implementing a Linked List 16.8 Wrap-Up Files ShoppingList.cpp <- Applicaton ShoppingItem.h, ShoppingItem.cpp <- Object List.h, List.cpp <- Linked List

2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Create and initialize pointers and references. –Store the address of a variable in a pointer. –Create a linked list of objects. –Access class members using a pointer. –Use dynamic memory management to create and delete objects.

3 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application

4 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application (Cont.) Figure 16.1 Running the completed Shopping List application. Figure 16.2 Adding a new item to the shopping list. Figure 16.3 Viewing the shopping list contents.

5 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application (Cont.) Figure 16.4 Adding more items to the shopping list. Figure 16.5 Viewing the updated shopping list contents. Items displayed in reverse order of input

6 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction to Pointers Figure 16.6 Directly and indirectly referencing a variable.

7 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction to Pointers (Cont.) Figure 16.8 Representation of y and yPointer in memory.

8 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Working with Pointers Reference the address of operator ( & ) –Returns the address of its operand Dereference value pointed by operator ( * ) –Also known as a unary indirection operatorunary indirection operator –Returns the value pointed by the name of the object to which the operand points The Reference and Dereference operators have complementary (or opposite) meanings. A variable referenced with & can be dereferenced with *.

9 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Reference (the address of) Operator ( & ) The first statement assigns the value 25 to andy (a variable whose address in memory we have assumed to be 1776). The second statement copied to fred the content of variable andy (which is 25). This is a standard assignment operation, as you have done so many times before. Finally, the third statement copies to ted not the value contained in andy but a reference to it (i.e., its address, 1776). The reason is that in this third assignment operation we have preceded the identifier andy with the reference operator (&), so we were no longer referring to the value of andy but to its reference (its address in memory). The variable that stores the reference to another variable (like ted in the previous example) is what we call a pointer. Source: http://www.cplusplus.com/doc/tutorial/pointers/http://www.cplusplus.com/doc/tutorial/pointers/ 1 andy = 25; 2 fred = andy; 3 ted = &andy;

10 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Dereference value pointed by Operator ( * ) Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by". 1 andy = 25; 2 ted = &andy; 3 4 beth = ted; // beth equal to ted ( 1776 ) 5 beth = *ted; // beth equal to value pointed by ted ( 25 ) Source: http://www.cplusplus.com/doc/tutorial/pointers/http://www.cplusplus.com/doc/tutorial/pointers/

11 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring a Variable of Type Pointer The declaration of pointers follows this format: Each points to a different data type. ̶ The data to which they point to do not occupy the same amount of memory (the data). Each pointer occupies the same amount of space in memory (the address). 1 int * number; 2 char * character; 3 float * greatnumber Source: http://www.cplusplus.com/doc/tutorial/pointers/http://www.cplusplus.com/doc/tutorial/pointers/ the asterisk (*) only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.

12 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring a Variable of Type Pointer (Cont.) 1 // my first pointer 2 #include 3 using namespace std; 4 5 int main () 6 { 7 int firstvalue, secondvalue; 8 int * mypointer; 9 10 mypointer = &firstvalue; 11 *mypointer = 10; 12 mypointer = &secondvalue; 13 *mypointer = 20; 14 cout << "firstvalue is " << firstvalue << endl; 15 cout << "secondvalue is " << secondvalue << endl; 16 return 0; 17 } Declaring pointer variable mypointer of type integer. Dereference value pointed by operator

13 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pointer Initialization Pointers can be initialized to 0, NULL or an address ̶ Symbolic constant NULL defined in as equal to 0 ̶ 0 is the only integer that can be directly assigned to a pointer Initialize pointers to avoid pointing to unknown or uninitialized areas of memory 1 int number; 2 int *tommy = &number; The behavior of this code is equivalent to : 1 int number; 2 int *tommy; 3 tommy = &number; Reference the address of operator Declaring pointer variable tommy of type int

14 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Working with References What is a Reference? –It's an alias for an object - another name by which it can be called. The implementation is frequently identical to that of pointers. But don't think of references as pointers - a reference is the object. –Strange phrases like "a reference IS the object" are used quite frequently in the C++ community. Such claims are only useful to hide the fact that C++ pointers & references are so similar that having both in the language is an unnecessary complication. In other contexts, the claims are simply false. Source: http://yosefk.com/c++fqa/ref.htmlhttp://yosefk.com/c++fqa/ref.html

15 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Then Why References? Both C and C++ provide pointers as a way of referring to objects indirectly. C++ added references as an alternative mechanism for doing what is essentially the same job. C++ reference types enable overloaded operators to look like built-in operators, as well as act like them.overloaded operators Source: http://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-Referenceshttp://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-References

16 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring and Initializing a Variable of Type Reference A reference declaration is nearly identical to a pointer declaration, except that a reference declaration uses the & operator instead of the * operator. For example, given: 1 int i = 3; then: 2 int *pi = &i; Declares pi as an object of type "pointer to int" whose initial value is the address of object i, while: 3 int &ri = i; Declares ri as an object of type "reference to int " referring to i. The ampersand (&), as used in line 3 is not the reference operator (although both use the same sign: &). Remember, they are two different functions of one sign. Source: http://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-Referenceshttp://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-References Reference the address of operator Declaring Reference variable ri of type integer.

17 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring and Initializing a Variable of Type Reference Nomenclature –Initializing a reference to refer to an object is often described as "binding the reference to the object." –many programmers pronounce " int * " as "int star," they usually pronounce " int & " as "int ref." Source: http://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-Referenceshttp://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-References

18 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. The Difference between Pointer and Reference Variables You can use a reference as if it were a value : myRef.myData, versus myPtr->myData, which is equivalent to ( *myPtr).myData (in this sense my Ref behaves like ( *myPtr )). A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. –In Fact, a reference must be initialized to point to an object - otherwise, the code won't compile. –After the initialization, you can't make a pointer, point to any number of objects. A pointer can point to NULL while reference can never point to NULL –In fact, a wide class of bugs comes from accessing dangling references - references to objects which were already destroyed. Source: http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-chttp://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c

19 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. The Difference between Pointer and Reference Variables You can't take the address of a reference (it is bound to the object) like you can with pointers (forming a pointer to a pointer). We will see an example of this when we study linked lists. There's no "reference arithmetics" –but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5. Source: http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-chttp://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c

20 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. When to use a Reference or Pointer Pointers can do almost everything that references can do, but they can lead to expressions that don't look right. On the other hand, references have some restrictions that make them less convenient than pointers for implementing algorithms and data structures. As a general rule, –Use pointers to implement algorithms and data structures. –Use references in function parameters and return types to define attractive interfaces. We look at Passing Arguments to Functions by Reference or Pointer in the next section. Source: http://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-Referenceshttp://www.eetimes.com/discussion/programming-pointers/4024641/An-Introduction-to-References

21 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference or Pointer There two (2) ways to perform a pass-by-reference 1.pass-by-reference with reference (&) argument 2.pass-by-reference with pointer (*) argument Review A parameter is the thing used to define a method or function while an argument is the thing you use to call a method or function. Parameter: void mySubroutine (int N){ … } N is a parameter Argument: int X; X = 10; mySubroutine(X); X is an argument

22 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference To indicate that a function passes parameters by reference, follow the variable type with an ampersand ( & ) in the function prototype Figure 16.9 Declaring a function that declares a reference parameter. Declaring a function that declares an int reference parameter Figure 16.10 Passing an argument by reference using a reference argument. Using a variable name to pass-by-reference using a reference argument

23 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Passing variables by reference allows the called function to directly modify the original variable in the caller. In this example the variable input. Figure 16.11 Defining a function that declares a reference parameter. Incrementing the reference parameter

24 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Figure 16.12 Output demonstrating the value of input before and after passing a value by reference with a reference argument.

25 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer To pass a function parameter as a pointer, follow the parameters type in the function prototype with an asterisk ( * ) The asterisk (*), as used here is not the dereference operator (although both use the same sign: *). Remember, they are two different functions of one sign. Figure 16.13 Declaring a function that declares a pointer parameter. Declaring a function that declares an int pointer parameter

26 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Figure 16.14 Passing a variable by reference using a pointer argument. Using the address of operator to pass-by- reference using a pointer

27 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Always use parenthesis to make order of operation clear. The asterisk (*), as used in line 53 is not the dereference operator (although both use the same sign: *). Remember, they are two different functions of one sign. Figure 16.15 Defining a function that declares a pointer argument. Increment the value Declaring Pointer variable countPointer of type integer. Dereference value pointed by operator

28 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Figure 16.16 Output showing input before and after passing it by reference using reference and pointer arguments. Recap Using a reference as a function parameter provides an attractive interface, while using a pointer parameter explicitly shows that a link exists between the parameter and the argument.

29 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Array names are actually pointers pointing to element zero in the array –arrayName is equivalent to &arrayName[ 0 ] –This is why arrays are always pass-by-reference as a pointer.

30 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Display a menu containing options for adding a new item to the list, displaying the shopping list and exiting the application Prompt the user for and input the menu selection If the user chose to add a new item to the list Prompt the user for and input the name and quantity of the item Create a new ShoppingItem object Add the new ShoppingItem to the linked List of ShoppingItems If the user chose to display the shopping list For each item in the list Display the items name and quantity If the user chose to exit Delete each ShoppingItem and exit the application Figure 16.17 Pseudocode for the ShoppingList application. 16.5Designing the Shopping List Application

31 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) Figure 16.18 ShoppingItem class definition. ShoppingItem constructor Get and set functions Member function that displays ShoppingItem information Defining variables that represent an items name and quantity Link data member Pointer variable next of type ShoppingItem Declares a shoppingItem pointer parameter Next Object in a linked list Declares a string reference parameter Objects name Quantity

32 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) A self-referential class contains a pointer to an object of the same class Not setting the link in the last node of a linked data structure to NULL (0) is a (possibly fatal) logic error. Figure 16.19 Two self-referential class objects linked together. Self-referential class objects linked together to form a list Pointer to the first self- referential class object in the list

33 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) Linked list –Collection of linked nodes –Length is dynamic Can increase and decrease as necessary –Singly-linked list has one link per node Figure 16.20 Graphical representation of a linked list.

34 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.6Constructing the Shopping List Application Figure 16.21 Including the ShoppingItem and List class definitions. Including the ShoppingItem.h and List.h header files ShoppingList C++ application Figure 16.22 Defining variables to store user input. Defining variables to store user input

35 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.6Constructing the Shopping List Application (Cont.) Invoking the List class default constructor Figure 16.23 Creating a List object. Creating a List object C++ is case sensitive so shoppingList the list is not the same as ShoppingList the application

36 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 1 Figure 16.2 Adding a new item to the shopping list.

37 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 1 Figure 16.24 Prompting the user for and inputting the items name and quantity. The ignore function removes one character from the input stream Prompting the user for and inputting the items name and quantity Figure 16.25 Adding a new ShoppingItem object to ShoppingList. Add a new ShoppingItem to the linked list

38 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 2 Figure 16.26 Displaying the name and quantity for each ShoppingItem in the linked list. Displaying the contents of the shopping list Figure 16.3 Viewing the shopping list contents.

39 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 3 Applications should delete any dynamic memory allocated during run-time. Figure 16.27 Deleting each ShoppingItem object from the ShoppingList. Deleting all ShoppingItem objects Figure 16.38 Terminating the Shopping List application.

40 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 16.28 List class definition. 16.7Implementing a Linked List List constructor Declaring set and get functions for the pointer to the first item in the list Declaring functions to manipulate the linked list of ShoppingItems Defining a pointer to the first item in the linked list List header file

41 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Keyword this –Inside a member function, you can explicitly refer to the object of that class on which the function was called with the keyword this Arrow operator ( -> ) –Shorthand notation for use with pointers –myObject->myData is equivalent to ( *myObject ).myData Figure 16.29 List constructor definition. Initializing the firstItem variable as a null pointer List C++ object

42 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 16.30 Graphical representation of inserting an item at the front of a linked list. 16.7Implementing a Linked List (Cont.)

43 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Dynamic memory allocation –Technique of obtaining memory during execution time –Allocated memory can be released so that it can be reused later new operator –Takes as an argument the type of object being created –Returns a pointer to newly created object Figure 16.31 Creating a ShoppingItem object in the addItem function. Creating a ShoppingItem using the new operator

44 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Assign NULL (zero) to the link member of a new node –Uninitialized pointers often lead to dangerous runtime errors Figure 16.32 Updating pointers when adding an item to the front of a linked list. Updating the new ShoppingItems nextItem variable and the Lists firstItem variable

45 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure 16.33 Defining the displayList function. Creating a pointer to the first item in the linked list Displaying a header

46 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure 16.34 Displaying each ShoppingItems name and quantity. Displaying the name and quantity for each ShoppingItem

47 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Deallocating memory –Not releasing dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely delete operator –Runs the destructor and deallocates the memory allocated by the new operator –Returns memory to the system

48 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure 16.35 Defining the deleteList function. Setting currentItem so that it points to the first item in the linked list Repeat until reaching the end of the list Variable currentItem is a pointer to type ShoppingItem

49 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Destructors –Functions that run just before an objects memory is released Figure 16.36 Deleting each ShoppingItem in the linked list. Store a temporary copy of the pointer to the next ShoppingItem Delete the ShoppingItem to which currentItem points Move to the next item in the List Temporary variable nextItem is a pointer to type ShoppingItem

50 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure 16.37 Shopping List application after entering several items.

51 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure 16.38 Terminating the Shopping List application.

52 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.h (1 of 2) List constructor

53 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.h (2 of 2) Get and set functions for the pointer to the first item in the list Declaring functions to manipulate the linked list of ShoppingItem s Pointer to the first item in the linked list

54 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (1 of 5) Initialize the firstItem as a null pointer

55 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (2 of 5)

56 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (3 of 5) Update the firstItem to point to the new ShoppingItem Update the appropriate pointers to add the new item to the list

57 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (4 of 5) Create a pointer to the first item in the linked list Display a header Display the name and quantity for each ShoppingItem

58 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (5 of 5) Create a pointer to the first item in the linked list Delete each ShoppingItem in the linked list

59 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.h (1 of 2) ShoppingItem constructor Get and set functions

60 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.h (2 of 2) Member function that displays ShoppingItem information Defining variables that represent an item name and quantity Link data member

61 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (1 of 4)

62 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (2 of 4)

63 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (3 of 4)

64 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (4 of 4)

65 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (1 of 4) Include the ShoppingItem.h and List.h header files Define variables to store user input Create a List object

66 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (2 of 4) Remove the newline from the input stream Prompt the user for and input the items name and quantity Add a new ShoppingItem to the linked list

67 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (3 of 4) Display the contents of the shopping list Delete all ShoppingItem objects in the linked list

68 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (4 of 4)

69 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 16 Shopping List Application. Turn in annotated source file with your own comments for Exercise 16.11 or Exercise 16.16 if you finish the design challenge. Answer and Turn-in Tutorial 16 Questions 16.1 to 16.10. Note: The author defines synonym on page 371. Always write the question followed by the answer. Remember to highlight the answer. Exercises 16.11 and 16.16. For both exercises start from your completed Tutorial. Due next Wednesday

70 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 2 – Introduction to Linked Lists

71 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 3 – Data Operations on Linked List

72 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 3 – Data Operations on Linked List


Download ppt "© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing."

Similar presentations


Ads by Google