1 we reviewed key points of pointer variables and dynamic
play

1. We reviewed key points of pointer variables and dynamic - PDF document

CISC2200 Note 10/3/2019 1. We reviewed key points of pointer variables and dynamic variables 2. Lab2 review a) Its our own version of vector template (as provided in C++ STL). b) Discussion of generic data type In lab2, we do so by


  1. CISC2200 Note 10/3/2019 1. We reviewed key points of pointer variables and dynamic variables 2. Lab2 review a) It’s our own version of vector template (as provided in C++ STL). b) Discussion of generic data type In lab2, we do so by making unsorted type to store ItemType objects ItemType class will be defined to 1) have our Appointment type member inside it class ItemType { …. private: Appointment info; }; 2) or to be an alias of Appointment type typedef Appointment ItemType c) Using dynamic variables in a class dynamic variables allocated for an object (here dynamically allocated array to store ItemTypes) will be ** Allcoated in constructors ** Deallocated in destructors d) PutItem() implementation hints 3 Implement unsorted type using linked structure The idea: using a linked structure to store the items.

  2. When adding an item into the unsorted type, create a node to store the item, and linked it with existing nodes …. Code walk-through: a) run command to obtain the sample code cpLinkedList b) practice questions: —> A member function that displays all items: —-> A membre function that reverse the linked list? 4 Generic data type via class templte code studies (Run the following command to obtain the code) cpTemplate Converting UnsortedType into a class template… Step 1: In unsorted.h, add the line before the class… ———————————————————————————————— //a prefix to say that this is a class template, i.e., it // can be used to generate // UnsortedType<int>, UnsortedType<double> …. // ItemType is like a type parameter, telling us the type // of items this class is storing template <class ItemType> class UnsortedType { public: struct NodeType { ItemType info; …. }; —————————————————————————————

  3. Step 2: in unsorted.cpp, modify each member function’s prototype as follows: ——————————————————————————————————— // all member functions is prefiexed with keyword // template template<class ItemType> int UnsortedType<ItemType>::GetLength() const // Post: Number of items in the list is returned. { return length; } ———————————————————————————————————— Step 3: in main.cpp: ** include the implementation file (not linked with it) #include “unsorted.cpp” Why? As unsorted.cpp does not specify a class implementation, it’s a template for generating class. ** declare unsortedType object by passing a type parameter ——————————————————————————- #include “unsorted.cpp” … int main() { UnsortedType <Appointment> apptBook; //Compiler (g++) read above line, and will use //class template defined in unsorted.cpp to generate // a class UnsortedType<Appointment> by replacing every // ItemType by Appointment UnsortedType <DollarAmount> checks; // Same here, a class UnsortedType<DollarAmount> will be generated by compiler by replacing every word “ItemType” by DollarAmount… … } ———————————————————————————- Step 4: compile (note that unsorted.cpp is not here!) g++ listDriverStd.cpp DollarAmoung.cpp

Recommend


More recommend