Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a C++ code which can add the value of two arrays with the following requi

ID: 665877 • Letter: D

Question

Design a C++ code which can add the value of two arrays with the following requirements:

Redesign your Array class from lab6 as a class template to work with the application below. Due to the g++ compiler. your class declaration and class method definitions must both reside in the header file, Array. h Do not create a separate Array. C file for your class template In addition, write your overloaded output stream operator as an inline friend method in the class declaration Include the class template header file in your application as below. The output for this program should be as follows:

Explanation / Answer

working c++ code:

#include<iostream>
#include<vector>
template<typename T>
struct adder
{
   std::vector<T> items;
   adder(const T &item) { items.push_back(item); }
   adder& operator,(const T & item) { items.push_back(item); return *this; }
};

template <class Type, size_t N>
class Array
{
public:

    Array(const adder<Type> & init)
    {
         for ( size_t i = 0 ; i < N ; i++ )
         {
               if ( i < init.items.size() )
                   m_Array[i] = init.items[i];
         }
    }
    size_t Size() const { return N; }
    Type & operator[](size_t i) { return m_Array[i]; }
    const Type & operator[](size_t i) const { return m_Array[i]; }

private:

    Type m_Array[N];
};

int main() {

        Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}