fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Drinks {
  5. private:
  6. std::string name;
  7. double price;
  8. int qtyInMachine;
  9. public:
  10. Drinks(std::string n, double p, int qty) : name(n), price(p), qtyInMachine(qty) {}
  11. std::string getName() const { return name; }
  12. };
  13.  
  14. class VendingMachine {
  15. private:
  16. static const int NUM_DRINKS = 5;
  17. static const Drinks drinks[NUM_DRINKS]; // Suggestion: Use std::vector here
  18.  
  19. public:
  20. static int getMaxNumOfDrinks() { return NUM_DRINKS; }
  21. static Drinks getDrink(const int i) { return drinks[i]; } // TODO: Add error handling here!!!
  22.  
  23. VendingMachine(){}
  24. };
  25.  
  26. const Drinks VendingMachine::drinks[VendingMachine::NUM_DRINKS] {Drinks("Cola",1.25,20),
  27. Drinks("Root Beer",1.35,20),Drinks("Orange Soda",1.20,20),Drinks("Grape Soda",1.20,20),
  28. Drinks("Bottled Water",1.55,20)};
  29.  
  30. int main() {
  31. VendingMachine vm1;
  32. for (int i = 0; i < VendingMachine::getMaxNumOfDrinks(); i++) {
  33. std::cout << VendingMachine::getDrink(i).getName() << " ";
  34. }
  35. }
Success #stdin #stdout 0s 4496KB
stdin
Standard input is empty
stdout
Cola Root Beer Orange Soda Grape Soda Bottled Water