COSC 1320 - C++ Programming and
ITSE 1307 - Introduction to C++ Programming
Bob Comer, Professor of Computer Studies
Using Arrays of Pointers to Objects
To try this program with your compiler, highlight the program text below, make a copy of it (ctrl-c in Windows), open a source code window in your compiler and paste the program code into the window (ctrl-v in Windows).
//************************************************************** //* Programmer: Bob Comer //* //* This program allows the user to look up item prices //* at a store. The price information is loaded from //* a file and stored in an array of pointers to objects. //************************************************************** #include <iostream> #include <fstream> using namespace std; const int MAX_ITEMS = 200; class ItemRec { private: int itemNo; //inventory item number float price; //item price public: ItemRec(); ItemRec(int, float); void set(int, float); int getItemNo(); float getPrice(); }; ItemRec::ItemRec() { itemNo = 0; price = 0; } ItemRec::ItemRec(int item, float itemPrice) { set(item, itemPrice); } void ItemRec::set(int item, float itemPrice) { itemNo = item; price = itemPrice; } int ItemRec::getItemNo() { return itemNo; } float ItemRec::getPrice() { return price; } void getItems( ifstream&, ItemRec*[], int&); int search( int, ItemRec*[], int); int main() { ItemRec* item[MAX_ITEMS]; // inventory items int numItems; // number of items in the lists int srchItemNo; // an item number to look up int position; // position of an item in list ifstream itemFile; // item price file // open Item Price file itemFile.open("itemfile.txt"); if (!itemFile) { cout << "Can not open item file \"itemfile.txt\"" << endl; return 1; } // load item price file getItems( itemFile, item, numItems); // look up item prices cout << "Enter Item Number (or -1 to quit): "; cin >> srchItemNo; while (srchItemNo > 0) { // get price for item position = search( srchItemNo, item, numItems); if (position == -1) { cout << "Item " << srchItemNo << " is not in the inventory" << endl; } else cout << "Price for item " << srchItemNo << " is $ " << item[position]->getPrice() < endl; // get next item cout << "Enter Item Number (or -1 to quit): "; cin >> srchItemNo; } return 0; } //************************************************************** //* FUNCTION getItems //* //* Copies item information from file into arrays //************************************************************** void getItems( ifstream &itemFile, // in - item price file ItemRec *item[], // out - list of items int &numItems) // out - number of items { int oneItemNo; // an item number float onePrice; // price for the item numItems = 0; itemFile >> oneItemNo >> onePrice; while (itemFile && numItems < MAX_ITEMS) { // move new item into arrays and increment item count item[numItems] = new ItemRec; item[numItems]->set( oneItemNo, onePrice ); numItems++; // get next item information itemFile >> oneItemNo >> onePrice; } } //************************************************************** //* FUNCTION search //* //* Find the location of an item in a list. //* //************************************************************** int search( int srchVal, // in - value to search for ItemRec *list[], // in - list to search int length) // in - number of items in list { // search list for item and return its position if found int index = 0; while (index < length) { if (srchVal == list[index]->getItemNo()) return index; index++; } // item not found - return a flag return -1; }
Copyright: Ó2005 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: November 30, 2005