Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of diff
ID: 3566698 • Letter: P
Question
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. 1. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. 2. Base class Package should include the name, address, city, state and zip code for the packages sender and recipient, and instance variables that store the weight (in ounces) and cost per ounce to ship the package. 3. Packages constructor should initialize these private instance variables with public properties. 4. Ensure that the weight and cost per ounce contain positive values. 5. Package should provide a public method calculateCost that returns a double indicating the cost associated with shipping the package. 6. Packages calculateCost method should determine the cost by multiplying the weight by the cost per ounce. 7. Derived class TwoDayPackage should inherit the functionality of base class Package, but also include an instance variable that represents a flat fee the shipping company charges for two-day delivery service. TwoDayPackages constructor should receive a value to initialize this instance variable. 8. TwoDayPackage should redefine method calculateCost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Packages calculateCost method. 9. Class OvernightPackage should inherit directly from class Package and contain an instance variable representing an additional fee per ounce charged for overnight delivery service. 10. OvernightPackage should redefine method calculateCost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost.
Explanation / Answer
#include #include #include using namespace std; // This class Package is the base class for two other classes, TwoDayPackage and OverNightPackage.// class Package // begins 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); // constructor // set and get functions for sender 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; // set and get functions for recipient 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; } // This class TwoDayPackage is the first derived class from class Package.// 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); // constructor void setFlatFee(double); double getFlatFee() const; void CalCost() const; private: double flatFee; }; // This class OverNightPackage is the second derived class from class Package.// 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); // constructor void setFee(double); double getFee() const; void CalCost() const; private: double fee; }; // This is the test program.// 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); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.