hey guys I am trying to code a template for my main program but I am having a difficult time doing it.
I am make a template called Array that manages a 1 dimensional array of contiguously stored lists of objects of type E. The object by default are doubles.
When the object is being constructed the Array object receives the number of type E objects in the array. My program also contains these two functions:
* unsigned int size() const - an inline query that returns the number of elements in the array.
* E& operator[](int i) - an inline subscript operator that returns a reference to element i, where the first element is at index 0. If the value of i is outside array bounds, your operator returns a reference to a dummy element of type E.
I think my main problem is when the constructor is being called, but I am not exactly sure. I have included the copy constructor and assignment operator but I haven't coded them yet. If anyone could just help me get on track with this I would really appreciate it.
Main program:
Code:
#include <iostream>
using namespace std;
#include "Array.h"
int main ( ) {
Array<int> x(3);
for (int i = 0; i < 3; i++)
x[i] = 9 - i;
x[-1] = 99;
for (int i = 0; i < 6; i++)
cout << x[i] << endl;
Array<> y(2);
y[0] = 2.1;
y[1] = 1.1;
for (int i = 0; i < 2; i++)
cout << y[i] << endl;
return 0;
}
template:
Code:
template <class Array = double,int size=50>
class E{
E a_[size];
int numb_;
public:
E(int i){
numb_=i;
}
unsigned int size() const{
return numb_;
}
E& operator[](int i){
if (i > numb_ - 1 && i < size)
numb_ = i + 1;
else
i = 0;
return a[i];
}
~E(){
delete [] numb_;
}
E(const E& original){
}
const E& operator =(const E& org){
}
};