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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#ifndef _ARRAY_H
#define _ARRAY_H
#include <vector>
#include <iostream>
template<class T, int n>
class Array
{
private:
T arr[n];
int len;
public:
Array();
Array(const Array & arr);
Array & operator=(const Array & a);
friend std::istream operator>>(std::istream is,const T & var);
T & operator[](int i) const;
void show();
};
template<class T, int n>
Array<T,n>::Array()
{
len = n;
for(int i = 0;i < len;i++)
arr[i] = (T)0;
}
template<class T, int n>
Array<T,n>::Array(const Array & arr)
{
if(len != arr.len)
std::cout << "arrays of different lengths, incompatable...nothing to be done..\n";
else{
for(int i = 0;i < len;i++)
arr[i] = arr.arr[i];
}
}
template<class T, int n>
Array<T,n> & Array<T,n>::operator=(const Array & a)
{
if(len != arr.len)
std::cout << "arrays of different lengths, incompatable...nothing to be done..\n";
else{
for(int i = 0;i < len;i++)
arr[i] = arr.arr[i];
}
}
template<class T,int n>
void Array<T,n>::show()
{
int counter = 0;
std::cout << "Contents of array: \n";
std::cout << "Length: " << len << std::endl;
std::cout << "Data: \n";
for(int i = 0; i < len;i++)
{
counter++;
std::cout << arr[i] << ",";
if(counter ==3){
std::cout << std::endl;
counter = 0;
}
}
}
template<class T, int n>
T & Array<T,n>::operator[](int i)
{
return arr[i];
}
template<class T, int n>
friend std::istream operator>>(std::istream is,const T & var)
{
//stuck here, no clue what to do!
}
#endif // _ARRAY_H
|