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.