The package inheritance hierarchy (below) to create a program that displays the address information and calculates the
Question:
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
using namespace std;
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 : "<cout<<"Address : "<cout<<"City : "<cout<<"State : "<cout<<"ZIP : "<cout<<"Nameof Recipient : "<cout<<"Address : "<cout<<"City : "<cout<<"State : "<cout<<"ZIP : "<cout<<"Weight of Package : "<cout<<"Cost per ounce : "<}
};
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 : "<return 0;
}
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
Microsoft Visual C# An Introduction to Object-Oriented Programming
ISBN: 978-1337102100
7th edition
Authors: Joyce Farrell