Question 1
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. 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.

Question 2
Use the Package inheritance hierarchy created in Question 1 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.

Recommended Answers

All 4 Replies

So do you have a real question or are you just another lazy leech who wants other people to do his homework?

this is my code....having too many errors....

#include<string>
#include <iostream>
#include <iomanip>

using namespace std;

// This class Package is the base class for two other classes, TwoDayPackage and OverNightPackage.//

class Package // begins class Package
{
public:
        Package(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double = 0.0, double = 0.0); // constructor

        // set and get functions for sender
        void setSendName(const string &);
        string getSendName() const;

        void setSendAdd(const string &);
        string getSendAdd() const;
        
        void setSendCity(const string &);
        string getSendCity() const;

        void setSendSt(const string &);
        string getSendSt() const;

        void setSendZip(const string &);
        string getSendZip() const;
        
        // set and get functions for recipient
        void setRecName(const string &); 
        string getRecName() const; 

        void setRecAdd(const string &); 
        string getRecAdd() const; 

        void setRecipientCity(const string &); 
        string getRecipientCity() const; 

        void setRecSt(const string &); 
        string getRecSt() const;

        void setRecZip(const string &); 
        string getRecZip() const;
        
        void setWt(double);
        double getWt() const;
        void setShip(double);
        double getShip() const;
        double CalCost() const;

private:
        string sendName;
        string sendAdd;
        string sendCity;
        string sendState;
        string sendZip;
        string recName;
        string recAdd;
        string recCity;
        string recState;
        string recZip;
        double wt;
        double shipCost;
};
Package::Package(const string &sname, const string &saddress, const string &scity, const string &sstate, const string &szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, const string &rzipcode, double wt, double shipCost)
{
        sendName = sname;
        sendAdd = saddress;
        sendCity = scity;
        sendState = sstate;
        sendZip = szipcode;
        recName = rname;
        recAdd = raddress;
        recCity = rcity;
        recState = rstate;
        recZip = rzipcode;
        setWt(wt);
        setShip(shipCost);
}

void Package::setSendName(const string &sname)
{
        sendName = sname;
}

string Package::getSendName() const
{
        return sendName;
}

void Package::setSendAdd(const string &saddress)
{
        sendAdd = saddress;
}

string Package::getSendAdd() const
{
        return sendAdd;
}
void Package::setSendCity(const string &scity)
{
        sendCity = scity;
}

string Package::getSendCity() const
{
        return sendCity;
}

void Package::setSendSt(const string &sstate)
{
        sendState = sstate;
}

string Package::getSendSt() const
{
        return sendState;
}

void Package::setSendZip(const string &szipcode)
{
        sendZip = szipcode;
}

string Package::getSendZip() const
{
        return sendZip;
}

void Package::setRecName(const string &rname)
{
        recName = rname;
}

string Package::getRecName() const
{
        return recName;
}

void Package::setRecAdd(const string &raddress)
{
        recAdd = raddress;
}

string Package::getRecAdd() const
{
        return recAdd;
}

void Package::setRecipientCity(const string &rcity)
{
        recCity = rcity;
}

string Package::getRecipientCity() const
{
        return recCity;
}

void Package::setRecSt(const string &rstate)
{
        recState = rstate;
}

string Package::getRecSt() const
{
        return recState;
}
void Package::setRecZip(const string &rzipcode)
{
        recZip = rzipcode;
}

string Package::getRecZip() const
{
        return recZip;
}

void Package::setWt(double wt)
{
        wt = (wt < 0.0 ) ? 0.0 : wt;
}
double Package::getWt() const
{
        return wt;
}
void Package::setShip(double shipCost)
{
        shipCost = ( shipCost < 0.0) ? 0.0 : shipCost;
}

double Package::getShip() const
{
        return shipCost;
}

double Package::CalCost() const
{

        return wt * shipCost;

}


// This class TwoDayPackage is the first derived class from class Package.//

class TDP : public Package
{
public:
        TDP(const string &, const string &, const string &, const string &, const string &, const string &,
        const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); // constructor
        
        void setFlatFee(double);
        double getFlatFee() const;
        void CalCost() const;

private:
        double flatFee;
};


// This class OverNightPackage is the second derived class from class Package.//

class ONP : public Package
{
public:

