Dynamic array

What is dynamic array using C++?

As the programmers, all of you may known what is the array mean? The array is the linear data structure which is stored the elements as the collection.​ Dynamically arrays are allocated on the heap at run time which it is allow the array collection to be pointed with operator new. As the example below:

ptr = new int[size];

Simple Dynamic Array in main program in C++


#include<iostream>
using namespace std;
int main()
{
//------dynamic array------ using pointer
	int *ptr;
	int size;
	cout << "Please enter the amount of numbers in the array: " << endl;
	cin >> size; //number of the size = 10
	ptr = new int[size]; //inherite/accept value
	//store value of the array 
	for (int i = 0; i < size; i++)
	{
		cout << "-element: ";
		cin >> ptr[i];
	}
	//access to the element of the array collection
	cout << "The elements in the array are:";
	for (int i = 0; i < size; i++)
	{
		cout <<" "<< ptr[i];
	}
	cout << endl;
	system("pause==0");
}

Dynamic Array By Create Class with Function

Class with Function of Dynamic Array:

// Create Class array
class myarr {
private:
	int *ptr;
	int size;
public:
	void dArray()
	{
		cout << "Please enter the amount of numbers in the array: " << endl;
		cin >> size; //number of the size = 10
		ptr = new int[size]; //inherite/accept value
							 //store value of the array 
		for (int i = 0; i < size; i++)
		{
			cout << "-element: ";
			cin >> ptr[i];
		}
		//access to the element of the array collection
		cout << "The elements in the array are:";
		for (int i = 0; i < size; i++)
		{
			cout << " " << ptr[i];
		}
	}
};

Using in Main program in C++

int main()
{
	myarr dynamicArray; // new name inherited from class
	dynamicArray.dArray(); // call function dArray in the class
}