This section contains the C++ find output programs with their explanations on C++ Templates (set 1).
Program 1:
#include <iostream>
using namespace std;
template <Type>
Type getLarge(Type A, Type B)
{
return (A > B) ? A : B;
}
int main()
{
cout << getLarge<char>('K', 'L') << endl;
cout << getLarge<int>(25, 38) << endl;
cout << getLarge<float>(3.14F, 22.5F) << endl;
return 0;
}
Program 2:
#include <iostream>
using namespace std;
template <typename Type>
Type fun(Type num)
{
int f = 1;
while (num > 0) {
f = f * num;
num--;
}
}
int main()
{
cout << fun<int>(5);
return 0;
}
Program 3:
#include <iostream>
using namespace std;
template <typename Type>
Type fun(Type num)
{
int f = 1;
while (num > 0) {
f = f * num;
num--;
}
}
int main()
{
Type<int> fact = 0;
fact = fun<int>(5);
cout << fact;
return 0;
}
Program 4:
#include <iostream>
using namespace std;
template <typename Type>
Type fun(Type num[], int size)
{
int large = 1;
large = num[0];
for (int i = 1; i < size; i++) {
if (large < num[i])
large = num[i];
}
return large;
}
int main()
{
int arr[] = { 1, 5, 2, 13, 15, 17, 0, 2 };
int large = 0;
large = fun<int>(arr, 8);
cout << large << endl;
return 0;
}
Program 5:
#include <iostream>
using namespace std;
template <typename Type>
Type fun(Type num[], int size)
{
int large = 1;
large = num[0];
for (int i = 1; i < size; i++) {
if (large < num[i])
large = num[i];
}
return large;
}
int main()
{
int arr[] = { 1, 5, 2, 13, 15, 17, 0, 2 };
int large = 0;
large = fun<char>(arr, 8);
cout << large << endl;
return 0;
}
Answer Program 1:
Output:
Explanation:
The above program will generate errors because the "typename" keyword is used to define the generic function. The correct way is given below to define the generic function.
Answer Program 2:
Output:
Explanation:
The above code will print "120" on the console screen. In the above program, we defined a generic function using templates with argument num. This function is used to calculate the factorial of the specified number.
Now coming to the main() function. Here we passed integer value in the function fun(). Then it will return 120 that will be printed on the console screen.
Answer Program 3:
Output:
Explanation:
The above code will generate errors because we declared variable fact using "Type", we cannot use "Type" in the main() function.
The "Type" is created using "typename" keyword for function fun(). That's why we can use Type with function fun() only.
Answer Program 4:
Output:
Explanation:
The above program will print "17" on the console screen.
In the above program, we created a generic function fun() with array num[] and size. The function fun() is created to find the largest element from the array.
Now coming to the main() function. Here we created the array of integers with 8 elements and created a local variable large. Then call function fun() with array "arr" and size 8.
The function fun() will return the largest element from the array that is 17. Then the final value of variable "large" is 17, which will be printed on the console screen.
Answer Program 5:
Output:
Explanation:
The above will generate a compilation error because of the below statement.
In this statement, we used char within angle bracket, but we passed an integer array and size. To resolve this problem we need to pass int instead of char.
need an explanation for this answer? contact us directly to get an explanation for this answer