Function Template

Sample Code

#include <iostream>  
using namespace std;
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;

}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}

Output

Class Template

Sample Code

#include <iostream>  

using namespace std;  

template<class T>  

class A   

{  

    public:  

    T num1 = 5;  

    T num2 = 6;  

    void add()  

    {  

        std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;  

    }  

      

};  

  

int main()  

{  

    A<int> d;  

    d.add();  

    return 0;  

}  

Output

Categorized in:

C++

Tagged in:

, , , , , , , , , , , , , , , ,

Share Article:

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock