Thread: Inheritance Hierarchy for a Package class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Inheritance Hierarchy for a Package class

    Hopefully someone may be able to get me straightened out...
    I have been working this program for about two days now. I have tried to follow the examples in my text book, but after several attempts I guess I can't see the forest through the tree. My object instatiatiations are messed up some how so I have commented out several statements so it will compile. I have included the requirements of how it is supposed to work in the comments. Thanks in advance.....

    Code:
    #include "stdafx.h"
    #include<string>
    #include <iostream>
    
    using std::string;
    using namespace std;
    using std::fixed;
    //using std::setprecision; -- this not being recognized for some reason so I commented it out for now
    
    
    /* This class Package is the base class for two other classes, TwoDayPackage and OverNightPackage.
    /  It should contain data members for name, address, city, state, zipcode for both a sender and a recipient.
    /  And data members for weight, and ship cost per ounce.
    /  The constructor should initialize these members.
    /  A public member function to calculateCost should be provided and perform : cost = weight * shipCost. */
    
    
    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 setSenderName(const string &);
    	string getSenderName() const;
    
    	void setSenderAddress(const string &);
    	string getSenderAddress() const;
    	
    	void setSenderCity(const string &);
    	string getSenderCity() const;
    
    	void setSenderState(const string &);
    	string getSenderState() const;
    
    	void setSenderZipcode(const string &);
    	string getSenderZipcode() const;
    	
    	// set and get functions for recipient
    	void setRecipientName(const string &); 
    	string getRecipientName() const; 
    
    	void setRecipientAddress(const string &); 
    	string getRecipientAddress() const; 
    
    	void setRecipientCity(const string &); 
    	string getRecipientCity() const; 
    
    	void setRecipientState(const string &); 
    	string getRecipientState() const;
    
    	void setRecipientZipcode(const string &); 
    	string getRecipientZipcode() const;
    	
    	void setWeight(double);
    	double getWeight() const;
    	void setShipCost(double);
    	double getShipCost() const;
    	double CalculateCost() const;
    
    protected:
    	string senderName;
    	string senderAddress;
    	string senderCity;
    	string senderState;
    	string senderZipcode;
    	string recipientName;
    	string recipientAddress;
    	string recipientCity;
    	string recipientState;
    	string recipientZipcode;
    	double weight;
    	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 weight, double shipCost)
    {
    	senderName = sname;
    	senderAddress = saddress;
    	senderCity = scity;
    	senderState = sstate;
    	senderZipcode = szipcode;
    	recipientName = rname;
    	recipientAddress = raddress;
    	recipientCity = rcity;
    	recipientState = rstate;
    	recipientZipcode = rzipcode;
    	setWeight(weight);
    	setShipCost(shipCost);
    }
    
    void Package::setSenderName(const string &sname)
    {
    	senderName = sname;
    }
    
    string Package::getSenderName() const
    {
    	return senderName;
    }
    
    void Package::setSenderAddress(const string &saddress)
    {
    	senderAddress = saddress;
    }
    
    string Package::getSenderAddress() const
    {
    	return senderAddress;
    }
    void Package::setSenderCity(const string &scity)
    {
    	senderCity = scity;
    }
    
    string Package::getSenderCity() const
    {
    	return senderCity;
    }
    
    void Package::setSenderState(const string &sstate)
    {
    	senderState = sstate;
    }
    
    string Package::getSenderState() const
    {
    	return senderState;
    }
    
    void Package::setSenderZipcode(const string &szipcode)
    {
    	senderZipcode = szipcode;
    }
    
    string Package::getSenderZipcode() const
    {
    	return senderZipcode;
    }
    
    void Package::setRecipientName(const string &rname)
    {
    	recipientName = rname;
    }
    
    string Package::getRecipientName() const
    {
    	return recipientName;
    }
    
    void Package::setRecipientAddress(const string &raddress)
    {
    	recipientAddress = raddress;
    }
    
    string Package::getRecipientAddress() const
    {
    	return recipientAddress;
    }
    
    void Package::setRecipientCity(const string &rcity)
    {
    	recipientCity = rcity;
    }
    
    string Package::getRecipientCity() const
    {
    	return recipientCity;
    }
    
    void Package::setRecipientState(const string &rstate)
    {
    	recipientState = rstate;
    }
    
    string Package::getRecipientState() const
    {
    	return recipientState;
    }
    void Package::setRecipientZipcode(const string &rzipcode)
    {
    	recipientZipcode = rzipcode;
    }
    
    string Package::getRecipientZipcode() const
    {
    	return recipientZipcode;
    }
    
    void Package::setWeight(double weight)
    {
    	weight = (weight < 0.0 ) ? 0.0 : weight;
    }
    double Package::getWeight() const
    {
    	return weight;
    }
    void Package::setShipCost(double shipCost)
    {
    	shipCost = ( shipCost < 0.0) ? 0.0 : shipCost;
    }
    
    double Package::getShipCost() const
    {
    	return shipCost;
    }
    
    double Package::CalculateCost() const
    {
    
    	return weight * shipCost;
    
    }
    
    
    /* This class TwoDayPackage is the first derived class from class Package.
    /  It should contain a data member flatFee, to reflect an additional cost.
    /  The constructor should recieve a value to initialize this fee and redefine the 
    /  function CalculateCost so that it adds the fee to the function's calculation. */
    
    
    class TwoDayPackage : public Package
    {
    public:
    	TwoDayPackage(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 CalculateCost() const;
    
    private:
    	double flatFee;
    };
    
    
    /* This class OverNightPackage is the second derived class from class Package.
    /  It should contain a data member extraFee, to reflect an additional cost per ounce.
    /  The constructor should redefine the function CalculateCost so that 
    /  it adds the fee to the standard cost per ounce before the function's calculation. */
    
    
    class OverNightPackage : public Package
    {
    public:
    
    	OverNightPackage(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double, double, double); // constructor
    
    		void setExtraFee(double);
    		double getExtraFee() const;
    		void CalculateCost() const;
    
    private:
    	double extraFee;
    };
    
    
    
    /* This is the test program.
    /  It should create objects of each type of Package and test member function CalculateCost. */
    
    int main()
    {
    	//OverNightPackage box("J. Jones", "123 Oak Street", "El Paso", "TX", "12345", "S. Smith", "321 Elm Street", "Tulsa", "OK", "54321", 16.00, 1.00, .60);
    	//TwoDayPackage parcel("A. Anderson", "456 Maple Street", "Las Vegas", "NV", "67890", "J. Johnson", "654 Ash Street", "Tuscon", "AZ", "09876", 24.00, .75, 4.00);
    	//cout << fixed << setprecision(2);
    	
    	cout << "To ship a box with overnight delivery\n";
    	//cout << "The sender    " << box.getSenderName()<< "\n";
    	//cout << "              " << box.getSenderAddress() << "\n";
    	//cout << "              " << box.getSenderCity() << " " << box.getSenderState() << " " << box.getSenderZipcode() << "\n";
    	
    
    	//cout << "The recipient " << box.getRecipientName()<< "\n";
    	//cout << "              " << box.getRecipientAddress() << "\n";
    	//cout << "              " << box.getRecipientCity() << " " << box.getRecipientState() << " " << box.getRecipientZipcode() << "\n";
    	//cout << "The cost is   $ " << box.CalculateCost << "\n";
    	
    	cout << "\n\n";
    
    	cout << "To ship a parcel with 2 day delivery\n";
    	//cout << "The sender    " << parcel.getSenderName()<< "\n";
    	//cout << "              " << parcel.getSenderAddress() << "\n";
    	//cout << "              " << parcel.getSenderCity() << " " << parcel.getSenderState() << " " << parcel.getSenderZipcode() << "\n";
    	
    
    	//cout << "The recipient " << parcel.getRecipientName()<< "\n";
    	//cout << "              " << parcel.getRecipientAddress() << "\n";
    	//cout << "              " << parcel.getRecipientCity() << " " << parcel.getRecipientState() << " " << parcel.getRecipientZipcode() << "\n";
    	//cout << "The cost is   $ " << parcel.CalculateCost << "\n";
    
    
    }
    Last edited by twickre; 12-08-2007 at 03:37 PM. Reason: Code Tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM