I am a mathematician trying to learn how to become a better programmer in C++. I am currently now trying to write the main data structures from scratch. I have now completed a generic array class and I wanted to see if I was missing anything or if there is anything I can improve on.
Here is the header file:
#ifndef Array_h
#define Array_h
#include <iostream>
template <class T>
class Array {
private:
int maxSize; // capacity of array
int length;
T *array;
public:
Array(int size = 100);
~Array();
void print() const;
bool isEmpty() const;
bool isFull() const;
int maxListSize() const;
int listSize() const;
T front() const;
T back() const;
void swap(T& x, T& y);
void sort();
int search(const T& item);
void insert(int location, const T& item);
void insertAtTop(const T& item);
void deleteItem(const T& item);
void operator=(const T& temp);
T& operator[](int index) { return array[index];}
};
template <class T>
Array<T>::Array(int size) {
if (size <= 0) {
std::cout << "Size cannot be less than 0";
maxSize = 100;
array = new T[maxSize];
}
else {
maxSize = size;
length = 0;
array = new T[maxSize];
}
}
template <class T>
Array<T>::~Array() {
delete[] array;
}
template <class T>
void Array<T>::operator=(const T &temp) {
for(int i = 0; i < length; i++) {
array[i] = temp;
}
}
template <class T>
void Array<T>::print() const {
for(int i = 0; i < length; i++) {
std::cout << array[i] << "\t";
}
}
template <class T>
bool Array<T>::isEmpty() const {
return (length == 0);
}
template <class T>
bool Array<T>::isFull() const {
return (length == maxSize);
}
template <class T>
int Array<T>::maxListSize() const {
return maxSize;
}
template <class T>
int Array<T>::listSize() const {
return length;
}
template <class T>
T Array<T>::front() const {
return array[0];
}
template <class T>
T Array<T>::back() const {
return array[length-1];
}
template <class T>
void Array<T>::swap(T& x, T& y) {
T temp = x;
x = y;
y = temp;
}
template <class T>
void Array<T>::sort() {
for(int i = 0; i < length - 1; i++) {
for(int j = 1; j < length - i - 1; j++) {
if(array[j] > array[j-1]) {
std::swap(array[j], array[j-1]);
}
}
}
}
template <class T>
int Array<T>::search(const T& item) {
for(int i = 0; i < length; i++) {
if(array[i] == item){
return i;
}
else {
return 0;
}
}
}
template <class T>
void Array<T>::insert(int location, const T& item) {
if(length != maxSize) {
int i = length;
for(;i > location; i--) {
array[i] = array[i-1];
}
array[i] = item;
length++;
}
}
template <class T>
void Array<T>::insertAtTop(const T& item) {
if(length != maxSize) {
array[length] = item;
length++;
}
}
template <class T>
void Array<T>::deleteItem(const T& item) {
bool flag = false;
int location = 0;
for(;location < length; location++) {
if(array[location] == item) {
flag = true;
break;
}
}
if(flag) {
for(int i = location; i < length - 1; i++) {
array[i] = array[i+1];
}
length--;
}
else {
std::cout << "Element not found" << std::endl;
}
}
#endif /* Array_h */
Here is the main file that tests some of the member functions (I have checked all and they work).
#include <iostream>
#include "Array.h"
int main(int argc, const char * argv[]) {
Array<int> arr(5);
arr.insertAtTop(2);
arr.insertAtTop(4);
arr.insertAtTop(6);
arr.insertAtTop(8);
std::cout<<"\n------------------------------------------------------------\n";
std::cout<<"---------------Displaying All contents of array---------------";
std::cout<<"\n------------------------------------------------------------\n";
arr.print();
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"-------------Inserting At Particular--------------";
std::cout<<"\n--------------------------------------------------\n";
arr.insert(1, 12);
arr.print();
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"----------------Deleting an Item-----------------";
std::cout<<"\n--------------------------------------------------\n";
arr.deleteItem(12);
arr.print();
return 0;
}