Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.1 kB
1
Indexable
Never
#ifndef NUMBERS_H
#define NUMBERS_H

#include <iostream>

template<typename T, std::size_t N>
class Numbers {
public:
    Numbers(const T (&arr)[N]);
    void showNumbers() const;

private:
    T elements[N];
};

#include "Numbers.cpp"

#endif // NUMBERS_H


#include "Numbers.h"

template<typename T, std::size_t N>
Numbers<T, N>::Numbers(const T (&arr)[N]) {
    for(std::size_t i = 0; i < N; ++i) {
        elements[i] = arr[i];
    }
}

template<typename T, std::size_t N>
void Numbers<T, N>::showNumbers() const {
    for(std::size_t i = 0; i < N; ++i) {
        std::cout << elements[i] << " ";
    }
    std::cout << std::endl;
}

// Explicit instantiation to avoid linker errors
template class Numbers<int, 5>;
template class Numbers<double, 5>;
#include <iostream>
#include "Numbers.h"

int main() {
    int intArr[5] = {1, 2, 3, 4, 5};
    double doubleArr[5] = {1.1, 2.2, 3.3, 4.4, 5.5};

    Numbers<int, 5> intNumbers(intArr);
    Numbers<double, 5> doubleNumbers(doubleArr);

    std::cout << "Integer array: ";
    intNumbers.showNumbers();

    std::cout << "Double array: ";
    doubleNumbers.showNumbers();

    return 0;
}

Leave a Comment