软件设计(II)教学课件:Chapter12 Templates(模板).pptVIP

  • 1
  • 0
  • 约8.86千字
  • 约 27页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter12 Templates(模板).ppt

Nested Class ?? Enclosing Class Independent in members Not inheritance, not membership Having their own members No special access to each other Related in “type” name Nested class is a “type” member of the enclosing class type “Name” of nested is visible in enclosing class scope Visibility in other scope? Determined by the accessibility keyword in declaration * Nested Class as a Type * template typename Type class Queue{ //.... private: struct QueueItem{ QueueItem(const Type ); Type item; QueueItem *next; }; QueueItem *head; QueueItem *tail; }; template typename Type struct QueueType::QueueItem{ QueueItem(const Type ); Type item; QueueItem *next; }; template typename Type class Queue{ //.... private: struct QueueItem; QueueItem *head; QueueItem *tail; }; Typename * class TypeA { public: class SubType { public: string toString() { return subType in TypeA;} }; int X; }; template class T class MyClass{ public: typename T::SubType sto; void Test{ T::X *Y; } }; A Complete Example * class TypeA { public: class SubType { public: string toString() { return subType in TypeA;} }; }; class TypeB { public: class SubType { public: string toString() { return subType in TypeB;} }; }; template class T class MyClass{ public: typename T::SubType sto; }; int main(){ MyClassTypeA obj1; cout obj1.sto.toString() endl; MyClassTypeB obj2; cout obj2.sto.toString() endl; } A Summary GP and templates Class template Template in inheritance Nested class and template Function template Template in overloading Order of matching * Review Questions Q2. Q3. * Chapter 12 Templates(模板) §12.1 Introduction §12.2 Class Templates §12.3 Function Templates §12.4 Nested Class and Templates * §12.1 Introduction Generic programming An approach to making the same program handling different types of data Templ

文档评论(0)

1亿VIP精品文档

相关文档