In: Computer Science
use c++
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 class 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. Package’s 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 determine 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 charges for two-day-delivery service. TwoDayPackage’s constructor should receive a value to initialize this data member. TwoDayPackage 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 from class Package and contain an additional data member representing an additional fee per ounce charged 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. Write a test program that creates objects of each type of Package and tests member function calculateCost. please send a link to a repl.it file to help find the solution or separate the files with names so I can know different header files to use use c++
cpp code:
package.h
#ifndef PACKAGE_H_INCLUDED
#define PACKAGE_H_INCLUDED
using namespace std;
//package class
class Package
{
//sender
string sen_name;
string sen_address;
string sen_city;
string sen_state;
string sen_ZIP;
//recipient
string rec_name;
string rec_address;
string rec_city;
string rec_state;
string rec_ZIP;
double weight;
double cost;
public:
Package();
//constructor with parameter
Package(string,string,string,string,string,string,string,string,string,string,double,double);
//calculate cost
double calculateCost ();
};
//two day package class inherits base class
class TwoDayPackage : public Package
{
double flatFee; //fee
public:
//constructor
TwoDayPackage(string,string,string,string,string,string,string,string,string,string,double,double,double);
double calculateCost (); //compute cost
};
//two day package class inherits base class
class OvernightPackage : public Package
{
double additionalFee; //fee
public:
//constructor
OvernightPackage(string,string,string,string,string,string,string,string,string,string,double,double,double);
double calculateCost (); //compute cost
};
#endif // PACKAGE_H_INCLUDED
package.cpp
#include <iostream>
#include "package.h"
using namespace std;
//set varibles
Package::Package(string n1,string a1,string c1,string s1,string
z1,string n2,
string a2,string c2,string s2,string z2,double w,double c)
{
//sender
this->sen_name=n1;
this->sen_address=a1;
this->sen_city=c1;
this->sen_state=s1;
this->sen_ZIP=z1;
//recipient
this->rec_name=n2;
this->rec_address=a2;
this->rec_city=c2;
this->rec_state=s2;
this->rec_ZIP=z2;
//set weight
this->weight=w;
this->cost=c; //set cost
}
//compute and return cost
double Package::calculateCost()
{
return weight*cost;
}
//derived class, set base class values
TwoDayPackage::TwoDayPackage(string n1,string a1,string c1,string
s1,string z1,string n2,
string a2,string c2,string s2,string z2,double w,double c,double
f):Package(n1,a1,c1,s1,z1,n2,a2,c2,s2,z2,w,c)
{
this->flatFee=f;//set flat fee
}
//return cost with flat fee
double TwoDayPackage::calculateCost()
{
return Package::calculateCost()+flatFee;
}
//derived class, set base class values
OvernightPackage::OvernightPackage(string n1,string a1,string
c1,string s1,string z1,string n2,
string a2,string c2,string s2,string z2,double w,double c,double
f):Package(n1,a1,c1,s1,z1,n2,a2,c2,s2,z2,w,c)
{
this->additionalFee=f;//set additional fee
}
//return cost with additional fee
double OvernightPackage::calculateCost()
{
return Package::calculateCost()+additionalFee;
}
main.cpp
#include <iostream>
#include <string>
#include "package.h"
using namespace std;
int main()
{
//set twoday package class
TwoDayPackage
p1("smith","2ndCross","Hudson","NY","1234","Angel","3rdCross","","NY","2342",5,230.5,50);
//set overnigth package class
OvernightPackage
p2("smith","2ndCross","Jamaica","NY","1234","Angel","3rdCross","","NY","2342",4,300,100);
//call function
cout << "TwoDayPackage cost: "<<p1.calculateCost()
<< endl;
cout << "OvernightPackage cost: "<<p2.calculateCost()
<< endl;
return 0;
}
output:
//for any clarification please do comments. if you found this solution useful, please give me thumbs up