4 Question 4 . You are given two classes Chicken and Egg, described below. . The
ID: 3918253 • Letter: 4
Question
4 Question 4 . You are given two classes Chicken and Egg, described below. . The class Chicken contains a vector of Egg objects. class Chicken C public: void layEgg O; int nunEggs ) const void getEggs (vector ) private: vector vec.eggs 1. The method numEggs returns the size of the vector vec.eggs and is const. 2. Method layEgg: instantiate an Egg object and push it back onto the vector vec.eggs Do not use dynamie memory. Just instantiate "Egg e" and push back e onto the vector. 3. In the method getEg8s, do two things: (a) Set r to a copy of the vector vec eggs. (b) Clear the vector vec eggs (e) In other words, the farmer collects the eggs and the chicken has no more eggs. . The class Egg contains a method hatch which returns a pointer to a new Chicken. class Egg t private: bool born; public: Egg (born false; bool isBorn) const f return born; Chicken, hatchO; 1. The default constructor initializes born to talse. The code is written for you. 2. The accessor methoul ieBorn) returns the value of born. The code is written for you. 3. In the method hatch, do the following. (a) ?f born is true, return NULL and exit. An egg eannot latch more than once, (b) If born is false, set born true. Dynamically create a new Chicken and return a pointer to the dynamie memory. In other words, a new ehicken is boru.Explanation / Answer
class Egg{ private: bool born; public: Egg() {born = false;} bool isBorn() const {return born;} Chicken * hatch(); }; class Chicken { public: void layEgg(); int numEggs() const; void getEggs(vector &v); private: vector vec_eggs; }; void Chicken::layEgg() { Egg e; vec_eggs.push_back(e); } int Chicken::numEggs() const { return vec_eggs.size(); } void Chicken::getEggs(vector &v) { vector new_(vec_eggs); v = new_; vec_eggs.clear(); } Chicken * Egg::hatch() { if(born) { return NULL; } else { born = true; return new Chicken(); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.