-2

I have a couple of questions I wanted to check with SO for my Data Structures in C++ course. They deal with the following class and multidimensional array:

class Order
{
public:
  Order();
  void addItem(string name, double price);

private:
  static const int MAX_ITEMS = 10;
  string itemNames[MAX_ITEMS];
  int numItems;  // # of items actually stored
  double totalPrice;
};

const int TABLES = 10;
const int SEATS = 4;
Order diningRoom[TABLES][SEATS];

Q1: How many copies of MAX_ITEMS does the array diningRoom contain?

This is 40 right? There is one copy for each element in the array, 10*4.

Q2: Member function addItem should have been instead declared how?

A.) void addItem(const string &name, double price);
B.) void addItem(string &name, double price);
C.) void addItem(string name, double price) const;
D.) void addItem(string name[], double price);

A? Pass by const reference? This one I'm not too sure of.

  • Q2: There is no hard, fast answer for it. http://stackoverflow.com/questions/10231349/are-the-days-of-passing-const-stdstring-as-a-parameter-over – PaulMcKenzie Sep 28 '15 at 21:35
  • A or B, but you can eliminate C (can't add item to Order if method is const) and D (only adding one item so an array of names is pointless). – user4581301 Sep 28 '15 at 21:42
  • @user4581301 For Q2, the original declaration is valid also. – PaulMcKenzie Sep 28 '15 at 21:44
  • Agreed. A, B, or ... What is less than A? Robert Downey Jr? No, that was Less Than Zero. Shoot. ASCII @? But what if OP is using EBCDIC? Or Baudot? – user4581301 Sep 28 '15 at 22:03

1 Answers1

2
  1. No. There is one instance of MAX_ITEMS since it is a static member.

  2. Yes. Const reference is the way to go.

warsac
  • 251
  • 1
  • 11
  • 2
    Umm... For #2. See my link in the comments. It is highly opinion-based. – PaulMcKenzie Sep 28 '15 at 21:35
  • 2
    Addendum: and because it is a static member there are **zero** copies of `MAX_ITEMS` in `diningRoom`. The one instance of `MAX_ITEMS` is sitting off to the side and not in any object. – user4581301 Sep 28 '15 at 21:42
  • I agree that it is opinion/circumstance based. But for the case of a c++ course it is usually the taught way to handle complex types that are not to be modified by the function. – warsac Sep 28 '15 at 21:44
  • Yeah, on #2 it really depends on what is being done with the string. Odds are pretty good that the string is going to be put in `itemNames` and std::move will make short work of it if you pass by value. Either way it gets copied, but the pass by value allows the compiler to play some fun optimizing games. – user4581301 Sep 28 '15 at 21:46
  • @user4581301 true. My answers never get very thurough when writing on my phone... ;) – warsac Sep 28 '15 at 21:46