Use the Package inheritance hierarchy created in Example 1 to create a program t
ID: 3828532 • Letter: U
Question
Use the Package inheritance hierarchy created in Example 1 to create a program that calculate 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, call each Package's calculate Cost 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.Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package();
Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
double calCost(double, double);
~Package();
private:
string destination_name;
string destination_address;
string destination_zip;
string destination_city;
string destination_state;
double weight;
double cost;
};
Package::Package()
{
cout<<"Constucting Package Object with default values: "<<endl;
string destination_name="";
string destination_address="";
string destination_zip="";
string destination_city="";
string destination_state="";
double weight=0;
double cost=0;
}
Package::Package(string d_name, string d_add, string d_zip, string d_city, string d_state, string r_name, string r_add, string r_zip, string r_city, string r_state, double w, double c){
cout<<"Constucting Package Object with user defined values: "<<endl;
string destination_name=d_name;
string destination_address=d_add;
string destination_zip=d_zip;
string destination_city=d_city;
string destination_state=d_state;
double weight=w;
double cost=c;
}
Package::~Package()
{
cout<<"Deconstructing Package Object!"<<endl;
delete Package;
}
double Package::calculateCost(double x, double y){
return x+y;
}
int main(){
double cost=0;
vector<Package*> shipment;
cout<<"Enter Shipping Cost: "<<endl;
cin>>cost;
shipment.push_back(new Package("sarathi","456 bangalore", "944072", "Elctronic", "KJ", cost, 18.5));
shipment.push_back(new Package ("venkatesh","10 Avenue", "46386", "BTM", "CY", cost, 33.3));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.