User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,223 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,272 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 324 | Replies: 17
Reply
Join Date: Jul 2008
Posts: 9
Reputation: chaoticabyss99 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chaoticabyss99 chaoticabyss99 is offline Offline
Newbie Poster

Re: Inheritance Hierarchy

  #11  
Jul 18th, 2008
Originally Posted by niek_e View Post
What errors? Post your new code, I'm not in the mood to stare into my crystal ball to locate your problem


Package.h

#ifndef Package_H
#define Package_H

#include <iostream>
#include <string>

using namespace std;

//The class Package is the base class for derived 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, 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 setSenderZip(const string &);  
    string getSenderZip() 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 setRecipientZip(const string &);   
    string getRecipientZip() const;  
      
    void setWeight(double);  
    double getWeight() const;  
    void setShip(double);  
    double getShip() const;  
    double calculateCost() const;  
  
private:  
    string senderName;  
    string senderAddress;  
    string senderCity;  
    string senderState;  
    string senderZip;  
    string recipientName;  
    string recipientAddress;  
    string recipientCity;  
    string recipientState;  
    string recipientZip;  
    double weight;  
    double shipCost;  
};

#endif

Package.cpp

#include <iostream>
#include <string>
using namespace std;

#include "Package.h"


Package::Package(const string & sname, const string & saddress, const string & scity, const string & sstate, const string & szip, const string & rname, const string & raddress, const string & rcity, const string & rstate, const string & rzip, double weight, double shipCost, double calculateCost)  
{  
    senderName = sname;  
    senderAddress = saddress;  
    senderCity = scity;  
    senderState = sstate;  
    senderZip = szip;  
    recipientName = rname;  
    recipientAddress = raddress;  
    recipientCity = rcity;  
    recipientState = rstate;  
    recipientZip = rzip;  
    setWeight(weight);  
    setShip(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::setSenderZip(const string & szip)  
{  
    senderZip = szip;  
}  
  
string Package::getSenderZip() const  
{  
    return senderZip;  
}  
  
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::setRecipientZip(const string & rzip)  
{  
    recipientZip = rzip;  
}  
  
string Package::getRecipientZip() const  
{  
    return recipientZip;  
}  
  
void Package::setWeight(double weight)  
{  
    weight = (weight < 0.0 ) ? 0.0 : weight;  
}  
double Package::getWeight() const  
{  
    return weight;  
}  
void Package::setShip(double shipCost)  
{  
    shipCost = ( shipCost < 0.0) ? 0.0 : shipCost;  
}  
  
double Package::getShip() const  
{  
    return shipCost;  
}  
  
double Package::calculateCost() const  
{    
    return weight * shipCost;  
}  


//The class TwoDayPackage is the first derived class from class Package
  
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;
};  
  
  
//The class OverNightPackage is the second derived class from class Package
  
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 = 0.0, double = 0.0, double = 0.0); //constructor  
  
        void setFee(double);  
        double getFee() const;
		//void calculateCost() const;
  
private:  
    double fee;
};  

PackageTest.cpp

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>

using namespace std;
using std::setprecision;

#include "Package.cpp"


//Test File
  
