Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <vector> #include <string> #include <iostream> using namespace std; enu

ID: 3903860 • Letter: #

Question

#include <vector>
#include <string>
#include <iostream>
using namespace std;

enum class Weekday {
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
   Sunday
};

class Patron {
public:
   Patron(string name)
       : _name{ name } {}

   string GetName() const { return _name; }
   virtual string GetDay() const = 0;

private:
   string _name;
};

class Member : public Patron {
public:
   Member(string name, long id)
       : Patron{ name }, _id { id } {}

   string GetDay() const override
   {
       return "any day";
   }

private:
   long _id;
};

class Guest : public Patron {
public:
   Guest(string name, Weekday day)
       : Patron{ name }, _day { day } {}

   string GetDay() const override
   {
       switch (_day)
       {
       case Weekday::Monday:
           return "Monday";
       case Weekday::Tuesday:
           return "Tuesday";
       case Weekday::Wednesday:
           return "Wednesday";
       case Weekday::Thursday:
           return "Thursday";
       case Weekday::Friday:
           return "Friday";
       case Weekday::Saturday:
           return "Saturday";
       case Weekday::Sunday:
           return "Sunday";
       }
   }

private:
   Weekday _day;
};

// There is a popular gym in town that is frequented weekly by many patrons.
// Some patrons are members, and can come every day.
// Some patrons are guests, and can only come on one day every week.
// All patrons have a name, by which they are called.
// We would like to keep track of a list of patrons, and print out some information about each one.
// OBJECTIVE: The following code has a problem that prevents it from compiling. Please fix the error
//   in such a way that the code compiles, and the patrons correctly print out their name and which day
// they are allowed into the gym. Remember, members can come any time, and guests can only
// come once a week on a specific day.
int main()
{
   vector<Patron> patrons;
   patrons.push_back(Member("Jean", 24601));
   patrons.push_back(Member("Cosette", 12345));
   patrons.push_back(Guest("Eponine", Weekday::Monday));
   patrons.push_back(Guest("Fantine", Weekday::Friday));
   patrons.push_back(Member("Javert", 123012));
   patrons.push_back(Member("Thenardier", 123493));
   patrons.push_back(Guest("Marius", Weekday::Sunday));
   patrons.push_back(Member("Gavroche", 84390));

   for (Patron& patron : patrons)
   {
       cout << "Hi, I'm " << patron.GetName() << " and I can come to the gym on " << patron.GetDay() << endl;
   }
}

Explanation / Answer

Below is the modified code which gets compiled and executed. The problem is vector container can hold derived class objects in vector of base class pointers only. Because it has to store all the extra data members available in the derived class inaddition to base class members. Hence it should be of pointer type.

main.cpp:

#include <iostream>

#include <vector>
#include <string>

using namespace std;

enum class Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};

class Patron {
public:
Patron(string name)
: _name{ name } {}

string GetName() const { return _name; }
virtual string GetDay() const = 0;

private:
string _name;
};

class Member : public Patron {
public:
Member(string name, long id)
: Patron{ name }, _id { id } {}

string GetDay() const override
{
return "any day";
}

private:
long _id;
};

class Guest : public Patron {
public:
Guest(string name, Weekday day)
: Patron{ name }, _day { day } {}

string GetDay() const override
{
switch (_day)
{
case Weekday::Monday:
return "Monday";
case Weekday::Tuesday:
return "Tuesday";
case Weekday::Wednesday:
return "Wednesday";
case Weekday::Thursday:
return "Thursday";
case Weekday::Friday:
return "Friday";
case Weekday::Saturday:
return "Saturday";
case Weekday::Sunday:
return "Sunday";
}
}

private:
Weekday _day;
};

// There is a popular gym in town that is frequented weekly by many patrons.
// Some patrons are members, and can come every day.
// Some patrons are guests, and can only come on one day every week.
// All patrons have a name, by which they are called.
// We would like to keep track of a list of patrons, and print out some information about each one.
// OBJECTIVE: The following code has a problem that prevents it from compiling. Please fix the error
// in such a way that the code compiles, and the patrons correctly print out their name and which day
// they are allowed into the gym. Remember, members can come any time, and guests can only
// come once a week on a specific day.
int main()
{
vector<Patron*> patrons;
patrons.push_back(new Member("Jean", 24601));
patrons.push_back(new Member("Cosette", 12345));
patrons.push_back(new Guest("Eponine", Weekday::Monday));
patrons.push_back(new Guest("Fantine", Weekday::Friday));
patrons.push_back(new Member("Javert", 123012));
patrons.push_back(new Member("Thenardier", 123493));
patrons.push_back(new Guest("Marius", Weekday::Sunday));
patrons.push_back(new Member("Gavroche", 84390));

for (Patron* patron : patrons)
{
cout << "Hi, I'm " << patron->GetName() << " and I can come to the gym on " << patron->GetDay() << endl;
}
}

Output:

Hi, I'm Jean and I can come to the gym on any day                                                                                

Hi, I'm Cosette and I can come to the gym on any day                                                                             

Hi, I'm Eponine and I can come to the gym on Monday                                                                              

Hi, I'm Fantine and I can come to the gym on Friday                                                                              

Hi, I'm Javert and I can come to the gym on any day                                                                              

Hi, I'm Thenardier and I can come to the gym on any day                                                                          

Hi, I'm Marius and I can come to the gym on Sunday                                                                               

Hi, I'm Gavroche and I can come to the gym on any day