Given the following linked list class which works for integers, convert it
into a class template:
Class | Class Template |
---|---|
class LinkedList; class ListElement { int datum; ListElement* next; ListElement (int const&, ListElement*); friend LinkedList; }; class LinkedList { ListElement* head; public: LinkedList (); ~LinkedList (); bool IsEmpty () const; int const& First () const; int const& Last () const; void Prepend (int const&); void Append (int const&); void Extract (int const&); void Print (); }; |
template <class T> class LinkedList; template <class T> class ListElement { T datum; ListElement* next; ListElement (T const&, ListElement*); friend LinkedList <T>; }; template <class T> class LinkedList { ListElement<T>* head; public: LinkedList (); ~LinkedList (); bool IsEmpty () const; T const& First () const; T const& Last () const; void Prepend (T const&); void Append (T const&); void Extract (T const&); void Print (); }; |
Given the following function prototype and class outline:
template <class type> myfunc(type a, type b);
template <class type> class myarray { type a[10]; public: myarray() { for (int i; i<10; i++) a[i] } }Specify how to instantiate the following:
a. myfunc for integers | myfunc(10,20) //or any other integers |
b. myarray for doubles | myarray<double> a; |
c. myfunc for characters | myfunc('A','B') //or any other characters |
d. myarray for characters | myarray<char> a; |
float x=3.12;You may get a warning:
warning C4305: 'initializing' : truncation from 'double' to 'float'
To get rid of the warning and still use floating point, you can write:
float x=3.12F;In total, there are three floating point types:
|
|
|