/*************************************************/ // R. A. Hillyard // inventory03.cpp // November 2001 // //class with constructors and friend function /*************************************************/ #include <iostream> #include <iomanip> using namespace std; class Inventory { public: //friend functions hasMost friend Inventory hasMost(Inventory p1, Inventory p2, Inventory p3); //member function hasMost Inventory hasMost(Inventory p1, Inventory p2); //constructors Inventory(); Inventory (int num); Inventory (int num, int stock, double price); void input(); void output(); int getPartNum(); int getInStock(); double getCost(); private: int partNum; int inStock; double cost; }; /****************************************************************/ Inventory hasMost(Inventory p1, Inventory p2, Inventory p3) { if(p1.inStock > p2.inStock && p1.inStock > p3.inStock) { return p1; } if(p2.inStock > p1.inStock && p2.inStock > p3.inStock) { return p2; } return p3; } /****************************************************************/ Inventory Inventory::hasMost(Inventory p1, Inventory p2) { if(p1.inStock > p2.inStock && p1.inStock > inStock) { return p1; } if(p2.inStock > p1.inStock && p2.inStock > inStock) { return p2; } return *this; //return the calling object } /****************************************************************/ Inventory::Inventory() { cout << "In default Constructor\n"; partNum = -1; inStock = 0; cost = 0.0; } /****************************************************************/ Inventory::Inventory (int num) { cout << "In 1 arg Constructor\n"; partNum = num; inStock = 0; cost = 0.0; } /****************************************************************/ Inventory::Inventory (int num, int stock, double price) { cout << "In 3 arg Constructor\n"; partNum = num; inStock = stock; cost = price; } /****************************************************************/ void Inventory::input() { cout << "\nenter part number, number in stock, and the cost:\n>"; cin >> partNum >> inStock >> cost; } /****************************************************************/ void Inventory::output() { cout << setw(6) << partNum << setw(6) << inStock << setw(8) << cost << endl; } /****************************************************************/ int Inventory::getPartNum() { return partNum; } /****************************************************************/ int Inventory::getInStock() { return inStock; } /****************************************************************/ double Inventory::getCost() { return cost; } /****************************************************************/ int main() { Inventory part1(1234, 542, 12.99); Inventory part2(2345, 234, 77.69); Inventory part3(8765, 87, 1352.63); Inventory part4; cout << "*** Inventory Database ***\n"; part1.output(); part2.output(); part3.output(); //call friend function part4 = hasMost(part1, part2, part3); cout << "The part with the most stock is: "; part4.output(); //call default constructor to reset part4 part4 = Inventory(); cout << "Part after being reset is: "; part4.output(); //call member function part4 = part3.hasMost(part1, part2); cout << "\nThe part with the most stock is: "; part4.output(); } /**********************Program Output*************************** In 3 arg Constructor In 3 arg Constructor In 3 arg Constructor In default Constructor *** Inventory Database *** 1234 542 12.99 2345 234 77.69 8765 87 1352.63 The part with the most stock is: 1234 542 12.99 In default Constructor Part after being reset is: -1 0 0 The part with the most stock is: 1234 542 12.99 /****************************************************************/