#include #include #include using namespace std; ////////////////////////////////////////////////////////// ////// DO NOT CHANGE ANYTHING IN THE public SECTION ////// ////////////////////////////////////////////////////////// class Weight { public: Weight(); // Constructor; MUST initialize the weight to 0 lb. 0 oz. void read(); // Prompt for and read a weight. // It MUST prompt "Enter pounds: ", read a number, // then prompt "Enter ounces: " and read another number. // You may assume the user will enter nonnegative integers. void print(); // Print this weight in the form "X lb. Y oz.", with no line breaks. // (X is the number of pounds, Y is the number of ounces.) // (For example, 5 lb. 2 oz. or 0 lb. 13 oz.) void set(int pounds_in, int ounces_in); // Sets this weight to the given number of pounds and ounces // (You may assume pounds_in and ounces_in are nonnegative.) bool heavier(Weight other_weight); // Returns true if and only if this weight is heavier than other_weight Weight plus(Weight other_weight); // Returns a new weight, which equals this weight + other_weight // (e.g. 2 lb. 3 oz. + 0 lb. 15 oz. = 3 lb. 2 oz.; // there are 16 ounces in a pound) // This weight is NOT changed. Weight times(double factor); // Returns a new weight, which equals this weight times 'factor' // e.g. if A is a Weight object, // A.times(2.0) gives a new weight that's twice A. // This weight is NOT changed. private: // #####***** YOU FILL IN THIS PART: *****##### int pounds; int ounces; }; ////////////////////////////////////////////////////////// //////////// DO NOT CHANGE ANYTHING IN main() //////////// ////////////////////////////////////////////////////////// int main() { Weight a, b, c; cout << "An uninitialized Weight variable is "; a.print(); cout << endl << endl; cout << "Please enter the weight of the first package:\n"; a.read(); cout << "Please enter the weight of the second package:\n"; b.read(); cout << endl; cout << "You entered "; a.print(); cout << " and "; b.print(); cout << endl; if (a.heavier(b)) cout << "The first package is heavier.\n"; else if (b.heavier(a)) cout << "The second package is heavier.\n"; else cout << "The two packages are the same weight.\n"; cout << "Their average weight is "; a.plus(b).times(0.5).print(); // OR: (what that complicated thing means!) // // Weight x; // x = a.plus(b); // x = x.times(0.5); // x.print(); // // The one-line version works because a.plus(b) is a Weight, // to which we can do .times(0.5) ... and the result of that // is also a Weight, to which we can do .print() . cout << endl << endl; c.set(5,0); cout << "The average weight of those two packages\n" << "TOGETHER WITH a package weighing "; c.print(); cout << " is "; a.plus(b).plus(c).times(1/double(3)).print(); cout << endl; return EXIT_SUCCESS; } // #####***** YOU FILL IN the function definitions *****##### // Remember that you need to attach Weight:: to the function name // when you're defining a function inside the class. Weight::Weight() { ounces= 0; pounds= 0; } void Weight::read() { cout << " how many lbs "; cout << endl; cin >> pounds; cout << " How many ozs "; cin >> ounces; cout << endl; } void Weight::print() { cout << pounds <<"'"<< ounces <<"'"; } void Weight::set(int pounds_in, int ounces_in) { pounds = pounds_in; ounces = ounces_in; } bool Weight::heavier(Weight other_weight) { return ( pounds > other_weight.pounds)|| (pounds==other_weight.pounds && ounces > other_weight.ounces); } Weight Weight::times(double factor) { int xtotal; Weight x; xtotal= (ounces + pounds*16); xtotal= xtotal * factor; x.pounds= xtotal/16; x.ounces= xtotal%16; return x; } Weight Weight::plus(Weight other_weight) { Weight x; x.pounds= pounds + other_weight.pounds; x.ounces= ounces + other_weight.ounces; return x; }