Templates and Inheritance Templates Inheritance 1 Options a

  • Slides: 6
Download presentation
Templates and Inheritance Templates & Inheritance 1 Options: - a template may be derived

Templates and Inheritance Templates & Inheritance 1 Options: - a template may be derived from another template. a non-template class may be derived from a template may be derived from a non-templates may use multiple inheritance. We will briefly examine the first two cases here; the remaining cases are left to the reader. Computer Science Dept Va Tech April 2001 OO Software Design and Construction © 2001 Mc. Quain WD & Keller BJ

A Base Template… Templates & Inheritance 2 Recall the queue template Queue. T from

A Base Template… Templates & Inheritance 2 Recall the queue template Queue. T from earlier notes: const int Size = 100; template <class Foo> class Queue. T { private: Foo buffer[Size]; int Head, Tail, Count; public: Queue. T(); void Enqueue(Foo Item); Foo Dequeue(); int get. Size() const; bool is. Empty() const; bool is. Full() const; ~Queue. T(); }; Computer Science Dept Va Tech April 2001 OO Software Design and Construction © 2001 Mc. Quain WD & Keller BJ

… and a Derived Templates & Inheritance 3 We can derive an extended queue

… and a Derived Templates & Inheritance 3 We can derive an extended queue template that adds the ability to “peek” at the element at the front of the queue: template <class Foo> class Inspectable. Queue. T : public Queue. T<Foo> { public: Inspectable. Queue. T(); Foo Inspect(); ~Inspectable. Queue. T(); }; Computer Science Dept Va Tech April 2001 OO Software Design and Construction © 2001 Mc. Quain WD & Keller BJ

Using Inspectable. Queue Template Inspectable. Queue. T<Location> Path; Location loc 1(. . . );

Using Inspectable. Queue Template Inspectable. Queue. T<Location> Path; Location loc 1(. . . ); Location loc 2(. . . ); . . . Path. Enqueue(loc 1); Path. Enqueue(loc 2); . . . Location Front = Path. Dequeue(); Location new. Front = Path. Inspect(); Computer Science Dept Va Tech April 2001 OO Software Design and Construction Templates & Inheritance 4 // base class method // derived class // method © 2001 Mc. Quain WD & Keller BJ

Non. Template Derived from Templates & Inheritance 5 Recalling the linked list template, Link.

Non. Template Derived from Templates & Inheritance 5 Recalling the linked list template, Link. List. T, discussed earlier, we can derive a Polygon class from it: class Polygon : public Link. List. T<Location> { public: Polygon(); void Widder. Shins(); // sort points void Print(ostream& Canvas); // draw itself }; Note that the template Link. List. T is elaborated with a specific type. Computer Science Dept Va Tech April 2001 OO Software Design and Construction © 2001 Mc. Quain WD & Keller BJ

Using the Polygon Class Polygon Hex; . . . Hex. Append. Node(Location(20, 20)); Hex.

Using the Polygon Class Polygon Hex; . . . Hex. Append. Node(Location(20, 20)); Hex. Append. Node(Location(30, 30)); Templates & Inheritance 6 // inherited from // Link. List. T template // insert other locations Hex. Print(cout); Computer Science Dept Va Tech April 2001 // derived class method OO Software Design and Construction © 2001 Mc. Quain WD & Keller BJ