/********************************************************************* C201 Computer Programming II Fall 2006 Dana Vrajitoru & A class to represent products in stock in a shop. **********************************************************************/ #include "product.h" #include using namespace std; #include // **************************** Constructors ************************** // Default constructor. Product::Product() { name[0] = '\0'; type = food_ty; quantity = 0; barcode = 0; price = 0.0; } //Copy constructor. Product::Product(Product &data) { Init(data); } // ************************** Initializing functions ***************** // Reset to empty values. It looks like the default constructor but it // can be called at any type to recycle the target object. void Product::Init() { name[0] = '\0'; type = food_ty; quantity = 0; barcode = 0; price = 0.0; } // Initialize the target object from the parameter data so that we can // copy an object to another one even after the object was created. void Product::Init(Product &data) { strcpy(name, data.name); type = data.type; quantity = data.quantity; barcode = data.barcode; price = data.price; } // ************************* Overloaded operators ******************** // Assignment operator. Product &Product::operator=(Product &data) { if (this != &data) { strcpy(name, data.name); type = data.type; quantity = data.quantity; barcode = data.barcode; price = data.price; } else cout << "Trying to copy an object of type Product to itself." << endl; // Important: return the target object. return *this; } // Input-output void Product::Input() { // To be copied from homework 7. } void Product::Output() { // To be copied from homework 7. } // ************************** Data manipulation ********************** // Add more products to the stock. void Product::Add(int how_many) { quantity += how_many; } // Remove some products from the stock. It needs to be a bool because // if we don't have that many items in the stock we can't remove // them. If the function returns false, the target object is // unchanged. bool Product::Remove(int how_many) { if (how_many <= quantity) { quantity -= how_many; return true; } else return false; } // An overloaded output operator so that we can use a product // object in a cout << statement. ostream &operator<<(ostream &out, Product &data) { out << "Name: " << data.name << ", "; switch (data.type) { case food_ty: out << "Food, "; break; case health_ty: out << "Health, "; break; case cloth_ty: out << "Cloth, "; break; default: out << "Home, "; } out << " Quantity: " << data.quantity << " Barcode: " << data.barcode << " Price: " << data.price << endl; return out; }