/* Filename: product.cpp Author: Br. David Carlson Date: July 5, 1998 Revised: July 30, 1998 so that a Product object no longer contains a string, but only a pointer to a string outside of the object. Revised: March 19, 2013 This file implements the Product class that was set up in product.h. */ #include "product.h" /* Given: InitialName A string containing the product name. InitialYear The year this product was made. InitialPrice The product's price. Task: This is the default constructor. It creates a new Product object containing the 3 items listed above, except that the object just contains a pointer to the InitialName string. Return: Nothing is directly returned, but the object is created. */ Product::Product(ShortString InitialName, int InitialYear, float InitialPrice) { // Allocate space outside of the object for a string: NamePtr = new ShortString; // If the new fails, the bad_alloc exception is unhandled and should // cause the program to abort. // Recall that you cannot simply assign one string into another. strcpy(NamePtr, InitialName); Year = InitialYear; Price = InitialPrice; // Just so that you can see when the constructor is called, print: cout << "Constructor called for object with name " << InitialName << endl; } /* Given: Nothing other than the implicit object. Task: This is the destructor. It destroys the implicit object, reclaiming all space that it used, including the space for the associated string outside of the object. Return: Nothing. */ Product::~Product(void) { // Print a message just so that you can see when the destructor // gets called: cout << "Destructor called for product named " << NamePtr << endl; // Explicitly reclaim the space used by the string: delete [] NamePtr; } /* Given: Nothing (other than the implicit object). Task: To print the data contained in this object. Return: Nothing. */ void Product::Print(void) const { cout << "Product name: " << NamePtr << endl << "Year made: " << Year << endl << "Price: " << Price << endl << endl; }