The following question was posted once but never answered. Can someone help with
ID: 3624920 • Letter: T
Question
The following question was posted once but never answered. Can someone help with this?
(Package Inheritance Hierarchy) Package-delivery services, such as FedEx , DHL and UPS, offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP code for both the sender and the recipient of the package, in addition to data members that store the weight (in ounces) and cost per ounce to ship the package. Packages constructor should initialize these data members. Ensure that the weight and cost per ounce contain positive values. Package should provide a public member function calculateCost that returns a double indicating the cost associated with shipping the package. Package’s calculateCost function should determinge the cost by multiplying the weight by the cost per ounce. Derived class TwoDayPackage should inherit the functionality of base class Package, but also include a data member that represents a flat fee that the shipping company chargers for two-day-delivery service. TwoDayPackages constructor should receive a value to initialize this data member. TwoDay Package should redefine member function calculateCost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package’s calculateCost function. Class OvernightPackage should inherit directly form class Package and contain an additional data member representing an additional fee per ounce charger for overnight-delivery service. OvernightPackage should redefine member function calculateCost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Writhe a test program that creates objects of each type of Package and test member function calculateCost
Explanation / Answer
Dear... Please try with this sample code: #include <iomanip> using namespace std; class Package { public:Package(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double = 0.0, double = 0.0); void setSendName(const string &); string getSendName() const; void setSendAdd(const string &); string getSendAdd() const; void setSendCity(const string &); string getSendCity() const; void setSendSt(const string &); string getSendSt() const; void setSendZip(const string &); string getSendZip() const; void setRecName(const string &); string getRecName() const; void setRecAdd(const string &); string getRecAdd() const; void setRecipientCity(const string &); string getRecipientCity() const; void setRecSt(const string &); string getRecSt() const; void setRecZip(const string &); string getRecZip() const; void setWt(double); double getWt() const; void setShip(double); double getShip() const; double CalCost() const; private: string sendName; string sendAdd; string sendCity; string sendState; string sendZip; string recName; string recAdd; string recCity; string recState; string recZip; double wt; double shipCost; }; Package::Package(const string &sname, const string &saddress, const string &scity, const string &sstate, const string &szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, const string &rzipcode, double wt, double shipCost) { sendName = sname; sendAdd = saddress; sendCity = scity; sendState = sstate; sendZip = szipcode; recName = rname; recAdd = raddress; recCity = rcity; recState = rstate; recZip = rzipcode; setWt(wt); setShip(shipCost); } void Package::setSendName(const string &sname)
{ sendName = sname; } string Package::getSendName() const { return sendName; } void Package::setSendAdd(const string &saddress) { sendAdd = saddress; } string Package::getSendAdd() const { return sendAdd; } void Package::setSendCity(const string &scity) { sendCity = scity; } string Package::getSendCity() const { return sendCity; } void Package::setSendSt(const string &sstate) { sendState = sstate; } string Package::getSendSt() const { return sendState; } void Package::setSendZip(const string &szipcode) { sendZip = szipcode; } string Package::getSendZip() const { return sendZip; } void Package::setRecName(const string &rname) { recName = rname; } string Package::getRecName() const { return recName; } void Package::setRecAdd(const string &raddress) { recAdd = raddress; } string Package::getRecAdd() const { return recAdd; } void Package::setRecipientCity(const string &rcity) { recCity = rcity; } string Package::getRecipientCity() const { return recCity; } void Package::setRecSt(const string &rstate) { recState = rstate; } string Package::getRecSt() const { return recState; } void Package::setRecZip(const string &rzipcode) { recZip = rzipcode; } string Package::getRecZip() const { return recZip; } void Package::setWt(double wt) { wt = (wt < 0.0 ) ? 0.0 : wt; } double Package::getWt() const { return wt; } void Package::setShip(double shipCost) { shipCost = ( shipCost < 0.0) ? 0.0 : shipCost; } double Package::getShip() const { return shipCost; } double Package::CalCost() const { return wt * shipCost; } // derived class.// class TDP : public Package { public: TDP(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); void setFlatFee(double); double getFlatFee() const; void CalCost() const; private: double flatFee; }; // derived class.// class ONP : public Package { public: ONP(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double=0.0, double=0.0, double=0.0); void setFee(double); double getFee() const; void CalCost() const; private: double fee; }; // main // int main() { ONP box("name", "123 this Street", "boston", "ma", "12345", "receiver", "123 that street", "medford", "ma", "25341", 10.00, 1.50, .85); TDP parcel("name2", "123 1st Street", "orlando", "fl", "56474", "receiver2", "833 2nd Street", "miami", "fl", "88472", 15.00, 1.05, 5.00); cout << fixed << setprecision(2); cout << "To ship a box with overnight delivery "; cout << "The sender " << box.getSendName()<< " "; cout << " " << box.getSendAdd() << " "; cout << " " << box.getSendCity() << " " << box.getSendSt() << " " <<box.getSendZip() <<" "; cout << "The recipient " << box.getRecName()<< " "; cout << " " << box.getRecAdd() << " "; cout << " " << box.getRecipientCity() << " "<< box.getRecSt() << " "<< box.getRecZip() << " "; cout << "The cost is $ " <<box.CalCost() << " "; cout << " "; cout << "To ship a parcel with 2 day delivery "; cout << "The sender " << parcel.getSendName()<< " "; cout << " " << parcel.getSendAdd() << " "; cout << " " << parcel.getSendCity()<<" "<< parcel.getSendSt()<<" " << parcel.getSendZip()<< " "; cout << "The recipient " << parcel.getRecName()<< " "; cout << " " << parcel.getRecAdd() << " "; cout << " " << parcel.getRecipientCity() << " " << parcel.getRecSt() << " " << parcel.getRecZip() << " "; cout << "The cost is $ "<<parcel.CalCost() << " "; system("pause"); return 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.