当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

《The C++ Programming Language》课程教学资源(PPT课件讲稿)Lecture 06 OOP with Templates

资源类别:文库,文档格式:PPT,文档页数:59,文件大小:660KB,团购合买
点击下载完整版文档(PPT)

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 “type￾independent” ◼ 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

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共59页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有