class Food
{
int Kg, Quality;
public:
Food(int kg, int quality)
{
Kg = kg;
Quality = quality;
}
~Food(void) {}
};
class Pizza : public Food
{
public:
int Flavor;
// Use of undeclared identifiers 'tempKg' 'tempQuality' [Error]
Pizza(int flavor) : Food(tempKg, tempQuality)
{
// I would like to know if i can use these variables as Food arguments
int tempKg=0, tempQuality=0;
int Flavor = flavor;
}
// This works as it should
/* Pizza(int flavor, int kg, int quality) : Food(kg, quality)
{
int Flavor = flavor;
} */
};
My question is if the parameters that I'm allowed to pass to Food's constructor can only be the parameters declared inside Pizza's constructor?
If they don't necessary need to be, how can I pass parameters into Food that aren't Pizza parameters?
My question is if the parameters that I'm allowed to pass to Food's constructor can only be the parameters declared inside Pizza's constructor?
You are trying to pass variables that don't exist yet when calling Food
's constructor. They are local to the body of Pizza
's constructor, but Food
must be initialized first before the body of Pizza
's constructor is entered.
how can I pass parameters into Food that aren't Pizza parameters?
You have a few choices:
add them to Pizza's constructor, and make them optional if desired:
Pizza(int flavor, int kg = 0, int quality = 0) : Food(kg, quality)
use helper functions that return the desired Food
values, such as calculated from Pizza
's parameter:
Pizza(int flavor) : Food(getKgFor(flavor), getQualityFor(flavor))
just hard-code the values:
Pizza(int flavor) : Food(0, 0)