// CIS 235 Lecture 10 // Example of an array of objects of different types // that share a commom base class // Use of virtual functions called by using a base class // pointer to illustrate "dynamic binding" of functions // NOTE: We are studying the behavior of overridden functions // Lab 6 illustrates "static binding" of functions // 1 - scoping // 2 - alias - ONLY for functions that are not virtual // 3 - base class pointer - ONLY for functions that are not virtual // 4 - casting #include using std::cout; using std::endl; using std::cin; #include "invent.h" #include "pen.h" #include "book.h" #include "namedBook.h" void main() { // Create any array of pens, books and namedBooks // But arrays must all be of the same type // So to have an array of pens, books and namedBooks we need // an array of base class pointers. invent * * data; int count; int i; cout << "How many items do you want to group together? "; cin >> count; data = new invent * [ count]; for( i = 0; i < count; i++) { cout << "Select p (pen) , b (book), or n (named book) "; char choice; cin >> choice; if ( choice == 'p' ) data[i] = new pen; else if ( choice == 'b' ) data[i] = new book; else data[i] = new namedBook; data[i]->kinput(); // POLYMORPHIC Function // the appropriate overridden method will be called // based on the data type of the object // the pointer variable points to } // Generate a report of the entire inventory and // give the total retail value cout << endl << "Inventory Report" << endl; double total = 0.0; for( i = 0; i < count; i++) { cout << data[i]->identify() << endl; // POLYMORPHIC Function data[i]->print(cout); // POLYMORPHIC Function cout << endl; total += data[i]->getRetailValue(); } cout << "\nTotal retail value is " << total << endl; // Give back all of the memory // First give back the contents for (i = 0; i < count; i++) delete data[i]; // Then give back the array of pointers delete [] data; } /* How many items do you want to group together? 3 Select p (pen) , b (book), or n (named book) n Enter data for a book Enter code, number, cost, price 8753 4 24.55 49.98 Enter y if hardback y Enter the name of the book - NO SPACES: C++_Programming Select p (pen) , b (book), or n (named book) p Enter data for a pen Enter code, number, cost, price 2367 24 .45 .99 Enter the pen color blue Select p (pen) , b (book), or n (named book) b Enter data for a book Enter code, number, cost, price 5469 24 4.41 9.75 Enter y if hardback n Inventory Report named book Code: 8753 Quantity: 4 Cost: 24.55 Price: 49.98 Type: Hardback Book name: C++_Programming pen Code: 2367 Quantity: 24 Cost: 0.45 Price: 0.99 Color: blue book Code: 5469 Quantity: 24 Cost: 4.41 Price: 9.75 Type: Softback Total retail value is 457.68 Named Book object leaving at address 003462F0 Book object leaving at address 003462F0 Inventory object leaving at address 003462F0 Pen object leaving at address 003464C0 Inventory object leaving at address 003464C0 Book object leaving at address 00346520 Inventory object leaving at address 00346520 Press any key to continue . . . */