Q:

C++ template program with arrays

belongs to collection: C++ programs on various topics

0

C++ template program with arrays

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program:

// C++ template program with arrays

#include <iostream>
#include <string.h>
using namespace std;

template <class TypeName>

// function using the template
void printArray(TypeName* arr, int len)
{
    for (int i = 0; i < len; i++) {
        cout << *arr << " ";
        ++arr; // pointing to next element
    }
    cout << endl;
}

// main code/ main function
int main()
{
    // declaring character array
    char chrArr[] = "IncludeHelp";
    //declaring integer array
    int numArr[] = { 10, 20, 30, 40, 50 };

    //printing array elements
    cout << "chrArr: ";
    printArray(chrArr, strlen(chrArr));

    cout << "numArr: ";
    printArray(numArr, 5);

    //passing direct string
    cout << "Hello: ";
    printArray("Hello", 5);

    return 0;
}

Output

 
chrArr: I n c l u d e H e l p
numArr: 10 20 30 40 50
Hello: H e l l o

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ | Define a class method outside the class defi... >>
<< C++ program | Different ways to print array elemen...