0

For some reason I get several errors but I think it has to do with the code that I commented on. Can someone help me please me out because I keep getting the same error over and over again. My code is supposed to answer the following questions: //Create a class callled Package, It should have member variables for the weight of the package and the package tracking number. Add functions to access these variables and set them in the constructor. Add a function to calculate the shipping cost, using the formula cost=$3 per pound

//Create a derived class called Box for box-shaped packages. It should store the dimensions of the height, width, and depth in inches. Redfine the function to calculate shipping where if the longest length+girth is greater than 108 inches then 30 is added to the normal cost

//Write a main function that creates a box, assigns a weight height width and depth and prints the shipping cost

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

    namespace shipping
    {

    const double COST_PER_POUND=3.0;

    class Package
    {

    public:
        Package();//default constructor
        Package(double new_weight, string new_tracking_num);

        double get_weight() const;//accessors
        string get_tracking_num() const;//accessors

        void set_weight(double new_weight);
        void set_tracking_num(string new_tracking_num);

        double cal_shipping() const;

    private:
        double weight;
        string tracking_num;
    };
    }

    #include <string>
    #include "Package.h"
    using namespace std;
    namespace shipping
    {

    Package::Package(): tracking_num(""), weight(0)
    {
    }

    Package::Package(double new_weight, string new_tracking_num):
    tracking_num(new_tracking_num), weight(new_weight)
    {
    }

    double Package::get_weight() const
    {
    return weight;
    }

    string Package::get_tracking_num() const
    {
    return tracking_num;
    }

    void Package::set_weight(double new_weight)
    {
    weight=new_weight;
    }

    void Package::set_tracking_num(string new_tracking_num)
    {
     tracking_num= new_tracking_num;
    }

    double Package::cal_shipping() const
    {
     return weight*COST_PER_POUND;
    }

    }

    #include <string>
    #include "Package.h"
    using namespace std;

    namespace shipping
    {
    class Box : public Package{
    public:
        Box();
        Box(double new_weight, string new_tracking_num,
             double new_width, double new_height,double new_depth);
        void set_dimension(double new_width, double new_height, double new_depth);
        void get_dimensions(double &cur_width, double &cur_height, double &cur_depth);
        double calc_shipping() const;

    private:
        double width, height, depth;
    };
}
         #include "Box.h"
         #include <string>
         using namespace std;

        namespace shipping
    {
    Box::Box(): Package(),height(0),depth(0),width(0)
    {

    }

    //my error comes here 
    Box(double new_weight, string new_tracking_num,
        double new_width,double new_height,double new_depth):
    Package(new_weight, new_tracking_num), height(new_height), depth(new_depth), width(new_width)
    {

    }
    void Box::set_dimension(double new_width, double new_height, double new_depth)
    {
        width=new_width;
        height=new_height;
        depth=new_depth;
    }
    void Box::get_dimensions(double &cur_width, double &cur_height, double &cur_depth)
    {
        cur_width=width;
        cur_height=height;
        cur_depth=depth;
    }

    double calc_shipping() const{

        double longest=width;
        double girth=(height*2)+(2*depth);
        if(height>longest)
        {
            longest=height;
            girth=(width*2)+(2*depth);
        }

        if(depth>longest)
        {
            longest=depth;
            girth=(height*2)+(2*width);
        }

        if(longest+girth>108)
        {
            return Package::cal_shipping()+30;
        }

        else
        {
            return Package::calc_shipping();
        }
    }
}
Brogrammer93
  • 97
  • 1
  • 2
  • 9
  • You appear to have forgotten the class name for `double calc_shipping() const`. Pretty sure that should be `double Box::calc_shipping() const`. The same is true for the constructor implementation; missing `Box::`. Finally, there is no `calc_shipping` in `Package`, you apparently forgot you called it `cal_shipping()` in that class (note missing `c`). – WhozCraig Apr 20 '15 at 04:33
  • @WhozCraig Thank u the code works now but nowI have a small question with 1 of my linecodes. Box::Box(double new_weight, string new_tracking_num, double new_width,double new_height,double new_depth): Package(new_weight, new_tracking_num), height(new_height), depth(new_depth), width(new_width) { When I remove the :Package(new_weight, new_tracking_num), height(new_height), depth(new_depth), width(new_width) and put Package(new_weight, new_tracking_num); height(new_height); depth(new_depth); width(new_width) in the body my test function gives me a different answer } – Brogrammer93 Apr 21 '15 at 04:13
  • You may find [**this question and answers**](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor/8523361#8523361) helpful. Best of luck. And try not to post walls of code in comments. If its important enough to know, it deserves to be in your question as an addendum. – WhozCraig Apr 21 '15 at 04:16

1 Answers1

1

I saw the following issues in your code:

  1. In your constructors, you initialize member variable in a different order than they appear in the class.

    Example:

    In the class Package, you have the variables:

     double weight;
     string tracking_num;
    

    In the constructors, you initialize tracking_num first and then weight. Example:

    Package::Package(): tracking_num(""), weight(0)
    {
    }
    

    It switches the order of initialization because that's a requirement. Since the compiler is changing the order of execution of the code, it warns you about that.

    You have more of those examples, in Package as well as Box.

  2. You forgot to add the class scope Box:: in couple of functions.

    Box(double new_weight, string new_tracking_num,
        double new_width, double new_height,double new_depth):
    

    needs to be

    Box::Box(double new_weight, string new_tracking_num,
             double new_width, double new_height,double new_depth):
    

    and

    double calc_shipping() const{
    

    needs to be

    double Box::calc_shipping() const{
    
  3. You have mis-spelled calc_shipping() in three places and used cal_shipping (a missing 'c').

R Sahu
  • 204,454
  • 14
  • 159
  • 270