0% found this document useful (0 votes)
48 views

Lecture 11

This document discusses function templates and class templates in C++. It provides an example of a function template called larger that can accept different data types. It then discusses defining a class template called listType that implements a generic list as a class that can hold different data types. It also shows how to define the member functions of a class template.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lecture 11

This document discusses function templates and class templates in C++. It provides an example of a function template called larger that can accept different data types. It then discusses defining a class template called listType that implements a generic list as a class that can hold different data types. It also shows how to define the member functions of a class template.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Function Templates

#include <iostream> #include <string> using namespace std; template <class Type> Type larger(Type x, Type y) int main() { cout << Larger of 5 and 6 = << larger(5, 6) << endl; cout << Larger of A and B = << larger(A, B) << endl; cout << Larger of 5.6 and 3.2 = << larger(5.6, 3.2) << endl; string str1 = Hello; string str2 = Happy; cout << Larger of << str1 << and << str2 << = << larger(str1, str2) << endl; return 0; } template <class Type> Type larger (Type x, Type y) { if (x >=y) return x; The type of data. Dont worry about what the word class means. This is a function template declaration. The function template is called larger. (Alternatively, we can have a class declaration.)

else return y; }

Revision on Classes
Let us define a class that performs various operation on a list. class intListType { public: bool isEmpty (); bool isFull (); int search (int searchItem); void insert (int newItem); void remove (int removeItem); void printList (); intListType (); private: int list [1000]; int length; }; Following this we can now define the member functions of class intListType. bool intListType::isEmpty () { } bool intListType::isFull () Returns the position in the list.

Class Templates
template <class elemType> class listType { public: bool isEmpty (); bool isFull (); void search (const elemType& searchItem, bool& found); void insert (const elemType& newElement); void remove (const elemType& removeElement); void destroyList (); void printList (); listType (); private: elemType list [100]; int length; }; Following this we can now define the member functions of class template listType. template<class elemType> bool listType<elemType>::isEmpty () {

} template<class elemType> bool listType<elemType>::isEmpty () { } To create a list of integers we use: listType<int> intList; To create a list of strings we use: listType<string> stringList;

Class Template for a List Implemented as an Array


This is similar to the above class template. template <class elemType> class arrayListType { public: const arrayListType<elemType>& operator= (const arrayListType<elemType>&); bool isEmpty () const; bool isFull () const; protected: }; Following this we can now define the member functions of class template arrayListType. Let us assume that one of the member functions performs a sequential search. We would define it as follows:

template <class elemType> int arrayListType<elemType>::seqSearch (const elemType& item) const { }

You might also like