3.5k views
5 votes
In this lab, you will build a system for package delivery services that provides different shipping options with specific price. You are required to create an inheritance hierarchy to represent the various types of packages. Use Package as the base class of the hierarchy and include classes TwoDaypackage and OvernightPackage that derive from Package. The 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, and data members that store the weight (in ounces) and cost per ounce to ship the package. The constructor of the class Package should initialize these data members. The weight and the cost per ounce should contain positive values. The class Package should provide a public member function calculateCost that returns a double indicating the cost associated with shipping the package. The calculateCost function in class Package should determine the cost by multiplying the weight by the cost per ounce. The derived class TwoDayPackage should not only inherit the functionality of the base class Package, but also include a data member that represents a flat fee per ounce that the shipping company charges for two-day delivery. The constructor of the class TwoDayPackage should receive a value to initialize this data member. Class TwoDayPackage should redefine member function calculateCost so that it computes the shipping cost by adding a flat fee per ounce to the standard cost per ounce calculated by the calculateCost function in the base class Package. The requirements of the derived class OvernightPackage are similar as that of the class TwoDayPackage. Specifically, class OvernightPackage should inherit directly from class Package and contain an additional data member representing an additional flat fee per ounce charged for overnight delivery. Class OvernightPackage should redefine member function calculateCost so that it adds the additional fee per ounce to the standard cost per ounce when calculating the shipping cost.

User NCore
by
4.7k points

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

#include <iostream>

#include <string.h>

using namespace st d;

class Package

{

protected:

//sender properities

char sname[10],saddress[50],scity[20],sstate[20];

long int szip;

//Receiver properities

char rname[10],raddress[50],rcity[20],rstate[20];

long int rzip;

//additonal datamembers

float weight,cost;

public:

Package() //Default constructor

{

strcpy(sname,"");strcpy(saddress,"");strcpy(scity,"");

strcpy(sstate,"");szip=0;

strcpy(rname,"");strcpy(raddress,"");strcpy(rcity,"");

strcpy(rstate,"");rzip=0;

weight=cost=0;

}

Package(char sn[10],char sa[50],char sc[20],char ss[20],long int sz,char rn[10],char ra[50],char rc[20],char rs[20],long int rz,float w,float c) //Constructor with parameter

{

strcpy(sname,sn);strcpy(saddress,sa);strcpy(scity,sc); //copying each parameter value to data member

strcpy(sstate,ss);szip=0;

strcpy(rname,rn);strcpy(raddress,ra);strcpy(rcity,rc);

strcpy(rstate,rs);rzip=0;

weight=w;cost=c;

}

float calculateCost() //calculating cost

{

return weight*cost;

}

};

class TwoDayPackage:public Package //Inheritance

{

float flatfee;

public:

TwoDayPackage(char sn[10],char sa[50],char sc[20],char ss[20],long int sz,char rn[10],char ra[50],char rc[20],char rs[20],long int rz,float w,float c,float ff) //Constructor with parameter

{

Package(sn,sa,sc,ss,sz,rn,ra,rc,rs,rz,w,c); //calling super class constructor

flatfee=ff; //copy last parameter

}

float calculateCost() //redefined calculateCost function

{

return (weight*cost)+flatfee; //return total cost

}

};

class OverNightPackage:public Package //Inheritance

{

float addfee;

public:

OverNightPackage(char sn[10],char sa[50],char sc[20],char ss[20],long int sz,char rn[10],char ra[50],char rc[20],char rs[20],long int rz,float w,float c,float a f) //constructor with parameter

{

Package(sn,sa,sc,ss,sz,rn,ra,rc,rs,rz,w,c); //calling super class constructor

addfee=a f; //copy last parameter

}

float calculateCost() //redefined calculateCost function

{

return (weight*cost)+addfee; //calculating total cost

}

};

int main()

{

Package package1("lou brown","1 main st","boston","ma",11111,"mary smith","7 elm st","new york","ny",22222,8.5,0.5); //creating objects

TwoDayPackage package2("lisa klein", "5 broadway", "somerville", "ma", 33333, "bob george", "21 pine rd", "cambridge", "ma", 44444, 10.5, .65, 2.0 );

OverNightPackage package3 ("ed lewis", "2 oak st", "boston", "ma", 55555, "don kelly", "9 main st", "denver", "co", 66666, 12.25, .7, .25 );

cout<<"\\Package Cost : "<<package1.calculateCost(); //calling each cost

cout<<"\\TwoDay Package Cost : "<<package2.calculateCost();

cout<<"\\Overnight Package Cost : "<<package3.calculateCost();

return 0;

}

User Dragonore
by
4.0k points