Reputation: 45
here is my code:
class package
{
protected:
string name;
string city;
string state;
int zip;
double weight;
double costPerOunce;
public:
package::package(string Name, string City, string State, int Zip, double Weight, double CostPerOunce):
name(Name), city(City), state(State),
zip(Zip), weight(Weight), costPerOunce(CostPerOunce)
{
}
double calculateCost()
{
return (weight * costPerOunce);
}
};
class twoDayPackage: public package
{
protected:
double flatFee;
public:
twoDayPackage::twoDayPackage(double FlatFee):
flatFee(FlatFee)
{
}
double calculateCost()
{
return (weight * costPerOunce) + flatFee;
}
};
int main()
{
system ("pause");
return 0;
}
i try to run this code and the error i get is as follows: error C2512: 'package' : no appropriate default constructor available
the error has something to do with inheritance of the base class constructor but i don't know exactly why the code isn't running. please help me.
Upvotes: 2
Views: 1357
Reputation: 24133
The constructor for twoDayPackage
will first create package
, before constructing flatFee
. As you don't tell it how to do that, it looks for a default way of constructing package
.
When you construct twoDayPackage
you need to give it everything it needs to construct the underlying package
. Either that, or have it determine values to pass to the package
constructor.
Passing in the required parameters looks like this:
class twoDayPackage {
public:
twoDayPackage(string Name, string City, string State, int Zip, double Weight, double CostPerOunce, double flatFee) :
package(Name, City, State, Zip, Weight, CostPerOunce),
flatFee(flatFee) {
}
//..
};
Upvotes: 2
Reputation: 5532
You need a constructor for package.
Also you don't need the package::package(...)
when declaring the constructor ( that's for when you define it in the cpp file. ) Just package(...)
will be just fine.
class package
{
protected:
string name;
string city;
string state;
int zip;
double weight;
double costPerOunce;
public:
package()
{}
// \/ You don't need package:: that's only needed when you define the func in cpp
package(
string Name, string City, string State, int Zip,
double Weight, double CostPerOunce
)
: name(Name), city(City), state(State),
zip(Zip), weight(Weight), costPerOunce(CostPerOunce)
{
}
double calculateCost()
{
return (weight * costPerOunce);
}
};
Upvotes: 3
Reputation: 1429
twoDayPackage::twoDayPackage(double FlatFee):
flatFee(FlatFee)
is calling the base constructor package()
, because you haven't specified anything else.
Add a line package::package(){};
in class package :)
Upvotes: 4