Thread: Inheritance Hierarchy for a Package class

  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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use code tags around your code. Indent.
    Code:
    cout << "The cost is $ " << box.CalculateCost << "\n";
    Missing parenthesis there. That takes the address of the function (should actually be &box.CalculateCost to be standards compliant).
    CalculateCost returns void which can't be output.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    Thanks for the quick response, but how do I get the objects to fill the constructors properly? I still get a compiler error that looks like this:

    unresolved external symbol "public: __thiscall TwoDayPackage::TwoDayPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double,double,double)" (??0TwoDayPackage@@QAE@ABV?$basic_string@DU?$char_ traits@D@std@@V?$allocator@D@2@@std@@000000000NNN@ Z) referenced in function _main

    thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Put code tags around your first post (the code) and indent it properly so we can see (it may be that the forum is truncating those tabs or spaces because you haven't put code tags around).
    Do you define your classes anywhere? Because I can't see them.
    If not, then there's your problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Attempted fix

    Does that look better?

    I am a bit clumsy and not accustomed to how the code tags work yet.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yep, like that.
    You can also wrap some lines if they're too long because code in code tags won't wrap, requiring everyone to scroll.
    But anyway, as I mentioned, TwoDayPackage class's functions are not defined anywhere I see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Steps to fix?

    I take it I need to add some blocks that look like this:

    Code:
    TwoDayPackage::setFlatFee()
    {
    	//some code
    }
    TwoDayPackage::getFlatFee()
    {
    	//some code
    }
    double TwoDayPackage::CalculateCost() const
    {
    	//some code
    
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes you do.
    If you add declarations for function but never define them when calling them, you get a linking error because the linker is trying to find the address of your functions and generate code to call it but never finds them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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