PDF4PRO ⚡AMP

Modern search engine that looking for books and documents around the web

Example: marketing

A C++ DYNAMIC ARRAY

Back to document page

A C++ DYNAMIC ARRAY C++ does not have a DYNAMIC ARRAY inbuilt, although it does have a template in the Standard Template Library called vector which does the same thing. Here we define a DYNAMIC ARRAY as a class, first to store integers only, and then as a template to store values of any type. First we define the required functions and operations: class Dynarray { private: int *pa; // points to the ARRAY int length; // the # elements int nextIndex; // the next highest index value public: Dynarray(); // the constructor ~Dynarray(); // the destructor int& operator[](int index); // the indexing operation void add(int val); // add a new value to the end int size(); // return length }; The class declares an integer pointer, pa, that will point to the ARRAY itself.

When a object of type Dynarray is created on the stack, as it will be by the above declaration, then care must be taken to clean-up any memory allocation when the object is destroyed (when its activation record is popped off the execution stack). This avoids memory leakage. The memory is recovered for re-use by using delete.

  Stack

Download A C++ DYNAMIC ARRAY


Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Spam in document Broken preview Other abuse

Related search queries