0
class Package{

    private:
        float weight;
        float cost;
    
    public:
        Package(float weight, float cost){
            setNumber(weight, cost);
        }

        void setNumber(float weight, float cost){
            this->weight = weight;
            this->cost = cost;
        }

        float getWeight(){
            return this->weight;
        }

        float getCost(){
            return this->cost;
        }

        float calculateCost(){
            return getWeight()*getCost();
        }
};

class TwoDayPackage : private Package{

    private:
        float fee;

    public:
        TwoDayPackage(float fee){
            setFee(fee);
        } 

        void setFee(float fee){
            this->fee = fee;
        }

        float getFee(){
            return this->fee;
        }

        float calculateCost(){
            return Package::calculateCost() + getFee();
        }
};

Hello I have these two classes wherein TwoDayPackage is a derived class from Package. In my code flow, I would like to enter two values for weight and cost in the class Package and calculate its cost. However, I would also like to override the function calculateCost() from Package when I try to enter values for the additional fee in TwoDayPackage. Wherein, I try and get the value from the original Package and add the additional fee from TwoDayPackage.

int main(){

    float a, b, c, d;

    cin >> a;
    cin >> b;
    cin >> c;

    Package pack(a,b);
    TwoDayPackage packTwoDayPackage(c);

    cout << packTwoDayPackage.calculateCost();
}

In main it looks like this, but I only get a wall of errors I cannot understand.

package.cpp: In constructor 'TwoDayPackage::TwoDayPackage(float)':
package.cpp:40:33: error: no matching function for call to 'Package::Package()'
   40 |         TwoDayPackage(float fee){
      |                                 ^
package.cpp:12:9: note: candidate: 'Package::Package(float, float)'
   12 |         Package(float weight, float cost){
      |         ^~~~~~~
package.cpp:12:9: note:   candidate expects 2 arguments, 0 provided
package.cpp:5:7: note: candidate: 'constexpr Package::Package(const Package&)'
    5 | class Package{
      |       ^~~~~~~
package.cpp:5:7: note:   candidate expects 1 argument, 0 provided
package.cpp:5:7: note: candidate: 'constexpr Package::Package(Package&&)'
package.cpp:5:7: note:   candidate expects 1 argument, 0 provided
package.cpp: In constructor 'OvernightPackage::OvernightPackage(float)':
package.cpp:62:36: error: no matching function for call to 'Package::Package()'
   62 |         OvernightPackage(float fee){
      |                                    ^
package.cpp:12:9: note: candidate: 'Package::Package(float, float)'
   12 |         Package(float weight, float cost){
      |         ^~~~~~~
package.cpp:12:9: note:   candidate expects 2 arguments, 0 provided
package.cpp:5:7: note: candidate: 'constexpr Package::Package(const Package&)'
    5 | class Package{
      |       ^~~~~~~
package.cpp:5:7: note:   candidate expects 1 argument, 0 provided
package.cpp:5:7: note: candidate: 'constexpr Package::Package(Package&&)'
package.cpp:5:7: note:   candidate expects 1 argument, 0 provided

It looks like this, I am not really sure where to start solving things. Please be patient with me, I just learned C++ yesterday and just got yeeted to OOP as well so I apologize in advance.

omoraisu
  • 51
  • 6
  • 4
    Here's a general hint. When confronted with a wall of errors, only look at the first one. Very often, fixing it will clear out a bunch of stuff, and it's a lot easier to reason about. –  Jun 11 '21 at 17:44
  • 1
    `TwoDayPackage` is-a `Package` and the constructor for `Package` needs a weight and a cost. Also private inheritance and no virtual functions suggests composition should be used instead of inheritance. – Richard Critten Jun 11 '21 at 17:45
  • 1
    `error: no matching function for call to 'Package::Package()'` Is pretty straightforward considering that the constructor of package is `Package(float weight, float cost)` –  Jun 11 '21 at 17:45
  • Unrelated : `class TwoDayPackage : private Package`. You sure you want private inheritance here? – Wander3r Jun 11 '21 at 17:48
  • @Frank but i am only allowed one argument in `TwoDayPackage` :( – omoraisu Jun 11 '21 at 17:49
  • @Wander3r i was playing around the public private pizzazz but eitherway it resulted to a shitton of errors xDD – omoraisu Jun 11 '21 at 17:50

3 Answers3

1

There some "hidden" code here:

TwoDayPackage(float fee) { }

The actual code being compiled is essentially:

TwoDayPackage(float fee) : Package() { }

which tries to construct a Package with no parameters, so you can fix your problem by using the correct constructor instead:

TwoDayPackage(float fee) : Package(0, fee) { }
1

You have created a type Package that can only be constructed if a weight and cost is provided.

Package(float weight, float cost){
    setNumber(weight, cost);
}

You also create a type TwoDayPackage that is-a type of Package and has this constructor:

TwoDayPackage(float fee){
    setFee(fee);
}

This constructor is invalid, because no weight or cost is provided to the Package object it derives from, producing an error, telling you that Package has no constructor that takes no parameters:

error: no matching function for call to 'Package::Package()'

You would instead need some way to provide these parameters, such as:

TwoDayPackage(float weight, float cost, float fee)
    : Package(weight, cost)
{
    setFee(fee);
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • can i ask something though... TwoDayPackage(float weight, float cost, float fee) `: Package(weight, cost)` whats the point of this part in the program? – omoraisu Jun 11 '21 at 18:02
  • @arpi314 Look for the [Ask Question] button at the top of every page. – Drew Dormann Jun 11 '21 at 19:28
1

This is happening because constructor of TwoDayPackage implicitly call parent constructor, because it's need to initialize parent class with data.

You can find more here and here.