////////////////////////////////////////////////////////////////////////////// // Perishable.C // // CMSC341 Fall 2001 -Project 1 // // Kyu Bae 212-04-5868 Section 201 // // kyu_bae@yahoo.com /kbae1@umbc.edu // // created: 1 September 2001 // // current: 16 September 2001 // ////////////////////////////////////////////////////////////////////////////// //header files #include "Perishable.H" //////////////////////////////////////////////////////////////////////////////// // class PerishableGoods: // vector object with 3 data types, type, color, weight. assigns to another // PerishableGoods. compare for equality of same PerishableGoods. // Author: kyu bae // Version: 10 september 2001 //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// // Default constructor: // assign all three data members to zero /////////////////////////////////////////////////////////////////////////// PerishableGoods::PerishableGoods() { _type = " "; _color = " "; _weight = 0; } //////////////////////////////////////////////////////////////////////////// // constructor with three arguement: assigns type to _type, color to _color // weight to _weight // Param type: type is string object to write to _type // Param color: color is string object to write to _color // Param weight: weight is integer type to write to _weight //////////////////////////////////////////////////////////////////////////// PerishableGoods::PerishableGoods(string type, string color, int weight) { _type = type; _color = color; _weight = weight; } //////////////////////////////////////////////////////////////////////////// // Copy constructor: // copy object of PerishableGoods to another object of PerishableGoods. // assignment operator is called here. // Param rhs: rhs to copy /////////////////////////////////////////////////////////////////////////// PerishableGoods::PerishableGoods(const PerishableGoods & rhs) { operator= (rhs); } /////////////////////////////////////////////////////////////////////////// // Destructor: /////////////////////////////////////////////////////////////////////////// PerishableGoods::~PerishableGoods() { } /////////////////////////////////////////////////////////////////////////// // Assignment operator: // assign pg to this PerishableGoods.check if copying same object. If not // same object, it assigns its data member to this object. // Param pg: pg is the PerishableGoods to assign the rvalue // Return: this PerishableGoods after modification /////////////////////////////////////////////////////////////////////////// const PerishableGoods & PerishableGoods::operator= (const PerishableGoods & pg) { if (this != &pg) { _type = pg._type; _color = pg._color; _weight = pg._weight; } return *this; } /////////////////////////////////////////////////////////////////////////// // print your PerishableGoods data members // Param out: out the stream to write data members /////////////////////////////////////////////////////////////////////////// void PerishableGoods::print(ostream &out) const { out << _type << " " << _color << " " << _weight; } /////////////////////////////////////////////////////////////////////////// // Equal operator: // comapre if PerishableGoods p1 is equal. and if not same returns false // if same returns true. // Param p1: p1 is the PerishableGoods to compare the perishableGoods // Return: boolean if they are same or not. /////////////////////////////////////////////////////////////////////////// bool PerishableGoods::operator == (const PerishableGoods &p1) const { if(!strcmp(_type.c_str(), p1._type.c_str()) && !strcmp(_color.c_str(), p1._color.c_str()) && (_weight == p1._weight)) return true; return false; } //////////////////////////////////////////////////////////////////////////////// // Output operator: // this calls print functions to print the object. // Param out: out the stream to which to write rPer // Param rPer: rPer is the PerishableGoods object to write // Return: ostream //////////////////////////////////////////////////////////////////////////////// ostream &operator << (ostream &out, const PerishableGoods &rPer) { rPer.print(out); return out; }