I have a base class here:
typedef float Pounds;
typedef int Seconds;
typedef float MilliJoulesPerLiter;
typedef float MilliJoules;
typedef int Liters;
class Fuel {
protected:
static const Pounds carbonDioxideEmmissionsPerLiter;
static const Seconds burnTime;
static const MilliJoulesPerLiter energyYield;
Liters volume {10};
MilliJoules totalMilliJoules {0};
public:
Fuel() : Fuel {10} {};
Fuel(int vol) : volume(vol) {
totalMilliJoules = volume * energyYield;
}
virtual void burn() {
cout << "Burning fuel..." << endl;
int quantity = volume < burnTime ? volume : burnTime;
Sleep(quantity);
volume -= quantity;
totalMilliJoules /= volume * energyYield;
totalEmmissions += carbonDioxideEmmissionsPerLiter * quantity;
cout << "Done burning fuel. Total millijoules burnt: " << quantity * energyYield << endl;
}
};
As you can see, I have a burn() method that uses variables that the base class itself defines. That burn() method should work the same exact way for all subclasses, the only difference is that the subclasses will have different values for those members.
So, here are my subclasses:
class Gasoline : public Fuel {
protected:
static const Pounds carbonDioxideEmmissionsPerLiter {5.07};
static const Seconds burnTime {10};
static const MilliJoulesPerLiter energyYield {34.8};
Liters volume {0};
MilliJoules totalMilliJoules {0};
public:
Gasoline() : Fuel {} {};
Gasoline(int vol) : Fuel {vol} {};
};
class Coal : public Fuel {
protected:
static const Pounds carbonDioxideEmmissionsPerLiter {2.42};
static const Seconds burnTime {5};
static const MilliJoulesPerLiter energyYield {23.9};
Liters volume {0};
MilliJoules totalMilliJoules {0};
public:
Coal() : Fuel {} {};
Coal(int vol) : Fuel {vol} {};
};
class Propane : public Fuel {
protected:
static const Pounds carbonDioxideEmmissionsPerLiter {3.17};
static const Seconds burnTime {20};
static const MilliJoulesPerLiter energyYield {25};
Liters volume {0};
MilliJoules totalMilliJoules {0};
public:
Propane() : Fuel {} {};
Propane(int vol) : Fuel {vol} {};
};
Every time I make a derived class object out of these and I call burn(), I get the base class values. I do know exactly why this is happening; obviously if I don't explicitly override the method burn(), then it will default to the base class version.
However, burn() is of rather nontrivial length, and so copy/pasting it throughout all the subclasses seems very repetitive.
Is there a way to write a method only once in a base class, where the ONLY difference is what the values of the class attributes are, and it uses those?
Essentially, we use virtual functions to resolve what version of the function we use at runtime, for virtual function calls. Are there virtual member accesses too?
- Tried making the
burn()functionvirtual, even though I know that's only for functions, but it was worth a try. Still uses base class version without an override. - Tried using
this->, to no avail. - Tried adding
virtualkeyword to member attributes, which is not valid syntax. - Initialized subclass attributes with an initializer list, changed from static/constant to non-static and non-constant, but the common denominator is not overriding
burn(). - Had subclasses define their own constructors without using base class constructor, but still not overriding
burn()