ADVERTISEMENT
ADVERTISEMENT

Example of Template in C++ Programming Language - Declaration and Use.


IncludeHelp 09 June 2016

This code snippet will declare and access the template. In this example I am going to explain you how to declare and use of a Template in C++.

C++ Code Snippet - Declaration and Use of a Template

/*Example of Template in C++*/

#include <iostream>

using namespace std;

template <class T>

T sum(T a,T b)
{
	T s;
	
	s=a+b;
	
	return s;	
}

int main()
{
	cout<<"Sum of two integers: "<<sum(10,20)<<endl;
	cout<<"Sum of two float: "<<sum(1234.f,34.445f)<<endl;
	cout<<"Sum of two doubles: "<<sum(10.23,20.45)<<endl;
	
	return 0;
}
    
    Sum of two integers: 30
    Sum of two float: 1268.44
    Sum of two doubles: 30.68

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.