Package Inheritance Hierarchy using C++
Posted by Samath
Last Updated: January 07, 2021
  15855

(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 Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Baseclass 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 calculate-Cost 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.

This Package Inheritance Hierarchy program is written using the c++ programming language. 

Code:

#include <iostream>
#include <string>
#include <iomanip>
 
using namespace std;
 
class Package
{
	private:
		string sender_name;
		string sender_address;
		string sender_city;
		string sender_state;
		string sender_ZIP;
		
		string recipient_name;
		string recipient_address;
		string recipient_city;
		string recipient_state;
		string recipient_ZIP;
		
		double weight;
		double costperounce;
	public:
 
 
		Package(string sender_n, string sender_addr, string sender_c,
 string sender_s, string sender_Z, string recipient_n, string recipient_addr,
string recipient_c,string recipient_s, string recipient_Z, double wei, 
double cost);
		
		 void setsender_name(string sender_n);
    string getsender_name();
 
    void setsender_address(string sender_addr);
    string getsender_address();
 
    void setsender_city(string sender_c);
    string getSendCity();
 
    void setsender_state(string sender_s);
    string getsender_state();
 
    void setsender_ZIP(string sender_Z);
    string getsender_ZIP();
    
    void setrecipient_name(string recipient_n);
    string getrecipient_name();
 
    void setrecipient_address(string recipient_addr);
    string getrecipient_address();
 
    void setrecipient_city(string recipient_c);
    string getrecipient_city();
 
    void setrecipient_state(string recipient_s);
    string getrecipient_state();
 
    void setrecipient_ZIP(string recipient_Z);
    string getrecipient_ZIP();
 
    void setweight(double w);
    double getweight();
 
    void setcostperounce(double cost);
    double getcostperounce();
 
    double calculateCost();
};
 
Package::Package(string sender_n, string sender_addr, string sender_c, string 
sender_s, string sender_Z, string recipient_n, string recipient_addr,string 
recipient_c,string recipient_s, string recipient_Z, double wei, double cost)
{	
		sender_name = sender_n;
		sender_address = sender_addr;
		sender_city = sender_c;
		sender_state = sender_s;
		sender_ZIP = sender_Z;
		
		recipient_name = recipient_n;
		 recipient_address = recipient_addr;
		recipient_city = recipient_c;
		recipient_state = recipient_s;
		recipient_ZIP = recipient_Z;
		
	if(wei > 0.0 && cost > 0.0)
	{
		weight = wei;
		costperounce = cost;
	}
	else
	{
		weight = 0.0;
		costperounce = 0.0;
	}	
	
}
 
void Package::setsender_name(string sender_n)
{
	sender_name = sender_n;
}
string Package::getsender_name()
{
	return sender_name;
}
    
void Package::setsender_address(string sender_addr)
{
	sender_address = sender_addr;
}
string Package::getsender_address()
{
	return sender_address;
}
 
void Package::setsender_city(string sender_c)
{
	sender_city = sender_c;
}
 
string Package::getSendCity()
{
	return sender_city;
}
 
void Package::setsender_state(string sender_s)
{
	sender_state = sender_s;
}
string Package::getsender_state()
{
	return sender_state;
}
 
void Package::setsender_ZIP(string sender_Z)
{
	sender_ZIP = sender_Z;
}
string Package::getsender_ZIP()
{
	return sender_ZIP;
}
    
void Package::setrecipient_name(string recipient_n)
{
	recipient_name = recipient_n;
}
string Package::getrecipient_name()
{
	return recipient_name;
}
 
void Package::setrecipient_address(string recipient_addr)
{
	recipient_address = recipient_addr;
}
string Package::getrecipient_address()
{
	return recipient_address;
}
 
void Package::setrecipient_city(string recipient_c)
{
	recipient_city = recipient_c;
}
string Package::getrecipient_city()
{
	return recipient_city;
}
 
void Package::setrecipient_state(string recipient_s)
{
	recipient_state = recipient_s;
}
string Package::getrecipient_state()
{
	return recipient_state;
}
 
void Package::setrecipient_ZIP(string recipient_Z)
{
	recipient_ZIP = recipient_Z;
}
string Package::getrecipient_ZIP()
{
	return recipient_ZIP;
}
 
void Package::setweight(double w)
{
	 weight = (w < 0.0 ) ? 0.0 : w;
}
double Package::getweight()
{
	return weight;
}
 
void Package::setcostperounce(double cost)
{
	costperounce = ( cost < 0.0) ? 0.0 : cost;
}
 
double Package::getcostperounce()
{
	return costperounce;
}
 
double Package::calculateCost()
{
	double result;
	
	result = weight * costperounce;
	
	return result;
}
 
 
class TwoDayPackage : public Package
{
	private:
		double two_day_delivery_fee;
	public:
		TwoDayPackage(string sender_n, string sender_addr, string 
sender_c, string sender_s, string sender_Z, string recipient_n, 
string recipient_addr,string recipient_c,string recipient_s, 
string recipient_Z,double wei, double cost, double delivery_fee);
		
		double gettwo_day_delivery_fee();
		void settwo_day_delivery_fee(double delivery_fee);
		double calculateCost();
};
 
TwoDayPackage::TwoDayPackage(string sender_n, string sender_addr, 
string sender_c, string sender_s, string sender_Z, string recipient_n, 
string recipient_addr,string recipient_c,string recipient_s, 
string recipient_Z, double wei, double cost, double delivery_fee)
:Package(sender_n, sender_addr, sender_c, sender_s, sender_Z, recipient_n, 
recipient_addr, recipient_c, recipient_s, recipient_Z,wei,cost)
{
	    settwo_day_delivery_fee(delivery_fee);	
	
}
 
 
double TwoDayPackage::gettwo_day_delivery_fee()
{
	return two_day_delivery_fee;
}
void TwoDayPackage::settwo_day_delivery_fee(double delivery_fee)
{
	two_day_delivery_fee = delivery_fee;
}
 
double TwoDayPackage::calculateCost()
{
	double result;
	result = Package::calculateCost() + two_day_delivery_fee;
	return result;
}
 
class OvernightPackage : public Package
{
	private:
		double overnight_delivery_fee;
	public:
	
OvernightPackage(string sender_n, string sender_addr, string sender_c, 
string sender_s, string sender_Z, string recipient_n, string recipient_addr,
string recipient_c,string recipient_s, string recipient_Z, double wei, 
double cost, double delivery_fee);
	
double calculateCost();
double getovernight_delivery_fee();
void setovernight_delivery_fee(double delivery_fee);
	
};
 
OvernightPackage::OvernightPackage(string sender_n, string sender_addr, 
string sender_c, string sender_s, string sender_Z, string recipient_n, 
string recipient_addr,string recipient_c,string recipient_s, 
string recipient_Z, double wei, double cost, double delivery_fee)
:Package(sender_n, sender_addr, sender_c, sender_s, sender_Z, recipient_n, 
recipient_addr, recipient_c, recipient_s, recipient_Z,wei,cost)
{
	    setovernight_delivery_fee(delivery_fee);
}
 
double OvernightPackage::getovernight_delivery_fee()
{
	return overnight_delivery_fee;
}
void OvernightPackage::setovernight_delivery_fee(double delivery_fee)
{
	overnight_delivery_fee = delivery_fee;
}
 
double OvernightPackage::calculateCost()
{
	double result;
	result =  (getcostperounce() + overnight_delivery_fee) * getweight();
	return result;
}
 
int main(int argc, char *argv[])
{
	OvernightPackage item1("Tom Brown", "123 Main Street", "Phoenix", 
"Arizona", "89754", "John", "123 bent street", "Hartford", "Connecticut", 
"87540", 12.00, 1.50, 1.10);
	TwoDayPackage item2("Monique Smith", "987 1st Street", "Sacramento", 
"California", "87654", "Paul", "833 palm Street", "Miami", "Florida", 
"98763", 18.00, 1.05, 8.00);
 
    cout << fixed << setprecision(2);
    cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
    cout << "Overnight Delivery\n";
    cout << "Sender        " << item1.getsender_name()<< "\n";
    cout << "              " << item1.getsender_address() << "\n";
    cout << "              " << item1.getSendCity() << " " << 
item1.getsender_state() << " " << item1.getsender_ZIP() << "\n";
     cout << "\n";
    cout << "Recipient     " << item1.getrecipient_name()<< "\n";
    cout << "              " << item1.getsender_address() << "\n";
    cout << "              " << item1.getrecipient_city() << " " <<
 item1.getrecipient_state() << " " << item1.getrecipient_ZIP() << "\n";
    cout << "Cost          $ " <<item1.calculateCost() <<"\n";
     cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
 
    cout << "\n\n";
    cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
    cout << "2 Day Delivery\n";
    cout << "Sender        " << item2.getsender_name()<< "\n";
    cout << "              " << item2.getsender_address() << "\n";
    cout << "              " << item2.getSendCity() << " " << 
item2.getsender_state() << " " << item2.getsender_ZIP() << "\n";
     cout << "\n";
    cout << "Recipient     " << item2.getrecipient_name()<< "\n";
    cout << "              " << item2.getsender_address() << "\n";
    cout << "              " << item2.getrecipient_city() << " " << 
item2.getrecipient_state() << " " << item2.getrecipient_ZIP() << "\n";
    cout << "Cost          $ " <<item2.calculateCost()<<"\n";
	cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";	
 
	system("pause");
	return 0;
}
   
  
 
 
   

 
 
how du i post code on this site, m trying to put screenshots nd code.
 
go to blog and click the link create a new blog. you can create blogs, post code or tutorial from there.