CSC 1061 Week 11 LinkList, Recursion, Templates PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
These notes cover C++ templates, linked lists, and recursion for CSC 1061, likely an undergraduate computer science course. The document outlines concepts like function templates, class templates, and implementing the rule of 3 for classes.
Full Transcript
CSC 1061 LINKLIST DATA STRUCTURE AND THE RULE OF 3 TEMPLATES AND RECURSION OBJECTIVES AGENDA: WEEK 11 Design, implement and test 1. Template classes collection classes that use 2. Node and LinkList linked lists underlying to store 3. Ru...
CSC 1061 LINKLIST DATA STRUCTURE AND THE RULE OF 3 TEMPLATES AND RECURSION OBJECTIVES AGENDA: WEEK 11 Design, implement and test 1. Template classes collection classes that use 2. Node and LinkList linked lists underlying to store 3. Rule of 3 a collection of elements 4. LinkLIst Rule of 3 Ensure value semantics by 1. Assignment Operator implementing the rule of 3 for 2. Copy Constructor dynamic classes. 3. Destructor Convert Node and LinkList to 5. Recursion generic template classes. REVIEW AND PRE-CHALLENGE Review 4.3 Implementing an Unordered Linklist Work through the CodeLens examples Complete the multiple-choice questions Note: Our LinkList is required to keep track of both head and tail TEMPLATES OVERVIEW (GEEKSFORGEEKS) The idea is to pass data type as a parameter so that we don’t need to write the same code for different data types – code is generic. How do templates work? Templates are expanded at compile time. The compiler does type checking before template expansion. Source code contains only function/class, but compiled code may contain multiple copies of same function/class. Function Templates – a generic function that can be used for different data types arguments. Class Templates - a class defines something that is independent of the data type (data structure or container). DISADVANTAGES OF TEMPLATES When templates are used, all code is exposed. Some compilers have poor support of templates. Most compilers produce unhelpful, confusing error messages when errors are detected in the template code. o It can make it challenging to develop the template https://www.mygreatlearning.com/blog/templates-in-cpp/#Advantages%20of%20Using%20Templates FUNCTION TEMPLATE Function templates are useful for overloaded functions with the same definitions The template allows not only the parameter value to be passed into the function, but the data type of the parameter as well. Templates rely upon using operators in the generic function definition Run and trace the code and notice the function that are called based upon the data type of the arguments. Comment out the explicit functions and uncomment the template function. Run and trace the code, what do you notice? EXAMPLE STD CLASS TEMPLATES Class templates are useful for data structure (collection classes) so that the type of data can be passed into the container Examples of class templates: std::array and std::vector Template parameter T is used for the type of the elements contained in the std::array or std::vector Class templates rely upon operators to keep definitions generic EXAMPLE CLASS TEMPLATE (GUIDE TO C++ TEMPLATES) template class Item { class Item { int data; T data; public: public: Item() : data(0) { } Item() : data(T()) {} int getData() const{return data;} T getData() const{return data;} void setData(int dataIn){data = void setData(T dataIn) {data = dataIn;} dataIn;} friend ostream& friend ostream& operator