70 likes | 201 Views
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
E N D
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 • Template prefix placed before class declaration
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
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)
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); };
Fini ?