#include #include #include using namespace std; class Package { protected: int barcode; double weight; double costperounce; public: Package(int = 0, double = 0, double = 1); virtual ~Package(){}; double calculateCost(void); virtual void print(void); }; Package::Package(int brcode, double wgt, double cst) : barcode{brcode}, weight{wgt}, costperounce{cst} { } double Package::calculateCost() { return (weight * costperounce); } void Package::print() { cout << "Barcode: " << barcode << "\t" << "Weight: " << weight << " oz" << "\t" << "Cost per oz: $" << left << setw(4) << costperounce << "\t\t\t\t" << "Total cost: $" << calculateCost() << "\t\n"; } class TwoDayPackage : public Package { private: double flatfee; public: TwoDayPackage(int = 0, double = 0, double = 1, double = 0); double calculateCost(void); void print(void) override; }; TwoDayPackage::TwoDayPackage(int brcode, double wgt, double cst,double fee) : Package(brcode, wgt, cst), flatfee{fee} { } double TwoDayPackage::calculateCost() { return (weight * costperounce + flatfee); } void TwoDayPackage::print() { cout << "Barcode: " << barcode << "\t" << "Weight: " << weight << " oz" << "\t" << "Cost per oz: $"<< left << setw(4) << costperounce << "\t" << " Flat fee: " << flatfee<< "\t"<< "\t" << "Total cost: $" << calculateCost() << "\t\n"; } class OvernightPackage : public Package { private: double xfeeperounce; public: OvernightPackage(int = 0, double = 0, double = 1, double = 0); double calculateCost(void); void print(void) override; }; OvernightPackage::OvernightPackage(int brcode, double wgt, double cst,double fee) : Package(brcode, wgt, cst), xfeeperounce{fee} { } double OvernightPackage::calculateCost() { return (weight * (costperounce + xfeeperounce)); } void OvernightPackage::print() { cout << "Barcode: " << barcode << "\t" << "Weight: " << weight << " oz" << "\t" << "Cost per oz: $" << left << setw(4) << costperounce +xfeeperounce<< " ($" << xfeeperounce << " O/N extra fee)" <<"\t"<< "\t" << "Total cost: $" << calculateCost() << "\t\n"; } int main() { cout << "\nOriginal output:\n"; Package pack(1235434, 12, 1); Package pack1(1234434, 10, 1.10); pack.print(); TwoDayPackage twopack(12345678, 12, 1, 13); twopack.print(); OvernightPackage ovpack(12345678, 12, 1, 2.50); ovpack.print(); cout << endl; vector packages={pack, pack1, twopack, ovpack}; cout << "Output using a vector of Package objects:\n"; for(auto elem : packages) elem.print(); cout << endl; //Package * basePtr =&twopack; //Base pointer pointing to derived object. vector packagePtr={&pack, &pack1, &twopack, &ovpack}; cout << "Output using a vector of Package pointers:\n"; for(auto elem : packagePtr) elem->print(); cout << endl; }