Dalldorf Design Home Portfolio Pricing Services Resume About Tutorials
 
                                  Modifying Dynamic Class Arrays

 Source Files  Dynamic Class Arrays.cpp



In this tutorial you will learn how to create and change a dynamic class array. This code was written with Visual Studio 2003 and used the win32 Console application. I only included the .cpp file so you can download and run it in whatever program you use.

First you need to create the class. For this excersize we will call it inventory

class inventory
{
public:
inventory();                // constructor
void displayInv();       // Show the inventory that has been added
void addInventory();  // Will be used to input new inventory

string invName;         // Name of product in inventory
int invAmount;          // Amount of inventory of the product

private:

};


As you can see in the code there is a constructor, two functions, a string and an int. This program will be simple and is going to be used to add inventory using the class as an array.

Now lets set up the functions and constructor.

// constructor
inventory::inventory()
{
invName = "NO ENTRY";
invAmount = 0;
}

// Display Inventory Function
void inventory::displayInv()
{
   if (invAmount == 0) {}
   else
   {
   cout << invName << " " << invAmount << endl;
   }
}

// Add Inventory Function
void inventory::addInventory()
{
cout << "Name of the product ";
cin >> invName;
cout << "Amount of product ";
cin >> invAmount;
}


The rest of the code is below. I will explain in detail where the class arrays are created..

//Main Class
int main()
{
int count = 1;           // Set up a counter for the dynamic array
int checkCount = 1;  // checkCount is used to determine if the class arrays need to be swaped and a new element added
int currentCount = 0; // Set up a variable to keep track of which element of the array you are in

bool open = true; // Control for the main while loop

inventory *inv;           // pointer to the class (used to create dynamic class array
inventory *temp;        // temporary pointer that will be used when another element needs to be "added"
inv = new inventory[1]; // create memory for the class array

while (open)
{
system("cls"); // Clear the screen
// This is where the swapping takes place to add a new element
          if (checkCount < count)
          {
          temp = new inventory[checkCount]; // create memory for the temp class array
                    for (int i=0;i<checkCount;i++) // swap from inv into temp
                    {
                    temp[i].invName = inv[i].invName;
                    temp[i].invAmount = inv[i].invAmount;
                    }
          delete [] inv; // Free up the memory from inventory
          inv = new inventory[count]; // Create inv array with the extra element
                    for (i=0;i<checkCount;i++) // swap from temp back to inv
                    {
                    inv[i].invName = temp[i].invName;
                    inv[i].invAmount = temp[i].invAmount;
                    }
          delete [] temp; // Free the memory from temp
          checkCount = count;
          currentCount = count - 1;
          }

// DISPLAY CURRENT INVENTORY //
cout << "CURRENT INVENTORY" << endl;
cout << "---------------------" << endl;
for (int i=0;i<count;i++)
{
inv[i].displayInv();
}
cout << "---------------------" << endl << endl;
// END DISPLAY CURRENT INVENTORY

// CREATE MENU FOR USER TO INPUT DATA //
int menuSelection = 0;
cout << "--------------------------" << endl;
cout << "1. Add inventory item " << endl;
cout << "2. Exit " << endl;
cout << "--------------------------" << endl;
cout << "Select an option: ";
cin >> menuSelection;

switch (menuSelection)
{
  case 1:
      inv[currentCount].addInventory();
      count++;
      break;
  case 2:
      open = false;
      break;
  default:
      cout << "Please Select either 1 or 2 " << endl;
      break;
}

}

system("pause");
return 0;
}

Lets focus on the part of the code where we dynamically create the class array and where we add another element to inv.
 
inventory *inv;              // pointer to the class (used to create dynamic class array
inventory *temp;           // temporary pointer that will be used when another element needs to be "added"
inv = new inventory[1]; // create memory for the class array

while (open)
{
system("cls");                // Clear the screen
// This is where the swapping takes place to add a new element
          if (checkCount < count)
          {
          temp = new inventory[checkCount];   // create memory for the temp class array
                    for (int i=0;i<checkCount;i++)   // swap from inv into temp
                    {
                    temp[i].invName = inv[i].invName;
                    temp[i].invAmount = inv[i].invAmount;
                    }
          delete [] inv;                                    // Free up the memory from inventory
          inv = new inventory[count];              // Create inv array with the extra element
                    for (i=0;i<checkCount;i++)      // swap from temp back to inv
                    {
                    inv[i].invName = temp[i].invName;
                    inv[i].invAmount = temp[i].invAmount;
                    }
          delete [] temp;                                 // Free the memory from temp
          checkCount = count;
          currentCount = count - 1;
          }

 First at the top we declare the pointers inv and temp. We start out the program with inv having one element. while is called with inv = new inventory[1];. When the user wants to input another inventory item it checks to see if checkCount is less than count. If it is less that temp pointer is called in and memory is put aside for it. temp = new inventory[checkCount];. Once that is done you put all the data from inv array into temp array. Then delete the inv array to free up memory and call it again with count (which is one number greater than checkNumber). Then you fill inv with the data from temp and then delete the temp.I recommend downloading the .cpp file and running the program to understand what is exactly going on.

Copyright © 2007 Dalldorf Design. All Rights Reserved