/* Filename: product3.cpp Author: Br. David Carlson Date: July 5, 1998 Revised: July 17, 2000 and April 2, 2021 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(string InitialName, int InitialYear, double InitialPrice) { 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) { Name = OldProduct.Name; Year = OldProduct.Year; Price = OldProduct.Price; } /* Given: Nothing other than the implicit object. Task: This is the destructor. It prints a message and reclaims the memory space of the implicit object. Return: Nothing */ Product::~Product(void) { cout << "The destructor has been called on an object containing:" << endl; (*this).Print(); // The automatic destructor runs at this point, reclaiming the memory // space used by the implicit Product object. } /* 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: NewName A new name to use for the implicit Product object. Task: To change the name of this object to NewName. Return: Nothing directly, but the Product object is modified. */ void Product::SetName(string NewName) { Name = NewName; } /* Given: NewYear A new year to use for the implicit Product object. Task: To change the year of this object to NewYear. Return: Nothing directly, but the Product object is modified. */ void Product::SetYear(int NewYear) { Year = NewYear; } /* 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::SetPrice(double NewPrice) { Price = NewPrice; } /* Given: Nothing. Task: To look up the name of the implicit Product object. Return: The name of this Product object, in the function name. */ string Product::GetName(void) const { return Name; } /* Given: Nothing. Task: To look up the year the implicit Product object was made. Return: This year of this Product object, 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. */ double 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) { 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; }