        ONP(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double=0.0, double=0.0, double=0.0); // constructor

                void setFee(double);
                double getFee() const;
                void CalCost() const;

private:
        double fee;
};



// This is the test program.//

int main()
{
        ONP box("name", "123 this Street", "boston", "ma", "12345", "receiver", "123 that street", "medford", "ma", "25341", 10.00, 1.50, .85);
        
        TDP parcel("name2", "123 1st Street", "orlando", "fl", "56474", "receiver2", "833 2nd Street", "miami", "fl", "88472", 15.00, 1.05, 5.00);

        cout << fixed << setprecision(2);
        
        cout << "To ship a box with overnight delivery\n";
        cout << "The sender    " << box.getSendName()<< "\n";
        cout << "              " << box.getSendAdd() << "\n";
        cout << "              " << box.getSendCity() << " " << box.getSendSt() << " " << box.getSendZip() << "\n";
        

        cout << "The recipient " << box.getRecName()<< "\n";
        cout << "              " << box.getRecAdd() << "\n";
        cout << "              " << box.getRecipientCity() << " " << box.getRecSt() << " " << box.getRecZip() << "\n";
        cout << "The cost is   $ " <<box.CalCost() << "\n";
        
        cout << "\n\n";

        cout << "To ship a parcel with 2 day delivery\n";
        cout << "The sender    " << parcel.getSendName()<< "\n";
        cout << "              " << parcel.getSendAdd() << "\n";
        cout << "              " << parcel.getSendCity() << " " << parcel.getSendSt() << " " << parcel.getSendZip() << "\n";
        

        cout << "The recipient " << parcel.getRecName()<< "\n";
        cout << "              " << parcel.getRecAdd() << "\n";
        cout << "              " << parcel.getRecipientCity() << " " << parcel.getRecSt() << " " << parcel.getRecZip() << "\n";
        cout << "The cost is   $ "<<parcel.CalCost() << "\n";

system("pause");
return 0;
}

>having too many errors....
I get two compilation errors, both having to do with trying to use the return value of a member function that returns void. Take a look at your CalCost member functions and change the return type to double.

You'll also have linker errors as you've neglected to define the member functions you declare.

Disclaimer: This code should be used as a learning tool. If you decide to use it improperly (cheating), then that's something you'll have to live with. It isn't up to me to decide what's fair and what isn't. If you don't like it, no one is going to force you to use it. If you feel that my posting this enables cheaters, then I urge you to stop worrying so much about how other people spend their time, and worry a little more about getting a life. Happy coding.

#include <iostream> 
#include <string> 
using namespace std; 
 
class Package // Base Class
{ 
private: // data members 
	string name, city, state, sender, recipient; 
    int zip; 
    string address; 
    float weight;  // in ounce 
    double cost;  // per ounce 
                     
public: //member functions 
    void setName(string name); 
    void setCity(string city); 
    void setState(string state); 
    void setZip(int zip); 
    void setAddress(string address); 
    void setSender(string sender); 
    void setRecipient(string recipient); 
    string getName()
	{ 
		return name;
	} 

    string getCity()
	{
		return city;
	} 

    string getState()
	{
		return state;
	} 

	int getZip()
	{
		return zip;
	} 

	string getAddress()
	{
		return address;
	} 
	
	string getSender()
	{
		return sender;
	} 

	string getRecipient()
	{
		return recipient;
	} 
                                
double calculateCost(float weight,double costPerOunce) //function that calculate the cost z 
{  
	double z;  //total cost = weight*cost per ounce 
    z = weight * costPerOunce;  //the cost z 
                                  
    cout<< "The Base Cost = " <<z << endl<<endl; 
    return z; 
}
}; // end class Package
 
void Package::setName(string n)
{
	name = n; 
} 
                                                    
void Package::setCity(string c)
{
	city = c;
} 

void Package::setState(string s)
{
	state = s;
} 

void Package::setZip (int zp)
{
	zip = zp;
} 

void Package::setAddress(string adr)
{
	address = adr; 
} 

void Package::setSender(string sen) 
{
	sender = sen;   
}  

void Package::setRecipient(string rec)   
{
	recipient = rec;                                             
}                                         
          
