Function arguments, skip and use defaults

I am wondering how to make a function where when you skip an argument, it will automatically use its default. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  class Store
  {
  public:
    void setInventory(bool hotdog = false, bool hamburger = false, bool frenchfries = false)
    {
      bHotDog = hotdog;
      bHamburger = hamburger;
      bFrenchFries = frenchfries;
    }
  private:
    bool bHotDog;
    bool bHamburger;
    bool bFrenchFries;
  };

  int main()
  {
    Store boston;
    Store newyork;
    boston.setInventory(true,,true); // boston sells hot dogs and french fries
    newyork.setInventory(,true,true); //new york sells hamburgers and french fries
    return 0;
  }


I am asking because the store class will have around a thousand variables that I'll be setting but for each object only 50 will be different than the default. To my knowledge this program won't work because you can't just skip an argument with a comma and let it use the default.

Thank you for your time.
I am asking because the store class will have around a thousand variables that I'll be setting but for each object only 50 will be different than the default.

It sounds to me like the faster route would be to list the items that the each store doesn't have and for the rest, make the default value true. But this is defiantly one of those examples where encapsulation works against you, so you may want to just make those member variables public. Do you have to track quantity as well? Or is it just a yes or no in stock kind of problem?
There are no named parameters, only positional parameters and thus "skipping" will fail.

The Store could have static members:
1
2
3
4
5
6
class Store {
public:
  static bool defBurger() { return false; }
...

boston.setInventory(true, Store::defBurger(), true);


Perhaps simpler is to not use a monolithic setInventory, but:
1
2
3
4
5
6
7
8
9
10
class Store {
public:
  Store & setBurger( bool state )
  {
    bHamburger = state;
    return *this;
  }
...

newyork.setBurger(true).setFries(true);


However, "thousand variables in one class" reeks of "there must be an alternative design". What if someone invents sliced bread after you program is in use? Will you add one more variable to the code, recompile, and upgrade all installations?
Hey @Computergeek01 thank you for the reply. I think my original post was a bit confusing. What I meant to say was the each store will have around 1000 variables (where the default is false) and I would want to use a function that would set around 50 to true. This code is also oversimplifying a bit. Not everything will be a boolean variable. I'll have some strings and unsigned shorts. However, I'm still only going to be changing around 50 from the default. I will have all the defaults set to false, but from my research you can only use defaults if they are the arguments at the end of the function, and that won't work for me.

I'm not sure what you mean by encapsulation. I'm going to do some research on that. But I don't see how moving the variable from public to private would make a difference (I just did that because I have been told that it is good programming to put your functions public and your variables private).
As for quantity its not important for this program. What I want to do it make about a couple hundred objects and set the inventory for each. The user (me) will be able to select a store and run different functions that will do analysis based on the inventory. For example:

Miami sells hot dogs, hamburgers, and tacos (so the boolean will be set to true for those). Hot dogs, hamburgers, and tacos will each be their own class that has variables like price, etc. So I can do a function that multiplies the price of each by miami's taxes, and show the user each product and the final price. Again this is simplifying. I believe I could do something like:

boston.setInventory(true,false,true);

but with a thousand products, I'm going to be typing false a lot (or I can use 0 I believe which would save a little time). I just don't understand the purpose of a default argument, if I can't skip arguments when I call the function (again not everything is a boolean and typing in default strings is time consuming).

Thank you again for your time
... and that won't work for me.

It won't work for you in this one specific use case is how you should be thinking of it. Default values for arguments are for those edge cases that are only different once in a great while. Personally, I only ever use them to turn logging features on or off in some of my functions and even then it's only for functions that I've put in DLL's where I'm too lazy to recompile the code. Not every tool in C++ is useful 100% of the time. But as a Turing complete language it tries to provide for as many scenarios as it can.

(I just did that because I have been told that it is good programming to put your functions public and your variables private).

It is in fact better practice to not just assume over generalized rules without regards to context when it comes to design considerations.
@keskiverto Thank you for the reply. I am still new to programming and I am a little unfamiliar with 'static' so I am going to do some research on that. Is it like 'const' because if it is, I don't think it would work because some stores will sell burgers so sometimes it will be true. As for the monolithic setInventory, I have thought about dividing it up into multiple functions, I have it divided into 3, one to take care of strings, one for the shorts, and the third for the bools.

The problem is that the bools are the majority as they are the inventory, and each store could potentially sell any of the products. I can't divide the class into Mexican store, etc, because while the Mexican store is most likely to sell tacos, not all sell every type of taco, and some sell hamburgers too. So when I set up each store, I have to go through all the inventory.

I also understand that my design is probably flawed. That is what I am trying to set up right now before I start entering everything. I'm still learning programming and this is really the first time I'm setting up something this complicated.

@Computergeek01 I just realized the point you were making regarding public vs private. That would make life easier as I could then just modify the specific variables directly. That could work for me as it will be a lot quicker. However, I was told that it is poor programming to do that, so it is kind of undesirable as I do want to learn how to program correctly. However, for now I think it will work.

edit: @Computergeek01 Oh ok I understand about over generalizing rules. I am still new to programming and I want to learn good habits and practices. I didn't want to do something the 'incorrect' way if there was a better way to do it I didn't know about. I think moving my variables to public will make life a lot easier and I am going to try to do that. Thank you again for the help.
Last edited on
How about this:
1
2
3
4
5
6
7
8
9
10
class Store {
  std::set<std::string> products;
public:
  void addProduct( std::string product ) {
    products.emplace( product );
  }

  bool hasProduct( const std::string & product ) const {
    return products.count( product );
  }

Now each store can have unique (and large) set of products. The set effectively replaces all your bool variables.
Topic archived. No new replies allowed.