0% found this document useful (0 votes)
6 views3 pages

C++ Dynamic Array Class Implementation

This document presents a C++ template class named 'Array' that implements a dynamic array with functionalities such as appending, inserting, removing, deleting, retrieving elements, reversing the array, searching for an element, and displaying the contents. It includes a constructor for initialization and a destructor for memory management. An example usage of the class is provided in the main function, demonstrating its capabilities.

Uploaded by

tabacc211
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

C++ Dynamic Array Class Implementation

This document presents a C++ template class named 'Array' that implements a dynamic array with functionalities such as appending, inserting, removing, deleting, retrieving elements, reversing the array, searching for an element, and displaying the contents. It includes a constructor for initialization and a destructor for memory management. An example usage of the class is provided in the main function, demonstrating its capabilities.

Uploaded by

tabacc211
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;

template <class T>


class Array {
private:
T* arr;
int size;
int capacity;

public:
// Constructor
Array(int cap = 10) {
capacity = cap;
size = 0;
arr = new T[capacity];
}

// Destructor
~Array() {
delete[] arr;
}

// Append: Add element at the end


void Append(T element) {
if (size == capacity) {
cout << "Array is full!" << endl;
return;
}
arr[size++] = element;
}

// Insert: Add element at specific location


void Insert(int index, T element) {
if (index < 0 || index > size) {
cout << "Invalid index!" << endl;
return;
}
if (size == capacity) {
cout << "Array is full!" << endl;
return;
}
for (int i = size; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = element;
size++;
}

// Remove: Remove element from end


void Remove() {
if (size == 0) {
cout << "Array is empty!" << endl;
return;
}
size--;
}

// Delete: Delete element at specific location


void Delete(int index) {
if (index < 0 || index >= size) {
cout << "Invalid index!" << endl;
return;
}
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}

// Get: Return element at specific index


T Get(int index) const {
if (index < 0 || index >= size) {
cout << "Invalid index!" << endl;
return T(); // return default value
}
return arr[index];
}

// GetSize: Return current size


int GetSize() const {
return size;
}

// Reverse: Reverse array elements


void Reverse() {
for (int i = 0; i < size / 2; i++) {
T temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}

// Search: Return index of element (or -1 if not found)


int Search(T element) const {
for (int i = 0; i < size; i++) {
if (arr[i] == element)
return i;
}
return -1;
}

// Display: Print all elements


void Display() const {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};

// ------------------- Example usage -------------------


int main() {
Array<int> a(5);
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link](1, 15);
[Link]();

[Link](2);
[Link]();

cout << "Element at index 1: " << [Link](1) << endl;


cout << "Index of 30: " << [Link](30) << endl;

[Link]();
[Link]();

cout << "Size: " << [Link]() << endl;

return 0;
}

You might also like