C++ Preface to Template

Objectives :

  • Template, distinction between overloading and overwriting a function. 
  • What is Template ? C++ offers parametric polymorphism which allows generic algorithms and data structures via Template
    • Template class is also know as "parameterized-classes"
       
  • It is great when data structures could not be determined upfront or the data structures would  be overridden (overwriting) in an application,  overloading a function (meaning multiple version of a function) offers a great deal of flexibilities
    • Overriding in C++, begins with the term inheritance ( inheriting a base class), where subclasses would overwrite the output of a function belongs to a base class.
  • In the current example we would be overloading the argument data-types in a function.
    • The method name ( signature) remains same, but the arguments in each will be different.
      • void dosomething(){};
      • void dosomething(int a1, string str1){}
      • void dosomething(int a1, string str1, double sal){};
  • Code : myTemplateFunct1.txt

When we need template

  • Main reasons, but not absolute.
    • making Type safe collection class
      • If not specialized for any data type, default is void type, meaning would accept any data-type
    • Creating one generic class than creating specialized class or function
  • Difference between template and macro
    • macro has less scope type checking than template.
    • in macro compiler won't verify the parameter compatibilities.
  • Comparing void pointer to a function and template
    • with void pointer can't check on specific data-type, operator overloading and deploy constructor /destructors.
  • Creating smart pointer: here template allows to create a generic wrapper to encapsulate objects, and offers late binding to the objects .

Base Reference : http://en.wikipedia.org/wiki/Operator_overloading

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.

Through templates C++ language provides parametric polymorphism which allows generic algorithms and data structures. When an instance of a class is created , the templates ensures the correct executions of the routines encapsulated in a class.

Templates are compiled when required. Template works  like a run-time code optimizer, reduces run time over head and memory foot prints. 

No code is generated until a template is instantiated, and when required, the compilers  allow the inclusion of template-file in a project without generating linkage errors.

 

Step: 1 Create a project

 

 

Step: 2 Create a source file

Step: 3 Edit Source code

Step: 4 Runtime View