Unformatted text preview:

OutlineLinear Container Design STL and PolymorphismOutline Linear STL & PolyCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 21November 15, 2011CPSC 427a, Lecture 21 1/29Outline Linear STL & PolyLinear Container DesignSTL and PolymorphismCPSC 427a, Lecture 21 2/29Outline Linear STL & PolyLinear Container DesignCPSC 427a, Lecture 21 3/29Outline Linear STL & PolyOverview of linear container exampleWe’ve seen three closely related designs for linear containers:I18c-VirtualI20a-MultipleI20b-Multiple-templateCommon to all three examples is the use of a linked list of Cellobjects to implement various kinds of containers store data objectsof type Exam.CPSC 427a, Lecture 21 4/29Outline Linear STL & PolyDifferences in functionality18c-Virtual implements a FIFO class Stack and a LIFOclass Queue of unordered Exams.20a-Multiple and 20b-Multiple add a key() method to Examand implement two new data structures that make use of the key:Iclass List is an unordered set of elements, where push()inserts an element at the front of the list and pop() promptsthe user for the key of the element to be removed.Iclass PQueue maintains its elements in a sorted list, wherepush() inserts an element into the middle of the list so as tomaintain the elements in descending order, based on anunderlying ordering on keys, and pop() removes the front(largest) element.CPSC 427a, Lecture 21 5/29Outline Linear STL & PolyClass structureThe class structure of the three implementations are similar as arethe main programs. All have have the following classes:Iclass Item are the objects that live in the containers.Iclass Exam are the application-specific user objects.Iclass Container is the abstract interface for all containers.Iclass Linear is the common code for the containers.In 18c-Virtual, Item and Exam are synonymous.In 20a-Multiple and 20b-Multiple-template, Item is derivedfrom Exam and extends the comparison operators < and ==.CPSC 427a, Lecture 21 6/29Outline Linear STL & PolyTemplate structure20b-Multiple-template adds a template parameter class T torepresent the application-specific user object type.In the sample application, main() instantiates the template asList<Item> and PQueue<Item>.As before, Item is derived from Exam, so to create containers for anew user type MyData would require rewriting Item appropriately.CPSC 427a, Lecture 21 7/29Outline Linear STL & PolyFurther extensionsCan we make Item a template class, e.g.,template <class T>class Item : public T, public Ordered<KeyType> { ... };Then the user with application-specific class MyData could just useItem<MyData> wherever 20b-Multiple-template uses Item.CPSC 427a, Lecture 21 8/29Outline Linear STL & PolyTwo problemsTwo problems must be overcome to make this approach work:1. Type KeyType appropriate to MyData must be definedsomewhere.2. Item contains an Exam-specific constructorItem(const char* init, int sc) : Exam(init, sc){}that would have to be eliminated in favor of something thatwould work in general.CPSC 427a, Lecture 21 9/29Outline Linear STL & PolyDefining KeyTypeProblem 1 can be solved by putting a typedef for KeyType intoclass MyClass. This type name can be used as follows:template <class T>class Item : public T, public Ordered<typename T::KeyType>{ ... };CPSC 427a, Lecture 21 10/29Outline Linear STL & PolyConstructing the data elementsProblem 2 is not so easily solved. Some possibilities:IHave the user construct a MyData object, then have Item useMyData’s copy constructor, e.g.,Item(const T& data) : T(data) {}This adds a time penalty and a requirement that T becopyable.IInstead of deriving Item<T> from T, compose a T* inItem<T>, and initialize it with a generic Item constructor,e.g.,T* base;Item(T* dt) : base(dt) {}CPSC 427a, Lecture 21 11/29Outline Linear STL & Poly21a-Multiple-templateThis second idea is implemented in example21a-Multiple-template.A consequence of this design is that main() now mentions onlythe user type (Exam), not Item, effectively isolating the userinterface from the underlying implementation, e.g.,List<Exam> L;L.put( new Exam("Ned", 29) );L.put( new Exam("Leo", 37) );L.put( new Exam("Max", 18) );CPSC 427a, Lecture 21 12/29Outline Linear STL & PolyStorage managementA basic design question has to do with ownership of dynamicstorage.In STL, the user retains ownership of arguments, and a containersuch as vector manages copies of the elements.In these linear container examples, ownership transfers to thecontainer.IThe user creates a element using new and passes a pointer tothe put() function.Ipop() returns ownership of the object to the user, who isthen responsible for its eventual deletion.IThe container is responsible for deleting any objects it stillcontains when it goes away.CPSC 427a, Lecture 21 13/29Outline Linear STL & PolySTL and PolymorphismCPSC 427a, Lecture 21 14/29Outline Linear STL & PolyDerivation from STL containersCommon wisdom on the internet says not to inherit from STLcontainers.For example,http://en.wikipedia.org/wiki/Standard Template Library says,“STL containers are not intended to be used as baseclasses (their destructors are deliberately non-virtual);deriving from a container is a common mistake.”This reflects Rule 35 of Sutter and Alexandrescu,“Avoid inheriting from classes that were not designed tobe base classes.”CPSC 427a, Lecture 21 15/29Outline Linear STL & PolyReplacing authority with understandingC++ is a complicated and powerful language.Some constructs such as classes are used for several differentpurposes.What is appropriate in one context may not be in another.Simple rules will not make you a good C++ programmer. Thought,understanding, and experience will.CPSC 427a, Lecture 21 16/29Outline Linear STL & PolyTwo kinds of derivationC++ supports two distinct kinds of derivation:ISimple derivation.IPolymorphic derivation.class A { ... };class B : public A { ... };We say B is derived from A, and B inherits members from A.Each B object has an A object embedded within it.The derivation is simple if no members of A are virtual;otherwise it is polymorphic.CPSC 427a, Lecture 21 17/29Outline Linear STL & PolyHow are they the same?With both kinds of derivation, a function of the base class A canbe overridden by a function in B.In both cases, one can create and delete objects of class B.Both A’s and B’s destructor are called when a B object is deleted.#include <iostream>class A { public:~A() { std::cout << "A’s destructor called" << std::endl; }};class B: public A {


View Full Document

Yale CPSC 427 - Lecture 21

Download Lecture 21
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 21 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 21 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?