Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ TEMPLATES, Lecture notes of C programming

Complete the line of code to create a new. BSTNode object with int data on the heap and assign nodePtr to point to it. 9. BSTNode<int>* nodePtr. BST, with ...

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

ekambar
ekambar 🇺🇸

4.7

(22)

18 documents

Partial preview of the text

Download C++ TEMPLATES and more Lecture notes C programming in PDF only on Docsity! C++ TEMPLATES Problem Solving with Computers-ll C++ ‘ re e <S08"" gtd yincd™ ames? 3 us 00! piv ace? gnt - a corto • Pa02 released! • Its about implementing a BST with a movie data set, collecting and analyzing running time! • Part of the assignment involves writing a report, explaining the trends in your data • Due 06/06 • Start early! • Midterm grades released! • Max: 55/50 (5 students) • Median: 90% • Mean: 88% Announcements When you write a template function, you choose a data type for the function to depend upon... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } A Template Function for Maximum BST, with templates: template<class Data> class BSTNode { public: BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; BSTNode( const Data & d ) : data(d) { left = right = parent = nullptr; } }; !6 BST, without templates: class BSTNode { public: BSTNode* left; BSTNode* right; BSTNode* parent; int const data; BSTNode( const int& d ) : data(d) { left = right = parent = nullptr; } }; BST, with templates: template<class Data> class BSTNode { public: BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; BSTNode( const Data & d ) : data(d) { left = right = parent = nullptr ; } }; How would you create a BSTNode object on the runtime stack? !7 A. BSTNode n(10); B. BSTNode<int> n; C. BSTNode<int> n(10); D. BSTNode<int> n = new BSTNode<int>(10); E. More than one of these will work { } syntax OK too Working with a BST template<typename Data> class BST { private: BSTNode<Data>* root; //Pointer to the root of this BS public: /** Default constructor. Initialize an empty BST. */ BST() : root(nullptr){ } void insertAsLeftChild(BSTNode<Data>* parent, const Data& item){ // Your code here } !10 Working with a BST: Insert //Assume this is inside the definition of the class void insertAsLeftChild(BSTNode<Data>* parent, const Data& item) { // Your code here } Which line of code correctly inserts the data item into the BST as the left
 child of the parent parameter. A.parent.left = item; B.parent->left = item; C.parent->left = BSTNode(item); D.parent->left = new BSTNode<Data>(item); E.parent->left = new Data(item); !11 Working with a BST: Insert void insertAsLeftChild(BSTNode<Data>* parent, const Data& item) { parent->left = new BSTNode<Data>(item); } Is this function complete? (i.e. does it do everything it needs to correctly insert the node?) A. Yes. The function correctly inserts the data B. No. There is something missing. !12 Template classes: Member function definition template<class T> class BST{ //Other code Node* getNodeFor(T value, Node* n) const; }; For the compiler a name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless its preceded by a typename Template classes: Including the implementation //In bst.h class BST{ //code }; #include "bst.cpp" How to Convert a Container Class to a Template 1. The template prefix precedes each function prototype or implementation. 2. Outside the class definition, place the word <Item> with the class name, such as bag<Item>. 3. Use the name Item instead of value_type. 4. Outside of member functions and the class definition itself, add the key- word typename before any use of one of the class’s type names. For example: typename bag<Item>::size_type 5. The implementation file name now ends with .template (instead of . CXx), and it is included in the header by an include directive. 6. Eliminate any using directives in the implementation file. Therefore, we must then write std:: in front of any Standard Library function such as Std: : copy. 7. Some compilers require any default argument to be in both the prototype and the function implementation. Review and demo an example
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved