/* Filename: dyobject.cpp Author: Br. David Carlson Date: July 30, 1998 Last Revised: March 13, 2013 This program shows how to dynamically allocate an object and then free it up later. For comparison purposes it also creates a static object in the usual way. The contents of both objects are printed. Tested with: Microsoft Visual C++ 2008 g++ under Linux */ #include "product.h" int main(void) { Product StaticProd("Sample product (static)", 1998, 14.95); ProductPtrType DynamicProdPtr; DynamicProdPtr = new Product("Another product (dynamic)", 1997, 9.33); // If the new fails, a bad_alloc exception is raised. // Since that exception is unhandled, the program would abort. // Next, let's use the two objects: cout << "Printing the static object:" << endl; StaticProd.Print(); cout << "Printing the dynamic object:" << endl; DynamicProdPtr->Print(); cout << "Printing the dynamic object in another way:" << endl; (*DynamicProdPtr).Print(); // Delete space used by the dynamic object -- uses the destructor: delete DynamicProdPtr; // The destructor is automatically called on the static object // when it goes out of scope. return 0; }