//Header file for Array.cpp.
#ifndef ARRAY_H
#define ARRAY_H
template<class Datatype>
class Array

	//Template for use with the Array's data type.
	//Member Variables.
	int size;
	int grow_size;
	int num_elements;
	Datatype* m_array;

	Array( int p_size)
		m_array = new Datatype[p_size];
		size = p_size;

		if( m_array!= 0 )
			delete[] m_array;
		m_array= 0;

	//Access Operator: Syntax to allow the use of editing.
	Datatype& operator[] ( int p_index)
		return m_array[p_index];

	void ArrayFunction( int* p_array)
	p_array[0] = 10;
	//Print Function: To print out all elemenst in the Array.
	void printArray()
		for(int i=0;i< size;i++)
			cout << "Position: " <<m_array[i]<<endl;


	//Resize Function: To resize the Array.
	void Resize( int p_size)
		Datatype* newarray = new Datatype[p_size];
		if( newarray== 0 )
		int min;
		if( p_size< size)
			min = p_size;
			min = size;
		int index;
		for( index = 0; index < min; index++ )
			newarray[index] = m_array[index];
		m_size= p_size;
		if( m_array!= 0 )
			delete[] m_array;
		m_array= newarray;

	//Remove Function: To remove an element in an Array.
	void Remove( int p_index)
		int index;
		for( index = p_index+ 1; index < size; index++ )
			m_array[index -1] = m_array[index];
	//Size Function: To get the size of the Array.
	int Size()
		return size;
	//Insert Function: To insert an element into the array between points in the index.
	void Insert( Datatype p_item, int p_index)
		int index;
		for( index = size-1; index > p_index; index—)
			m_array[index] = m_array[index -1];
		m_array[p_index] = p_item;



#include"Array.h"//To allow the use of Array.h Header file.
using namespace std;// To allow the use of the standard library.

int main()//Main method for CA1Arrays.
	//Creating an array.
	Array<int> intArray(10);
	//Filling the Array(intArray)with elements from 1-10.
	intArray[0] = 1;
	intArray[1] = 200;
	intArray[2] = 323;
	intArray[3] = 976;
	intArray[4] = 545;
	intArray[5] = 767;
	intArray[6] = 234;
	intArray[7] = 123;
	intArray[8] = 456;
	intArray[9] = 789;


Recommended Answers

All 2 Replies

You are not altering the size in remove.

Resize still refers to m_size

At line 95 you have index— but I think you mean index-- NOTE that is not a minus in what is currently in your source file.

Why do you have grow_size and num_elements members in your array-class, when you don't use them anywhere?

Your code mostly looks fine, just give some thought to what size means and whether you can trust it. Also, what does your Insert() method do if the array is already full?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.