C++ Program to find Sum of Array using function template

By | 06.01.2017

Sum of Array using function template


Write a C++ Program to find Sum of Array using function template. Here’s a Simple C++ Program to find Sum of Array using function template in C++ Programming Language.


What are Templates in C++ ?


Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..


Function Template :

The general form of a template function definition is shown here:

template <class type> ret-type func-name(parameter list)

{
// body of function
}


Class Template :

Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:

template <class type> class class-name

{
.
.
.
}


Below is the source code for C++ Program to find Sum of Array using function template which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to find Sum of Array using function template  */

#include <iostream>

using namespace std;


template<class T> T sum(T a[], int length)
{
        T ret = a[0];
        for (int i = 1; i < length; i ++)
                ret += a[i];

        return ret;
}

int main(void) {
        int int_data[5];
        float float_data[5];
        int i = 0;

        // input 5 integers
        cout << "\nInput 5 integers :: \n" << endl;
        for (; i < 5; i++)
                cin >> int_data[i];
        // print sum of 5 integers
        cout << "\nSum of above is :: " << sum(int_data, 5) << endl;

        // input 5 float numbers
        cout << "\nInput 5 floats :: \n" << endl;
        for (i = 0; i < 5; i ++)
                cin >> float_data[i];
        // print sum of 5 float numbers
        cout << "\nSum of above is :: " << sum(float_data, 5) << endl;

        cin.get();
        return 0;
}

OUTPUT : :


/*  C++ Program to find Sum of Array using function template  */

Input 5 integers ::

1
2
3
4
5

Sum of above is :: 15

Input 5 floats ::

1.3
2.3
3.5
4.3
2.1

Sum of above is :: 13.5

Process returned 0

Above is the source code and output for C++ Program to find Sum of Array using function template which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

2.8 10 votes
Article Rating
Category: C++ Programming Template Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments