Homework # 1(Due Date: Sunday July 3 by midnight)

Submission:
Submit your homework via E-learning

Deliverables:
Select your 2 code files (main.cpp and Array.h) and compress them into a file named StudentID_FirstName.zip
Note: use Winrar to compress your files.

Array Class
Write your own code to implement an Array class that represent a 1D array of elements, the variable size holds the size of the array.

#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
using namespace std;

template <class Type>
class Array
{
public:
	Array (int s);
	Array (const Array& obj);
	~Array ();

	const Array& operator= (const Array& obj);

	Type GetElement (int index) const;
	void SetElement (Type ele, int index);

	void Resize (int newSize);

	void Print () const;
	void Destroy ();

private:
	int size;
	Type* elements;
};

...			// functions definitions here 

#endif
		
  1. [1 point]
    Array (int s); // constructor

    Constructor sets size to s and allocates memory for elements

    Note: size must be greater than zero
  2. [1 point]
    Array (const Array& obj); // copy constructor

    Deep copy (not shallow copy) because the object has dynamic memory
  3. [1 point]
    ~Array (); // destructor

    Destructor deallocates elements memory and sets it to NULL, also sets size to 0.
    // same as Destroy ();
  4. [1 point]
    const Array& operator= (const Array& obj); // assignment operator overloading

    Deep copy (not shallow copy) because the object has dynamic memory
  5. [1 point]
    Type GetElement (int index) const;

    If index less than zero or index greater than or equal to size this operation can not be proceeded, otherwise

    Returns the elements at position index.
  6. [1 point]
    void SetElement (Type ele, int index);

    If index less than zero or index greater than or equal to size this operation can not be proceeded, otherwise

    Sets the element at position index to ele
  7. [1 point]
    void Print () const;

    Prints all elements in the array
  8. [1 point]
    void Destroy ();

    Deallocates elements memory and sets it to NULL, also sets size to 0.
  9. [2 point]
    void Resize (int newSize);

    Resize changes the size of the array by making it smaller or bigger. After the operation is done size = newSize
    if newSize <= 0, operation can not be proceeded
    if newSize == size, nothing happens
    if newSize < size; elements at positions [newSize, size) will no longer exist
    if newSize > size; elements at positions [size, newSize) will be added to the array
  10. In main.cpp, test all the functionalities of Array class.

Here's a sample main function and its output

#include <string>
using namespace std;

#include "Array.h"

void main ()
{
	Array<string> strArray (5);

	strArray.SetElement ("First", 0);
	strArray.SetElement ("second", 1);
	strArray.SetElement ("third", 2);
	strArray.SetElement ("fourth", 3);
	strArray.SetElement ("fifth", 4);

	strArray.Print ();

	cout<<"Resize array to become of size 3\n\n";
	strArray.Resize (3);
	strArray.Print ();

	cout<<"Resize array to become of size 6\n\n";
	strArray.Resize (6);
	strArray.Print ();
	
	Array strArray2 (strArray); // copy constructor
	cout<<"Elements of strArray2:\n";
	strArray2.Print ();

	strArray2.SetElement ("Yes", 3);
	strArray2.SetElement ("No", 5);

	strArray = strArray2;	// assignment operator overloading

	cout<<"Elements of strArray:\n";
	strArray.Print ();

	strArray.Destroy ();
	cout<<"Elements of strArray:\n";
	strArray.Print ();
}