int main()  
{  
    OverNightPackage box("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 10.00, 1.50, .85);  
      
    TwoDayPackage parcel("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 15.00, 1.05, 5.00);  
  
    cout << fixed << setprecision(2);  
      
	cout << "To ship a box with overnight delivery:\n"
         << "\nThe sender    " << box.getSenderName()  
         << "\n              " << box.getSenderAddress()
         << "\n              " << box.getSenderCity() << ", " << box.getSenderState() << "  " << box.getSenderZip() 
      
  
         << "\nThe recipient   " << box.getRecipientName()  
         << "\n                " << box.getRecipientAddress()
         << "\n                " << box.getRecipientCity() << ", " << box.getRecipientState() << "  " << box.getRecipientZip()  
         << "\nThe cost is   $ " << box.calculateCost()
      
      
         << "\n\n\n\nTo ship a parcel with 2 day delivery:\n"
         << "\nThe sender    " << parcel.getSenderName()  
         << "\n              " << parcel.getSenderAddress()
         << "\n              " << parcel.getSenderCity() << ", " << parcel.getSenderState() << "  " << parcel.getSenderZip()  
      
  
         << "\nThe recipient " << parcel.getRecipientName() 
         << "\n              " << parcel.getRecipientAddress()
         << "\n              " << parcel.getRecipientCity() << ", " << parcel.getRecipientState() << "  " << parcel.getRecipientZip()
         << "\nThe cost is   $ "<< parcel.calculateCost() << endl;  
 
	_getch();

}

I'm using VC++ 2008 Express Edition; if that matters.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Inheritance Hierarchy

  #12  
Jul 18th, 2008
You use calculateCost as a function, but also as a variable to the constructor in this line:

Package::Package(const string & sname, const string & saddress, const string & scity, const string & sstate, const string & szip, const string & rname, const string & raddress, const string & rcity, const string & rstate, const string & rzip, double weight, double shipCost, double calculateCost)
So that would cause some problems.

Also this line:
#include "Package.cpp" (in PackageTest.cpp) is wrong. I think you meant: #include "Package.h"
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Jul 2008
Posts: 9
Reputation: chaoticabyss99 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chaoticabyss99 chaoticabyss99 is offline Offline
Newbie Poster

Re: Inheritance Hierarchy

  #13  
Jul 18th, 2008
Originally Posted by niek_e View Post
You use calculateCost as a function, but also as a variable to the constructor in this line:

Package::Package(const string & sname, const string & saddress, const string & scity, const string & sstate, const string & szip, const string & rname, const string & raddress, const string & rcity, const string & rstate, const string & rzip, double weight, double shipCost, double calculateCost)
So that would cause some problems.

Also this line:
#include "Package.cpp" (in PackageTest.cpp) is wrong. I think you meant: #include "Package.h"


So, do I need to get rid of the variable in Package:ackage or the function calculateCost??
And when I use #include "Package.h" in the PackageTest.cpp file, instead of #include "Package.cpp", I get 50 errors.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Inheritance Hierarchy

  #14  
Jul 18th, 2008
Originally Posted by chaoticabyss99 View Post
So, do I need to get rid of the variable in Package:ackage or the function calculateCost??
Well, you're not doing anything with it right?

Originally Posted by chaoticabyss99 View Post
And when I use #include "Package.h" in the PackageTest.cpp file, instead of #include "Package.cpp", I get 50 errors.


*sigh*. Wack this code: (from package.cpp)
  1. //The class TwoDayPackage is the first derived class from class Package
  2.  
  3. class TwoDayPackage : public Package
  4. {
  5. public:
  6. TwoDayPackage(const string &, const string &, const string &, const string &, const string &, const string &,
  7. const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
  8.  
  9. void setFlatFee(double);
  10. double getFlatFee() const;
  11. //void calculateCost() const;
  12.  
  13. private:
  14. double flatFee;
  15. };
  16.  
  17.  
  18. //The class OverNightPackage is the second derived class from class Package
  19.  
  20. class OverNightPackage : public Package
  21. {
  22. public:
  23. OverNightPackage(const string &, const string &, const string &, const string &, const string &, const string &,
  24. const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
  25.  
  26. void setFee(double);
  27. double getFee() const;
  28. //void calculateCost() const;
  29.  
  30. private:
  31. double fee;
  32. };
in the header file (package.h). The header file is where the function-declaration should be.
Now :
- add default constructors/destructors
- write the function-defenition for the 2 derived classes. You have only made the declaration, but they don't do anything yet.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Jul 2008
Posts: 9
Reputation: chaoticabyss99 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chaoticabyss99 chaoticabyss99 is offline Offline
Newbie Poster

Re: Inheritance Hierarchy

  #15  
Jul 18th, 2008
Originally Posted by niek_e View Post
Well, you're not doing anything with it right?



*sigh*. Wack this code: (from package.cpp)
  1. //The class TwoDayPackage is the first derived class from class Package
  2.  
  3. class TwoDayPackage : public Package
  4. {
  5. public:
  6. TwoDayPackage(const string &, const string &, const string &, const string &, const string &, const string &,
  7. const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
  8.  
  9. void setFlatFee(double);
  10. double getFlatFee() const;
  11. //void calculateCost() const;
  12.  
  13. private:
  14. double flatFee;
  15. };
  16.  
  17.  
  18. //The class OverNightPackage is the second derived class from class Package
  19.  
  20. class OverNightPackage : public Package
  21. {
  22. public:
  23. OverNightPackage(const string &, const string &, const string &, const string &, const string &, const string &,
  24. const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
  25.  
  26. void setFee(double);
  27. double getFee() const;
  28. //void calculateCost() const;
  29.  
  30. private:
  31. double fee;
  32. };
in the header file (package.h). The header file is where the function-declaration should be.
Now :
- add default constructors/destructors
- write the function-defenition for the 2 derived classes. You have only made the declaration, but they don't do anything yet.


So, are you saying that the 2 derived classes need to be in the header file? I've only been learning c++ 6 weeks now, so I'm still an amateur.
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Inheritance Hierarchy

  #16  
Jul 18th, 2008
The declaration should be in the header. The definition should be in the cpp file.

example:

foo.h
  1. //declare the class
  2. class foo
  3. {
  4. public:
  5. foo(){}; //do nothing constructor
  6. int bar (int); //declare the function int bar(int)
  7. }

foo.cpp:
  1. #include "foo.h"
  2. //etc
  3.  
  4. //definition of the function 'bar' from class 'foo'
  5. int foo::bar(int i);
  6. {
  7. return i+1;
  8. }
Last edited by niek_e : Jul 18th, 2008 at 3:57 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Jul 2008
Posts: 9
Reputation: chaoticabyss99 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chaoticabyss99 chaoticabyss99 is offline Offline
Newbie Poster

Re: Inheritance Hierarchy

  #17  
Jul 18th, 2008
Thanks niek_e for your insight. It's late here and I need to get some sleep. I'll take a look at this again tomorrow ( or later today since it's already 3:15am) when my brain is working a lil better.
Reply With Quote  
Join Date: Jul 2008
Posts: 9
Reputation: chaoticabyss99 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chaoticabyss99 chaoticabyss99 is offline Offline
Newbie Poster

Re: Inheritance Hierarchy

  #18  
Jul 20th, 2008
Thanks niek_e for helping me out. I finally got it figured out!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC