The c ++ Programming Language Lecture 6 OOP With Templates
The C++ Programming Language Lecture 6: OOP with Templates
Basic Concepts and Syntaxes
Basic Concepts and Syntaxes
Template u Original name-Parameterized Types Parameterized-Abstracted data type information from Implementation Type- For every class template or function template, there are different features and characters when bound to different data pes, they act like a type Template generate a function or class automatically according to the value or type user specified string, vector, list are all class templates, and we will implement our template in this lecture
Template ◼ Original name – Parameterized Types ◼ Parameterized – Abstracted data type information from implementation ◼ Type – For every class template or function template, there are different features and characters when bound to different data types, they act like a type ◼ Template generate a function or class automatically, according to the value or type user specified ◼ string, vector, list are all class templates, and we will implement our template in this lecture
Starting Point-the binary tree class Piglet Root Eeyore RoO Left child Right child Chris K anga Pooh Tigger Operations that need to be provided Insert, remove Find, clear Print(preorder, inorder, postorder) Preorder: Piglet, Eeyore, Chris, Kanga, Roo, Pooh, Tigger Inorder: Chris, Eeyore, Kanga, Piglet, Pooh, Roo, Tigger Postorder: Chris Kanga, Eeyore, pooh, Tigger, roo piglet
Starting Point – the binary tree class ◼ Operations that need to be provided ◼ Insert, remove ◼ Find, clear ◼ Print (preorder, inorder, postorder) – Preorder: Piglet, Eeyore, Chris, Kanga, Roo, Pooh, Tigger – Inorder: Chris, Eeyore, Kanga, Piglet, Pooh, Roo, Tigger – Postorder: Chris, Kanga, Eeyore, Pooh, Tigger, Roo, Piglet Piglet Roo Pooh Tigger Right child Eeyore Chris Kanga Left child Root
The binary tree class 2 classes BinaryTree- storing only one pointer pointing to the root BINode-storing the value of node and links to left and right childs We provide a non-template BTNode class declaration class string BTNode p pI rivate string m szVal nt m iCnt string btNode* Child string btnode* rchild };
The binary tree class ◼ 2 classes ◼ BinaryTree – storing only one pointer pointing to the root ◼ BTNode – storing the value of node and links to left and right childs ◼ We provide a non-template BTNode class declaration class string_BTNode { public: //… private: string m_szVal; int m_iCnt; string_BTNode* lchild; string_BTNode* rchild; };
Parameterized Types u Without Template, we have to implement multiple BTNode classes with different names and data types For those insertion, deletion and traversing ops, their mechanism won't change when node data type changes thus are type- independent? a We extract the "type-dependent"parts and parameterize them
Parameterized Types ◼ Without Template, we have to implement multiple BTNode classes with different names and data types ◼ For those insertion, deletion and traversing ops, their mechanism won’t change when node data type changes, thus are “typeindependent” ◼ We extract the “type-dependent” parts and parameterize them
Parameterized Types(cont) template< typename valtype class btnode //forward declaration template typename valtype class btnode //class definition public The valtype in Class template is used as a space reserver pI rivate valtype m Val a It's a type parameter that int m icnt could replace any built-in BTNode* Child BTNode* rchild type or user-defined type }; while running
Parameterized Types (cont.) template class BTNode; //forward declaration //… template class BTNode //class definition { public: //… private: valtype m_Val; int m_iCnt; BTNode* lchild; BTNode* rchild; }; ◼ The valtype in class template is used as a space reserver ◼ It’s a type parameter that could replace any built-in type or user-defined type while running
Parameterized Types(cont) template typename elemtype> class Binary Tree //forward declaration template typename valtype class btnode public pI rivate friend class binary Tree
Parameterized Types (cont.) template class BinaryTree; //forward declaration template class BTNode { public: //… private: //… friend class BinaryTree; };
Parameterized Types(cont) u To generate entity class from the class template, real types should be provided in the template parameter list(<>) which the type parameter would bind to ■ Usage BTNode< int bti BTNode< string btS a bti and bts are different objects from different classes
Parameterized Types (cont.) ◼ To generate entity class from the class template, real types should be provided in the template parameter list(<>) which the type parameter would bind to ◼ Usage BTNode bti; BTNode bts; ◼ bti and bts are different objects from different classes
Parameterized Types(cont) u Definition of class BinaryTree template*m root a When we need to add template parameter list after the class template? a Except in the definition of class template and its member functions
Parameterized Types (cont.) ◼ Definition of class BinaryTree template class BinaryTree { public: //… private: //… BTNode* m_root; }; ◼ When we need to add template parameter list after the class template? ◼ Except in the definition of class template and its member functions