Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Function Templates in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 134
    Comment on it

    Function Templates:-Templates are used to achieve the generic programming. By using function template the code will be independent to any data type.

    A template is used to create generic class or a function. By using function templates we have used the same function for different data types. The below Code will show you how to make the function templates.

    Template.cpp

    #include<iostream.h>
    #include<conio.h>
    template<class T>
    
    inline T add(T a,T b)
    {
        return (a+b);
    }
    
    void main()
    {
    clrscr();
    
    //Add Two Integer Numbers
    int a=2,b=3;
    cout<<"Integer:="<<add(a,b);
    
    //Add Two Floating Numbers
    float x=2.5f,y=3.2f;
    cout<<"\n\nFloat:="<<add(x,y);
    
    //Add Two Double Numbers
    double m=4.5,n=3.9;
    cout<<"\n\nDouble:="<<add(m,n);
    
    getch();
    }
    

    In above code we have define a function add as a function template and different data types int,float and double using the same function for addition.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: