1 / 7

CSE 2341 Object Oriented Programming with C++ Note Set #17

CSE 2341 Object Oriented Programming with C++ Note Set #17. Overview. Templates Class templates. Class Templates. Templates can be used to create generic ADTs. Can create one general version of class w/o having to duplicate for different data types

temira
Download Presentation

CSE 2341 Object Oriented Programming with C++ Note Set #17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 2341Object Oriented Programming with C++Note Set #17

  2. Overview • Templates • Class templates

  3. Class Templates • Templates can be used to create generic ADTs. • Can create one general version of class w/o having to duplicate for different data types • Template prefix placed before class declaration

  4. SimpleVector template<class T> class SimpleVector { private: T* aptr; int arraySize; void memError(); void subError(); public: SimpleVector() {aptr = 0; arraySize = 0;} SimpleVector(int); SimpleVector(const SimpleVector&); ~SimpleVector(); int size() {return arraySize;} t& operator[](const int&); } Complete Definition in Handout SimpleVector.h

  5. Defining Objects of the Class Template • Data type that is to be passed as the type parameter is placed in angle brackets after the class name as in: SimpleVector<int> intTable(10); SimpleVector<double> doubleTable(10)

  6. Templates and Inheritance • Templated classes can be involved in inheritance template <class T> class SearchableVector:public SimpleVector<T> { public: SearchableVector(int s):SimpleVector<T>(s) {} SearchableVector(SearchableVector&); SearchableVector(SimpleVector<T>& obj): SimpleVector<T>(obj) {} int findItem(T); };

  7. Fini ?

More Related