class TwoDayPackage: public Package // derived class
{     
public:
	double calcShippingCost(float weight, double costPerOunce, double flatFee) 
		/* function that calculate shipping cost by adding the flat fee to the weight-based cost 
		calculated by base class Package’s calculateCost function*/ 
	{
		double z; //shippingcost of Two day Package class
		
		z= calculateCost(weight,costPerOunce) + flatFee;  
		cout<< "The TwoDayPackage Cost = " <<z << endl;

		return z;
	}

private:
	double flatFee;
}; // end TwoDayPackage
 
class OvernightPackage: public Package //derived class that adds the additional fee per ounce
{
public:
	double calcCostOvernight(float weight,double costPerOunce,double additionalCost )
	{
		double z; //shippingcost of overnight class
		z = calculateCost(weight, costPerOunce)+(additionalCost * weight);
		
		cout<< "The OvernightPackage Cost = " <<z << endl;

		return z;
	}
private:
	double overnightCost; //per ounce 
}; // end class OvernightPackage
 
int main()
{
	int i;  //i represent the user`s choice number
	string customerName,customerAddress,city,state,senderAddress,recipientAddress; 
    float packageWeight; 
    string customerCity; 
    double costPerOunce; 
    double flatFee; 
    double additionalCost; 
    string customerState; 
    int customerZipcode;
	
	Package base;   //the object base of the package class 
    TwoDayPackage twoday;  //the object twoday of the first inhereted calss 
    OvernightPackage overnight;   //the object overnight of the second inhereted calss   
 
    cout<<"    *****Welcome To The American Package Delievery Services*****"<<endl<<endl; 
    cout<<"Please Fill In The Requested Data Follow:        "<<endl<<"-----------------------------------------"<<endl<<endl;; 
         
    cout<<"Enter Customer Name "<<endl<<endl; 
    cin>>customerName; 
    cout<<endl; 
    base.setName(customerName);  //call function setName 
         
    cout<<"Enter Customer Address"<<endl<<endl; 
    cin>>customerAddress; 
    cout<<endl; 
    base.setAddress(customerAddress); 
 
    cout<<"Enter Customer City"<<endl<<endl; 
    cin>>customerCity; 
    cout<<endl; 
    base.setCity(customerCity); 
         
    cout<<"Enter Customer State"<<endl<<endl; 
    cin>>customerState; 
    cout<<endl; 
    base.setState(customerState); 
         
    cout<<"Enter Customer ZIP code"<<endl<<endl; 
    cin>>customerZipcode; 
    cout<<endl; 
    base.setZip(customerZipcode); 
         
    cout<<"Enter Weight"<<endl; 
    cin>>packageWeight; 
    cout<<endl; 
    cout<<"Enter Cost Per Ounce"<<endl; 
    cin>>costPerOunce; 
    cout<<endl; 
         
    cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl; 
    cout<<"   1- Calculate Base Cost  "<<endl<<endl; 
    cout<<"   2- Calculate Two Day Cost "<<endl<<endl; 
    cout<<"   3- Calculate Over Night Cost"<<endl<<endl; 
    cin>>i; 
    cout<<endl;  //i represent customer choice
	
	switch (i)
	{
	case 1 :
		base.calculateCost(packageWeight,costPerOunce);
		break;
	
	case 2 : 
		cout<<"Enter Flat Cost"<<endl<<endl;  //additonal(to weight and cost) needed information  
        cin>>flatFee;
		twoday.calcShippingCost(packageWeight,costPerOunce,flatFee); 
        break;
	
	case 3 : 
		cout<<"Enter The Additional Cost"<<endl<<endl; 
        cin>>additionalCost; 
        overnight.calcCostOvernight(packageWeight,costPerOunce,additionalCost); 
        break;
	
	default:
		cout<<"INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
	} // end switch
	
	cout<<"Enter sender address "<<endl<<endl; 
    cin>>senderAddress; 
    cout<<endl; 
    base.setSender( senderAddress); 
         
    cout<<"Enter ricipent address"<<endl<<endl; 
    cin>>recipientAddress; 
    cout<<endl; 
    base.setRecipient(recipientAddress);    
         
    cout<<"address from:"<< senderAddress<<endl; 
    cout<<"To:"<<recipientAddress<<endl;
	
	system ("pause");
	return 0;
} // end main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.