Question: 1. (Package Inheritance Hierarchy) Use the package inheritance hierarchy (below) to create a program that displays the address information and calculates the shipping costs for
1. (Package Inheritance Hierarchy) Use the package inheritance hierarchy (below) to create a program that displays the address information and calculates the shipping costs for several packages. The program should contain a vector of package pointers to objects of classes: TwoDayPackage and OvernightPackage. Loop through the vector to process the packages polymorphically. For each package, invoke get functions to obtain the address information of the sender and the recipient, then print the two addresses as they would appear on mailing labels. Also, call each package's calculateCost member function and print the result. Keep track of the total shipping cost for all packages in the vector, and display this total when the loop terminates.
#include
class Package // base class { private: string nameSender, addressSender, citySender, stateSender, ZIPSender,nameRecipient, addressRecipient, cityRecipient, stateRecipient, ZIPRecipient; double weight,cost;
public: //constructor Package(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost) { this->nameSender = nameSender; this->addressSender = addressSender; this->citySender = citySender; this->stateSender = stateSender; this->ZIPSender = ZIPSender; this->nameRecipient = nameRecipient; this->addressRecipient = addressRecipient; this->cityRecipient = cityRecipient; this->stateRecipient = stateRecipient; this->ZIPRecipient = ZIPRecipient; if(weight > 0) this->weight = weight; if(cost > 0) this->cost = cost;
} //get methods double getWeight() { return weight; } double getCost() { return cost; } double calculateCost() { return weight*cost; }
void display() { cout<<" Name of sender : "< }; class TwoDayPackage : public Package // derived class { private : double flatFee; public: //sending arguments to base class constructor TwoDayPackage(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost,double flatFee):Package(nameSender,addressSender,citySender,stateSender,ZIPSender,nameRecipient,addressRecipient,cityRecipient,stateRecipient,ZIPRecipient,weight,cost) { this->flatFee = flatFee; } double calculateCost() { return flatFee + getWeight()*getCost(); } void displayInfo() { cout<<" Two Day Package "; display(); cout<<" Flat Fee : "< class OverNightPackage : public Package { private : double additionalFee; public: OverNightPackage(string nameSender, string addressSender, string citySender, string stateSender, string ZIPSender,string nameRecipient, string addressRecipient, string cityRecipient, string stateRecipient, string ZIPRecipient,double weight,double cost,double additionalFee):Package(nameSender,addressSender,citySender,stateSender,ZIPSender,nameRecipient,addressRecipient,cityRecipient,stateRecipient,ZIPRecipient,weight,cost) { this->additionalFee = additionalFee; } double calculateCost() { return additionalFee + getWeight()*getCost(); } void displayInfo() { cout<<" Overnight Package "; display(); cout<<" Additional Fee : "< }; int main() { TwoDayPackage p1("John","234,New Street","ALBANY", "NY" ,"12261-0001","Smith","307"," Trenton", "NJ"," 08625-0307",4.5,12.5,15.5); p1.displayInfo(); cout<<" Total Cost : "< OverNightPackage p2("Candy","234,New Street","ALBANY", "NY" ,"12261-0001","Nancy","307"," Trenton", "NJ"," 08625-0307",4.8,17.5,23.5); p2.displayInfo(); cout<<" Total Cost : "< Output: Two Day Package Name of sender : John Address : 234,New Street City : ALBANY State : NY ZIP : 12261-0001 Nameof Recipient : Smith Address : 307 City : Trenton State : NJ ZIP : 08625-0307 Weight of Package : 4.5 Cost per ounce : 12.5 Flat Fee : 15.5 Total Cost : 71.75 Overnight Package Name of sender : Candy Address : 234,New Street City : ALBANY State : NY ZIP : 12261-0001 Nameof Recipient : Nancy Address : 307 City : Trenton State : NJ ZIP : 08625-0307 Weight of Package : 4.8 Cost per ounce : 17.5 Additional Fee : 23.5 Total Cost : 107.5 2. Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user -defined class type, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator ==. Now what happens when you attempt to run this program?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
