Template Programming Exercise 2 in C++

Template Programming are the base of generic programming. The main advantage of template is that we don’t have to create different functions for different data types. Datatype is passed as a parameter in template. “template” keyword is used to define a template.

If you want to practice with Functions Programming Exercises kindly read the following article.

Syntax:

template <class type> return_type fun_name(parameters)
{
   // function defination
} 

If you want to practice with Arrays kindly read the following article.

Question Number 2

Write a function template that accepts three parameters and displays them in reverse order.

include<iostream>
using namespace std;

template <class Type>
void Reverse(Type a, Type b, Type c)
{
	cout<<"The values in reverse order: ";
	cout<<c<<" "<<b<<" "<<a<<endl;
}
 main()
 {
 	int x, y, z;
 	float d, e, f;
 	cout<<"Enter three integers: ";
 	cin>>x>>y>>z;
 	cout<<"Enter three floating point numbers: ";
 	cin>>d>>e>>f;
 	Reverse(d, e, f);
 }

If you want to practice with OOP Programming Exercises kindly read the following article.

Leave a Reply