DOC PREVIEW
Yale CPSC 427 - Lecture 15
School name Yale University
Pages 22

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineVirtue DemoLinear Data Structure DemoTemplatesOutline Virtue Linear TemplatesCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 15October 28, 2010CPSC 427a 1/22Outline Virtue Linear TemplatesVirtue DemoLinear Data Structure DemoTemplatesCPSC 427a 2/22Outline Virtue Linear TemplatesVirtue DemoCPSC 427a 3/22Outline Virtue Linear TemplatesVirtual virtueclass Basic {public:virtual void print(){cout <<"I am basic. "; }};class Virtue : public Basic {public:virtual void print(){cout <<"I have virtue. "; }};class Question : public Virtue {public:void print(){cout <<"I am questing. "; }};CPSC 427a 4/22Outline Virtue Linear TemplatesMain virtueWhat does this do?int main (void) {cout << "Searching for Virtue\n";Basic* array[3];array[0] = new Basic();array[1] = new Virtue();array[2] = new Question();array[0]->print();array[1]->print();array[2]->print();return 0;}See demo 15a-Virtue!CPSC 427a 5/22Outline Virtue Linear TemplatesLinear Data Structure DemoCPSC 427a 6/22Outline Virtue Linear TemplatesUsing polymorphismSimilar data structures:ILinked list implementation of a stack of items.ILinked list implementation of a queue of items.Both support a common interface:Ivoid push(Item*)IItem* pop()IItem* peek()Iostream& print(ostream&)They differ only in where push() places a new item.The demo 15b-Virtual (from Chapter 15 of textbook) showshow to exploit this commonality.CPSC 427a 7/22Outline Virtue Linear TemplatesInterface fileWe define this common interface by the abstract class.class Container {public:virtual void put(Item*) =0;virtual Item* pop() =0;virtual Item* peek() =0;virtual ostream& print(ostream&) =0;};Any class derived from it is required to implement these fourfunctions.We could derive Stack and Queue directly from Container, butwe instead exploit even more commonality between these twoclasses.CPSC 427a 8/22Outline Virtue Linear TemplatesClass Linearclass Linear: public Container {protected: Cell* head;private: Cell* here; Cell* prior;protected: Linear();virtual ~Linear ();void reset();bool end() const;void operator ++();virtual void insert( Cell* cp );virtual void focus() = 0;Cell* remove();void setPrior(Cell* cp);public: void put(Item * ep);Item* pop();Item* peek();virtual ostream& print( ostream& out );};CPSC 427a 9/22Outline Virtue Linear TemplatesExample: Stackclass Stack : public Linear {public:Stack(){}~Stack(){}void insert( Cell* cp ) { reset(); Linear::insert(cp); }void focus(){ reset(); }ostream& print( ostream& out ){out << " The stack contains:\n";return Linear::print( out );}};CPSC 427a 10/22Outline Virtue Linear TemplatesExample: Queueclass Queue : public Linear {private:Cell* tail;public:Queue() { tail = head; }~Queue(){}void insert( Cell* cp ) {setPrior(tail); Linear::insert(cp); tail=cp; }void focus(){ reset(); }};CPSC 427a 11/22Outline Virtue Linear TemplatesClass structureClass structure.IContainer specifies the common interface.ILinear contains the bulk of the code. It is derived fromContainer.IStack and Queue are both derived from Linear.ICell is a “helper” class that is aggregated by Linear.IItem is the base type for the container elements. It is definedby a typedef here but would normally be specified by atemplate.IExam is a non-trivial item type used by main to illustratestacks and queues.CPSC 427a 12/22Outline Virtue Linear TemplatesC++ featuresThe demo illustrates several C++ features.1. [Container] Pure abstract class.2. [Cell] Friend functions.3. [Cell] Printing a pointer in hex.4. [Cell] Operator extension operator Item*().5. [Linear] Virtual functions and polymorphism.6. [Linear] Scanner pairs (prior, here) for traversing a linked list.7. [Linear] Operator extension operator ++()8. [Linear, Exam] Use of private, protected, and publicin same class.CPSC 427a 13/22Outline Virtue Linear Templates#include structureGetting #include’s in theright order.Problem: Making surecompiler sees symboldefinitions before they areused.Partial solution: Make de-pendency graph. If notcyclic, each .hpp file in-cludes the .hpp files justabove it.exam.hppitem.hppcontainer.hpplinear.hppqueue.hppstack.hppcell.hppCPSC 427a 14/22Outline Virtue Linear TemplatesTemplatesCPSC 427a 15/22Outline Virtue Linear TemplatesTemplate overviewTemplates are instructions for generating code.Are type-safe replacement for C macros.Can be applied to functions or classes.Allow for type variability.Example:template <class T>class FlexArray { ... };Later, can instantiateclass RandString : FlexArray<const char*> { ... };and useFlexArray<const char*>::put(store.put(s, len));CPSC 427a 16/22Outline Virtue Linear TemplatesTemplate functionsDefinition:template <class X> void swapargs(X& a, X& b) {X temp;temp = a;a = b;b = temp;}Use:int i,j;double x,y;char a, b;swapargs(i,j);swapargs(x,y);swapargs(a,b);CPSC 427a 17/22Outline Virtue Linear TemplatesSpecializationDefinition:template <> void swapargs(int& a, int& b) {// different code}This overrides the template body for int arguments.CPSC 427a 18/22Outline Virtue Linear TemplatesTemplate classesLike functions, classes can be made into templates.template <class T>class FlexArray { ... };makes FlexArray into a template class.When instantiated, it can be used just like any other class.For a flex array of ints, the name is FlexArray<int>.No implicit instantiation, unlike functions.CPSC 427a 19/22Outline Virtue Linear TemplatesCompilation issuesRemote (non-inline) template functions must be compiled andlinked for each instantiation.Two possible solutions:1. Put all template function definitions in the .hpp file alongwith the class definition.2. Put template function definitions in a .cpp file as usual butexplicitly instantiate.E.g., template class FlexArray(int); forces compilationof the int instantiation of FlexArray.CPSC 427a 20/22Outline Virtue Linear TemplatesTemplate parametersTemplates can have multiple parameters.Example:template<class T, int size> declares a template with twoparameters, a type parameter T and an int parameter size.Template parameters can also have default values.Used when parameter is omitted.Example:template<class T=int, int size=100> class A { ... }.A<double> instantiates A to type A<double, 100>.A<50> instantiates A to type A<int, 50>.CPSC 427a 21/22Outline Virtue Linear TemplatesUsing template classesDemo 15c-Evaluate implements a simple expression evaluatorbased on a precedence parser.It derives a template class Stack<T> from the template classFlexArray<T> introduced in 13-Hangman-full.The precedence parser


View Full Document

Yale CPSC 427 - Lecture 15

Download Lecture 15
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 15 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 15 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?