1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
using namespace std;
template <typename Array1Type, typename Array2Type>
class Arrays
{
private:
//declaring array sizes and arrays as private attributes
const unsigned int c_size1;
const unsigned int c_size2;
Array1Type* MyArray1;
Array2Type* MyArray2;
public:
//constructor that initializes member variables
Arrays (const Array1Type* Array1, const unsigned int &size1, const Array2Type* Array2, const unsigned int &size2)
:c_size1(size1), c_size2(size2)
{
for (unsigned int i = 0; i < c_size1; ++i)
{
MyArray1 [i] = Array1 [i];
}
for (unsigned int i = 0; i < c_size2; ++i)
{
MyArray2 [i] = Array2 [i];
}
}
//accesor functions
Array1Type& GetElemArray1(int Index) {return MyArray1[Index];}
Array2Type& GetElemArray2 (int Index) {return MyArray2[Index];}
};
int main()
{
//declaring and defining first array
int IntArray[10] = {0,1,2,3,4,5,6,7,8,9};
//declaring and defining second array
char CharArray [10] = {'a', 'b','c','d', 'e','f','g','h','i','j'};
//constructing object TwoArrays of type Arrays
Arrays <int,char> TwoArrays (IntArray, 10, CharArray, 10);
//reading integer used to determine element position
cout <<"Insert the zero base index of the element you want to see from first array: ";
int Index = 0;
cin >> Index;
//writing the Index-element
cout << "The element IntArray[" <<Index<<"]= "<<TwoArrays.GetElemArray1(Index)<<endl;
cout <<"Insert the zero base index of the element you want to see from second array: ";
cin >> Index;
cout << "The element CharArray[" <<Index<<"]= "<<TwoArrays.GetElemArray2(Index)<<endl;
return 0;
}
|