×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Templates | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Templates (set 1).
Submitted by Nidhi, on September 03, 2020

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;
}

Output:

main.cpp:4:11: error: ‘Type’ has not been declared
 template <Type>
           ^~~~
main.cpp:5:1: error: ‘Type’ does not name a type
 Type getLarge(Type A, Type B)
 ^~~~
main.cpp: In function ‘int main()’:
main.cpp:12:13: error: ‘getLarge’ was not declared in this scope
     cout << getLarge<char>('K', 'L') << endl;
             ^~~~~~~~
main.cpp:12:22: error: expected primary-expression before ‘char’
     cout << getLarge<char>('K', 'L') << endl;
                      ^~~~
main.cpp:13:22: error: expected primary-expression before ‘int’
     cout << getLarge<int>(25, 38) << endl;
                      ^~~
main.cpp:14:22: error: expected primary-expression before ‘float’
     cout << getLarge<float>(3.14F, 22.5F) << endl;
                      ^~~~~

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.

template <typename Type> 
Type getLarge(Type  A, Type B) 
{ 
	return (A > B)? A: B; 
}  

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;
}

Output:

120

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.

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;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:17:5: error: ‘Type’ was not declared in this scope
     Type fact = 0;
     ^~~~
main.cpp:17:10: error: expected primary-expression before ‘int’
     Type fact = 0;
          ^~~
main.cpp:19:5: error: ‘fact’ was not declared in this scope
     fact = fun(5);
     ^~~~

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.

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;
}

Output:

17

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.

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;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:22:29: error: no matching function for call to ‘fun(int [8], int)’
     large = fun(arr, 8);
                             ^
main.cpp:5:6: note: candidate: template Type fun(Type*, int)
 Type fun(Type num[], int size)
      ^~~
main.cpp:5:6: note:   template argument deduction/substitution failed:
main.cpp:22:29: note:   cannot convert ‘arr’ (type ‘int [8]’) to type ‘char*’
     large = fun(arr, 8);
                             ^

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.