Templates in C++ - A Guide for Generic Programming

templates in c n.w
1 / 17
Embed
Share

"Learn about templates in C++, a powerful tool for generic programming that allows code reusability, time-saving, and flexibility. Understand the types of templates, such as class templates and function templates, with examples and insights into creating template classes with multiple parameters."

  • C++
  • Templates
  • Generic Programming
  • Code Reusability
  • Flexibility

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. TEMPLATES IN C++ K.THAMIZHSELVI Asst. Prof. of Computer Science Bon Secours college for Women, Thanjavur

  2. TEMPLATES Template is a new concept allow the function or class to work on more than one data type at once without writing different codes for different data types. The parameters used during its definition is of generic type and can be replaced later by actual parameter. This is called the concept of generic programming The templates are also called as parameterized classes or functions.

  3. Purpose of Templates Used in large programs Code reusability Time saving Flexibility of program Used to create a family of classes or functions.

  4. Types of Templates 2 types: Class Template Function Template

  5. Class Templates A Class Template can represent various similar classes operating on different data types. Syntax: template < class T1, class T2, > class classname { functions; };

  6. Eg: template <class T> class vector { T* v; int size; public: vector (int m) { v = new T [size = m]; for ( int i = 0; i <size; i++) v[i] = 0; } vector ( T* a) { for ( int i = 0; i <size; i++) v[i] = a[i]; } T operator* (vector &y) { T sum = 0; for ( int i = 0; i <size; i++) sum + = this -> v[i] * y . V [i]; return sum; } };

  7. Template class A class created from a class template is called a Template class. classname < type> objectname (arglist); The process of creating a specific class from class template is called Instantiation.

  8. Class Templates with Multiple Parameters More than one generic data types can be used in a class template. They can be declared by comma separated list within the template specification. template < class T1, class T2, ..> class classname { body of the class };

  9. Example: template <class T1, class T2> Class Test { T1 a; T2 b; public : Test (T1 x, T2 y) { a = x; b = y; } void show() { cout<< a << and << b << \n ; } };

  10. int main() { cout<< Instantiating the class template test1: ; Test < float, int > test1 (1.23, 123); test1.show(); cout<< Instantiating the class template test2: ; Test < int, char > test2 (100, w ); test2.show(); return 0; }

  11. Output for the Program Instantiating the class template test1: 1.23 and 123 Instantiating the class template test2: 100 and w

  12. Function Templates Function templates used to create a family of functions with different argument types. Syntax: Template < class T> returntype functionname (arguments of type T) { body of the function }

  13. Example template < class T> void swap ( T &x, T &y) { T temp = x; x = y; y = temp; }

  14. Function Template With Multiple Parameters Syntax: template < class T1, class T2, .> returntype functionname(arguments of types T1,T2,..) { body of the function }

  15. Example: template < class T1, class T2> void display ( T1 x, T2 y) { cout << x << y << \n ; } int main() { cout << calling function template with integer and character string type parameters \n ; display( 2000, ECG ); cout << calling function template with float and integer type parameters \n ; display( 2.12, 212); return 0; }

  16. Output calling function template with integer and character string type parameters 2000 ECG calling function template with integer and character string type parameters 2.12 212

More Related Content