/*************************************************/ // R. A. Hillyard // inventory04.cpp // November 2001 // // class with constructors, friend functions and operator overloading // use of const keyword for reference parameters and functions /*************************************************/ #include<iostream> #include<iomanip> using namespace std; class Inventory { public: //friend functions friend istream& operator >>(istream& ins, Inventory& p1); friend ostream& operator <<(ostream& outs, const Inventory& p1); friend bool operator > (const Inventory& p1, const Inventory& p2); friend Inventory hasMost(const Inventory& p1, const Inventory& p2, const Inventory& p3); //constructors Inventory(); Inventory (int num); Inventory (int num, int stock, double price); //accessor functions int getPartNum() const; int getInStock() const; double getCost() const; //mutator functions void setPartNum(int num); void setInStock(int num) ; void setCost(double num); private: int partNum; int inStock; double cost; }; /****************************************************************/ istream& operator >>(istream& ins, Inventory& p1) { ins >> p1.partNum >> p1.inStock >> p1.cost; return ins; } /****************************************************************/ ostream& operator <<(ostream& outs, const Inventory& p1) { outs << setw(5) << p1.partNum << setw(6) << p1.inStock << setw(10) << p1.cost << endl; return outs; } /****************************************************************/ bool operator > (const Inventory& p1, const Inventory& p2) { return(p1.inStock > p2.inStock); } /****************************************************************/ Inventory hasMost(const Inventory& p1, const Inventory& p2, const Inventory& p3) { if(p1 > p2 && p1 > p3) {return p1;} if(p2 > p1 && p2 > p3) {return p2;} return p3; } /****************************************************************/ Inventory::Inventory() { partNum = -1; inStock = 0; cost = 0.0; } /****************************************************************/ Inventory::Inventory (int num) { partNum = num; inStock = 0; cost = 0.0; } /****************************************************************/ Inventory::Inventory (int num, int stock, double price) { partNum = num; inStock = stock; cost = price; } /****************************************************************/ int Inventory::getPartNum() const { return partNum; } /****************************************************************/ int Inventory::getInStock() const { return inStock; } /****************************************************************/ double Inventory::getCost() const { return cost; } /****************************************************************/ void Inventory::setPartNum(int num) { partNum = num; } /****************************************************************/ void Inventory::setInStock(int num) { inStock = num; } /****************************************************************/ void Inventory::setCost(double num) { cost = num; } /****************************************************************/ int main() { Inventory part1(1234, 542, 12.99); Inventory part2(2345, 234, 77.69); Inventory part3; cout << "enter partNum stock and cost: "; cin >> part3; //use of >> operator double value = (part1.getInStock() * part1.getCost()) + (part2.getInStock() * part2.getCost()) + (part3.getInStock() * part3.getCost()); Inventory part4 = hasMost(part1, part2, part3); cout << "*** Inventory Database ***\n"; cout << part1 << part2 << part3 << endl; //use of << operator cout << "\nTotal value of inventory: " << value << endl; cout << "The part with the most stock is: " << part4 << endl; return 0; } /****************************************************************/ /*******************Program Output****************************** enter partNum stock and cost: 6543 434 34.56 *** Inventory Database *** 1234 542 12.99 2345 234 77.69 6543 434 34.56 Total value of inventory: 40219.1 The part with the most stock is: 1234 542 12.99 */