The question is (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. 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.

i have written the code but get some errors.Can someone compile and correct this and tell me why it happens?

#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;
}
Member Avatar for iamthwee

> Can someone compile and correct this and tell me why it happens?

This sentence shocks me, because it suggests you have written all that in one go and then just tried to compile/test it.

Lesson one, compile and test your code periodically.

If you have, then break the program up, test each bit at a time.

I did compile by parts but i can't just get going with the CalCost() function.I think i am commiting mistake there.

I asked someone to compile to have a better idea.

I did compile by parts but i can't just get going with the CalCost() function.I think i am commiting mistake there.

Comment out everything but the "return 0" line in main, see if it compiles. If it does, start uncommenting lines one by one till it doesn't compile, then look at the first line that doesn't compile. Yes you have a mistake in CalCost:

Line 50 - CalCost returns a double.
Line 198 - returns a double (so far, so good).
Line 216 - subclass returns a void?
Line 233 - subclass returns a void?

Lines 260, 273 - looks like the function calls DO NOT want a void function.

Decide what the CalCost function(s) should return (double or void).

I di what you said.The first line itself in main gives linking error.Also i changed the subclass to return double.

I di what you said.The first line itself in main gives linking error.Also i changed the subclass to return double.

Lines 211, 229 - Where are the constructors that correspond to these constructor prototypes?

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.