Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Types of templates in C++

 Templeate in C++



  • Generic programming, which entails writing code in a style that is independent of any specific type, is built on templates.
  • A template is a model or recipe for developing a universal class or function. Generic programming is demonstrated through library containers like iterators and algorithms, which were created utilising the template notion.
  • Each container has a unique definition, however there are numerous alternative types of vectors that can be defined, such as vectors that are int or strings.
  • Both functions and classes can be defined using templates; let's look at how they operate.

  • Types of Templates

  • function Templates

  • Class Templates

  • Variadic Templates.

  • Function Templates:-

  • The following syntax of function templets are
  • syntax:-

  • template ret-type func-name(parameter list)
  •  {
  •    // body of function

  • Here, the term "type" denotes a data type that the function will employ. Within the function definition, this name may be utilised.

  • The function template that returns a maximum of two values looks like the following:
  • example code :-

  • #include
  • #include

  • using namespace std;

  • template
  • inline T const& Max (T const& a, T const& b)
  •  { 
  •    return a
  • }

  • int main () {
  •    int i = 15;
  •    int j = 10;
  •    cout

  •    double f1 = 10.5; 
  •    double f2 = 8.7; 
  •    cout

  •    string s1 = "Hello"; 
  •    string s2 = "ramana"; 
  •    cout

  •    return 0;
  • }


  • Class Template:- 

  • Class templates can be defined in the same way that function templates can. This is a generic class declaration in its general form.
  • syntax 

  • template class class-name {
  •    .
  •    .
  • statements ;   .
  • }

  • Here, type is a placeholder type name for the actual type, which will be determined when a class is created. By using commas to separate your list of definitions, you can provide multiple generic data types.

  • The example for creating the class Stack and implementing generic methods to push and pop elements from the stack is given below.
  • example code :-
    • #include
    • #include
    • #include
    • #include
    • #include

    • using namespace std;

    • template
    • class Stack 
    •    private: 
    •       vector elems;    // elements 

    •    public: 
    •       void push(T const&);  // push element 
    •       void pop();               // pop element 
    •       T top() const;            // return top element 
    •       
    •       bool empty() const {      // return true if empty.
    •          return elems.empty(); 
    •       } 
    • }; 

    • template
    • void Stack::push (T const& elem) { 
    •    // append copy of passed element 
    •    elems.push_back(elem);    

    • template
    • void Stack::pop () { 
    •    if (elems.empty()) { 
    •       throw out_of_range("Stack::pop(): empty stack"); 
    •    }
    •    
    •    // remove last element 
    •    elems.pop_back();         

    • template
    • T Stack::top () const { 
    •    if (elems.empty()) { 
    •       throw out_of_range("Stack::top(): empty stack"); 
    •    }
    •    
    •    // return copy of last element 
    •    return elems.back();      

    • int main() { 
    •    try {
    •       Stack         intStack;  // stack of ints 
    •       Stack stringStack;    // stack of strings 

    •       // manipulate int stack 
    •       intStack.push(7); 
    •       cout

    •       // manipulate string stack 
    •       stringStack.push("hello"); 
    •       cout
    •       stringStack.pop(); 
    •       stringStack.pop(); 
    •    } catch (exception const& ex) { 
    •       cerr
    •       return -1;
    •    } 

    • PREPROCESSOR :

    • The preprocessors are the directives that tell the compiler how to preprocess the data before the real compilation process begins.


      Only characters that are white space may come before a preprocessor directive on a line; all preprocessor directives start with #. Preprocessor directives do not terminate in a semicolon because they are not C++ statements (;).


      A #include directive has already been used in each case. Using this macro, a header file can be added to the source file.


      C++ supports a number of preprocessor directives, including #include, #define, #if, #else, and #line. Let's look at some crucial instructions.

  • #define Preprocessor 

  • Symbolic constants are produced by the #define preprocessor directive. The usual form of the directive is, and the symbolic constant is referred to as a macro.
  • syntax :

  • #define macro-name replacement-text 

  • example code :-
  • #include
  • using namespace std;

  • #define PI 3.14159

  • int main () {
  •    cout

  •    return 0;
  • }



  • This post first appeared on Anyupdate, please read the originial post: here

    Share the post

    Types of templates in C++

    ×

    Subscribe to Anyupdate

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×