/* Filename: example2.cpp Author: Br. David Carlson Date: March 18, 2017 This program shows two ways of creating and using an array of objects, static and dynamic. It also shows how to implement the related method of using an array of pointers to objects. */ #include "product2.h" const int MAX = 3; typedef Product ProductArrayType[MAX]; typedef ProductPtrType ProductPtrArrayType[MAX]; void TestStaticArrayOfObjects(void); void TestDynamicArrayOfObjects(void); void TestArrayOfObjectPointers(void); int main(void) { cout << "Start of main function" << endl; // Set up the formatting of the doubles so that we see 2 decimal places. cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); // First, we use a statically created array of objects. We use the entire array, but the code // could be modified to use a partially filled array. TestStaticArrayOfObjects(); // Second, we dynamically allocate an array of objects. In this one we ask the user // how long the array needs to be, but we could instead use a constant. TestDynamicArrayOfObjects(); // Third, we use a statically created array of pointers to objects. TestArrayOfObjectPointers(); cout << "End of main function" << endl; return 0; } /* Given: Nothing Task: To fill a statically-created array of objects with data read in from the user and then to print this data back out. Return: Nothing. */ void TestStaticArrayOfObjects(void) { cout << endl << "Start of function TestStaticArrayObjects, before declaring the array of objects" << endl; ProductArrayType ProductArray; // This is our array of objects (with default values to start with) string NameTemp, Junk; int YearTemp; double PriceTemp; // We ask the user for the data to put into each of the objects in ProductArray. cout << endl << "You will now be asked to enter data for the array of objects." << endl; for (int k = 0; k < MAX; k++) { cout << "Enter the name for product " << k << " : "; getline(cin, NameTemp); cout << "Enter the year for product " << k << " : "; cin >> YearTemp; cout << "Enter the price for product " << k << " : "; cin >> PriceTemp; getline(cin, Junk); // Clear out the input buffer. ProductArray[k].SetName(NameTemp); ProductArray[k].SetYear(YearTemp); ProductArray[k].SetPrice(PriceTemp); } // Now we print the data. cout << endl << "Here is the data that your entered:" << endl; for (int k = 0; k < MAX; k++) { ProductArray[k].Print(); } cout << "End of function TestStaticArrayObjects" << endl; } /* Given: Nothing Task: To ask the user how many products there are to be procesed and then to dynamically allocate an array of objects of that length, filling that array with data that the user enters interactively. Then to print all the data from the array and finally to free up the space used by this array. Return: Nothing. */ void TestDynamicArrayOfObjects(void) { cout << endl << "Start of function TestDynamicArrayObjects, before allocating the array of objects" << endl; // Dynamic Array is a pointer to the start of the array, but it is also a name for the array. ProductPtrType DynamicArray; int NumProducts; string NameTemp, Junk; int YearTemp; double PriceTemp; cout << "How many products do you want to put in the dynamically allocated array? "; cin >> NumProducts; getline(cin, Junk); // Clear out the input buffer. try { DynamicArray = new Product[NumProducts]; } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(1); } for (int k = 0; k < NumProducts; k++) { cout << "Enter the name for product " << k << " : "; getline(cin, NameTemp); cout << "Enter the year for product " << k << " : "; cin >> YearTemp; cout << "Enter the price for product " << k << " : "; cin >> PriceTemp; getline(cin, Junk); // Clear out the input buffer. DynamicArray[k].SetName(NameTemp); DynamicArray[k].SetYear(YearTemp); DynamicArray[k].SetPrice(PriceTemp); } // Now we print the data. cout << endl << "Here is the data that your entered:" << endl; for (int k = 0; k < NumProducts; k++) { DynamicArray[k].Print(); } // You must call delete to reclaim the space for the array of objects: delete [] DynamicArray; cout << "End of function TestDynamicArrayObjects" << endl; } /* Given: Nothing Task: Statically create an array of pointers to objects, filling that array with pointers to dynamically allocated Product objects, each of which contains data entered interactively by the user. All of the data is then printed, and then the objects are deleted. Return: Nothing. */ void TestArrayOfObjectPointers(void) { cout << endl << "Start of function TestArrayOfObjectPointers, before declaring the array of pointers to objects" << endl; ProductPtrArrayType ProductPtrArray; // This is our array of pointers to objects. string NameTemp, Junk; int YearTemp; double PriceTemp; // We ask the user for the data to put into each of the objects pointed to in ProductPtrArray. cout << endl << "You will now be asked to enter data for each object pointed to in ProductPtrArray." << endl; for (int k = 0; k < MAX; k++) { try { ProductPtrArray[k] = new Product; } catch (bad_alloc) { cerr << "Could not allocate sufficient space" << endl; exit(2); } cout << "Enter the name for product " << k << " : "; getline(cin, NameTemp); cout << "Enter the year for product " << k << " : "; cin >> YearTemp; cout << "Enter the price for product " << k << " : "; cin >> PriceTemp; getline(cin, Junk); // Clear out the input buffer. ProductPtrArray[k]->SetName(NameTemp); ProductPtrArray[k]->SetYear(YearTemp); ProductPtrArray[k]->SetPrice(PriceTemp); } // Now we print the data. cout << endl << "Here is the data that your entered:" << endl; for (int k = 0; k < MAX; k++) { ProductPtrArray[k]->Print(); } // You must call delete to reclaim the space for each object: for (int k = 0; k < MAX; k++) { delete ProductPtrArray[k]; } cout << "End of function TestArrayOfObjectPointers" << endl; }