/* Filename: product3.cpp Author: Br. David Carlson Date: July 5, 1998 Revised: July 17, 2000 This file implements the Product class that was set up in product3.h. */ #include "product3.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. Return: Nothing is directly returned, but the object is created. */ Product::Product(ShortString InitialName, int InitialYear, float InitialPrice) { // Recall that you cannot simply assign one string into another. strcpy(Name, InitialName); Year = InitialYear; Price = InitialPrice; } /* Given: OldProduct An existing Product object. Task: This is a copy constructor. It creates a new Product object containing a copy of the data from OldProduct. Return: Nothing is directly returned, but the implicit object is created. */ Product::Product(const Product & OldProduct) { strcpy(Name, OldProduct.Name); Year = OldProduct.Year; Price = OldProduct.Price; } /* 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: " << Name << endl << "Year made: " << Year << endl << "Price: " << Price << endl << endl; } /* Given: NewPrice A new price to use for the implicit Product object. Task: To change the price of this object to NewPrice. Return: Nothing directly, but the Product object is modified. */ void Product::ChangePrice(float NewPrice) { Price = NewPrice; } /* Given: Nothing. Task: To look up the name of the implicit Product object. Return: ProductName The name of this Product object. */ void Product::GetName(ShortString ProductName) const { // Once again, you cannot use assignment with strings. strcpy(ProductName, Name); } /* Given: Nothing. Task: To look up the year the implicit Product object was made. Return: This year in the function name. */ int Product::GetYear(void) const { return Year; } /* Given: Nothing. Task: To look up the price of the implicit Product object. Return: The price of this Product object, in the function name. */ float Product::GetPrice(void) const { return Price; } /* Given: RHS A Product object (the right-hand side of the < ). Task: To compare the price of the implicit Product object to that of the RHS. Return: In the function name return true if the implicit object is less than RHS in price, false otherwise. */ bool Product::operator<(const Product & RHS) const { if (Price < RHS.Price) return true; else return false; } /* Given: RHS An existing Product object. Task: This is an overloaded assignment operator. It copies all of the data from the RHS object into the implicit object. Return: A reference to the implicit object is returned in the function name so as to allow one to chain assignments together as in: ProductA = ProductB = ProductC; The implicit object is modified. */ Product & Product::operator=(const Product & RHS) { strcpy(Name, RHS.Name); Year = RHS.Year; Price = RHS.Price; return *this; } /* Given: LHS A Product object (the left-hand side of the == ). RHS A Product object (the right-hand side of the == ). Task: To compare the prices of the LHS and RHS objects. Return: In the function name return true if the LHS object is equal to the RHS object in price, false otherwise. */ bool operator==(const Product & LHS, const Product & RHS) { if (LHS.Price == RHS.Price) return true; else return false; } /* Given: OutputStream An output file stream. Prod A Product object. Task: To print the contents of Prod onto the OutputStream. Return: The modified file stream is returned in the function name. This allows one to chain together more than one << operator as in: cout << ProductA << endl << ProductB << endl; */ ostream & operator<<(ostream & OutputStream, const Product & Prod) { OutputStream << "Product name: " << Prod.Name << endl << "Year made: " << Prod.Year << endl << "Price: " << Prod.Price << endl << endl; return OutputStream; }