1 / 11

Class Template

Class Template. A class template is a pattern for the compiler to use to build a class definition. Since the compiler generates the class definition the template must be made visible. This is done by including the implementation file in the header file. Class Template.

parley
Download Presentation

Class Template

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. Class Template A class template is a pattern for the compiler to use to build a class definition. Since the compiler generates the class definition the template must be made visible. This is done by including the implementation file in the header file.

  2. Class Template template <class Item> class myClass { // class members };

  3. Class Template template <class T> class myClass { public: myClass(); void setData(const T&) T getData() const; private: T myData; };

  4. Class Template template <class T> void myClass<T>::setData(const T& inData) { myData = inData; } template <class T> T myClass<T>::getData() { return myData; }

  5. Class Template A class template is a model for the compiler to use to build a class definition. Since the compiler generates the class definition the template must be made visible. This is done by including the implementation file in the header file.

  6. Class Template (myClass.h) template <class Item> class myClass { // class member }; #include “myClass.cpp”

  7. Class Template myClass<int> anInt; myClass<float> aFloat; myClass<CString> aCString; anInt.setData(2); cout << anInt.getData() << endl; aFloat.setData(3.14); cout << aFloat.getData() << endl; aCString.setData(“test string”); cout << aCString.getData() << endl;

  8. #include <iostream.h> #include "afx.h" // for MFC CString #include "myClass.h” int main() { myClass<int> anInt; myClass<double> aFloat; myClass<CString> aCString; anInt.setData(2); cout << anInt.getData() << endl; aFloat.setData(3.14); cout << aFloat.getData() << endl; aCString.setData("test string"); cout << aCString.getData() << endl; return 0; }

  9. #include <iostream.h> #include "afx.h" // for MFC CString #include "myClass.h” int main() { myClass<int> anInt; myClass<double> aFloat; myClass<CString> aCString; anInt.setData(2); cout << anInt.getData() << endl; aFloat.setData(3.14); cout << aFloat.getData() << endl; aCString.setData("test string"); cout << aCString.getData() << endl; return 0; }

  10. /* Screen capture of output 2 3.14 test string Press any key to continue */

  11. Class Template We can now use the template to generalize the List ADT.

More Related