(COMP2012)[2014](s)final_=in8gpimcc^_56359.pdf

The following classes are defined in ecosystemh note

Info icon This preview shows pages 14–17. Sign up to view the full content.

View Full Document Right Arrow Icon
The following classes are defined in ecosystem.h . Note that you are not allowed to add any other extra member functions: // Class: Organism const double INIT LIFE_POINT = 100.0; // default life point = 100 class Organism { public: Organism(double lp = INIT_LIFE_POINT): lifePoint(lp) {} virtual double nutritionValue() const = 0; void setLifePoint(double lp) {lifePoint=lp;} double getLifePoint() const {return lifePoint; } private: double lifePoint; }; // Class: Plant class Plant: public Organism { public: Plant(double w, double lp = INIT_LIFE_POINT); virtual double nutritionValue() const = 0; void photosynthesis(double rateOfChange); protected: double weight; }; // Class: HealthyPlant class HealthyPlant : public Plant { public: HealthyPlant(double w, double lp = INIT LIFE_POINT); virtual double nutritionValue() const ; }; // Class: PoisonPlant class PoisonPlant : public Plant { public: PoisonPlant(double degOfPoison, double w, double lp = INIT_LIFE_POINT); virtual double nutritionValue() const; private: double degreeOfPoison; }; // Class: Animal class Animal: public Organism { public: Animal(double lp = INIT_LIFE_POINT); // assume it is implemented virtual double nutritionValue() const ; void eat(const Organism* victim) ; }; (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Page 15 of 23 You are required to implement the above classes in ecosystem.cpp (a) [6 marks] The Plant class (i) [3 marks] Implement the constructor of Plant , which constructs a Plant object with a lifepoint lp and weight w . Plant::Plant(double w, double lp) : Organism(lp), weight(w) { } (ii) [3 marks] Implement the member function photosynthesis of Plant , which increases the Plant weight by rateOfChange , where rateOfChange is in percentage. void Plant::photosynthesis(double rateOfChange) { weight = weight * (1.0 + rateOfChange/100.0); } (b) [3 marks] In the HealthyPlant class, implement the member function nutritionValue which returns its nutrition value. double HealthyPlant::nutritionValue() const { return getLifePoint()*weight; } (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.
Image of page 15
Page 16 of 23 (c) [3 marks] In the PoisonPlant class, implement the member function nutritionValue which returns its nutrition value. double PoisonPlant::nutritionValue() const { return -degreeOfPoison*getLifePoint()*weight; } (d) [4 marks] An animal object eats a victim , which may point to an animal, healthy or poison plant, by adding its life point with the nutrition value of the victim . Implement the member function eat of the Animal class. Note that if the input pointer is NULL or points to the animal itself, you should do nothing (an animal cannot eat itself). void Animal::eat(const Organism* victim) { if (victim!= NULL && victim!= this) { double lp = getLifePoint(); lp += victim->nutritionValue(); setLifePoint(lp); victim->setLifePoint(0); // may be set to any negative value } } (e) [9 marks] In this part of the problem, you are going to write a program applying the class functions.
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern