0

I'm practicing a prototype program for a text-based game with complex character features. I have simplified much of what I already wrote, but I'm new to programming and have run into a problem. I want to have a sub-class WaterBottle that has an int drink of value 3. I want another sub-class Hydration to have an int called hydLvl. I want a function within the Hydration subclass that is able to add the drink value to the hydLvl variable when used in the main function.

class Character {

};

class Hydration : public Character {
    public:
        int hydLvl = 5;
        int hydPrint(void) {
            std::cout << "Hyd Lvl: " << hydLvl << endl;
            return 0;
        }
};

class WaterBottle : public Character {
    public:
        int drink = 3;
};
...

So far, I've been unable to successfully utilize the drink variable from the WaterBottle class to modify the hydLvl variable from the Hydration class.

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40
ClayDav
  • 1
  • 1
  • 1
    What does "utilize the drink variable from the WaterBottle class to modify the hydLvl variable from the Hydration class" mean? That won't happen automatically. You'll need a `setDrink()` type function that does the work for you. – tadman Jun 16 '20 at 21:58
  • 3
    Shouldn't `WaterBottle` inherit from `Hydration`? – eduffy Jun 16 '20 at 21:58
  • 2
    Tip: Instead of utility "print" methods, have conventional getter/setter methods and handle the display logic in another concern. You're cluttering up your class here with things that aren't unique to Character. – tadman Jun 16 '20 at 21:59
  • 2
    Does it even make sense for `Hydration` to inherit from `Character`? What is `Hydration` even supposed to represent? If it's a mixin class for "something that can drink liquids" then it would make sense for `Character` to inherit it, rather than it inheriting `Character`. – cdhowie Jun 16 '20 at 22:00
  • 5
    The inheritance model here seems wrong. `class X : public Y` does not add features to class `Y`, it creates a whole new class `X` that has everything `Y` does plus whatever else you define. It almost seems like `Character` should have a `hydration` property instead of a subclass. – tadman Jun 16 '20 at 22:00
  • The pattern I'd use here is defining `Character` that has `std::vector inventory` with an `Item` base class for "thing that you can put in inventory". – tadman Jun 16 '20 at 22:01
  • @tadman Except it would have to be a vector of pointers/references since `item` would probably be polymorphic. ;) – cdhowie Jun 16 '20 at 22:02
  • @cdhowie Probably a smart pointer, but yeah, that! – tadman Jun 16 '20 at 22:03
  • 1
    Recommendation: [Read a bit about the Liskov Substitution Principle](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle) – user4581301 Jun 16 '20 at 22:08
  • 2
    You can have a function in Hydration that takes a WaterBottle parameter. That function will have access to the Hydration's hydLvl and the WaterBottle's drink, because it's in Hydration and drink is public in WaterBottle. Then all it has to do is addition and assignment. What's the problem? – Dennis Sparrow Jun 16 '20 at 22:11
  • Adding to the comments by @tadman, `Hydration` doesn't sound like an inventory item, but it might be in another similar container of the character's properties/statistics. – aschepler Jun 16 '20 at 22:12
  • 2
    If you're thinking that you want to alter a variable in another class, you are likely not thinking abstractly enough. Likely, you want a situation where using the water bottle will increase the hydration level. At this level, do you care how the hydration level is stored? No, you do not. You simply want to tell your `Hydration` object that it just got `x` units of liquid. How much is `x`? You get that by asking the `WaterBottle` object how much hydration it provides. It might seem overly complex to write something like `hydro.add(bottle.amount());`, but it compartmentalizes your logic. – JaMiT Jun 16 '20 at 23:14

0 Answers0