Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Karl Hou
- // lab 5a, The "MyStaticArray" program
- #ifndef MyStaticArray_H
- #define MyStaticArray_H
- template <typename V, int CAPACITY>
- class Array{
- public:
- Array();
- inline V& operator[](int);// Setter function, use const if you want a getter
- int getCapacity() const; // Returns max data size
- int scan(const V&) const; // Search for a data value
- private:
- V data[CAPACITY];
- int index;
- V dummy;
- };
- template<typename V, int CAPACITY> // Default Constructor
- Array<V, CAPACITY>::Array() {
- }
- template<typename V, int CAPACITY> //Setter
- inline V& Array<V, CAPACITY>::operator[](int index) {
- if (index < 0)
- return dummy;
- if (index >= CAPACITY)
- return dummy;
- return data[index];
- }
- template<typename V, int CAPACITY> // Capacity returner
- int Array<V, CAPACITY>::getCapacity() const {
- return CAPACITY;
- }
- template<typename V, int CAPACITY> // A search function, returns -1 if not found.
- int Array<V, CAPACITY>::scan(const V& arg) const {
- for (int i = 0; i < CAPACITY; i++) {
- if(arg == data[i]) {
- return i;
- }
- }
- return -1;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement