Question: Please help me with the below program. ----------------------------------------------------------------------------------------------------------------------------------- #include #include using namespace std; class Humanoid { public: double height(); double weight(); protected: double _speciesHeight; double
Please help me with the below program.
-----------------------------------------------------------------------------------------------------------------------------------
#include
#include
using namespace std;
class Humanoid
{
public:
double height();
double weight();
protected:
double _speciesHeight;
double _speciesWeight;
};
double Humanoid::height()
{
return _speciesHeight;
}
double Humanoid::weight()
{
return _speciesWeight;
}
class Moclan : public Humanoid
{
public:
Moclan(double moclanHeight, double moclanWeight);
bool isEgg();
Moclan layEgg();
void hatch();
private:
bool _isEgg;
bool _theEgg;
};
Moclan::Moclan(double moclanHeight, double moclanWeight)
{
_speciesHeight = moclanHeight;
_speciesWeight = moclanWeight;
_isEgg = false;
_theEgg = false;
}
bool Moclan::isEgg()
{
return _isEgg;
}
Moclan Moclan::layEgg()
{
_theEgg = true;
_isEgg = true;
return *this;
}
void Moclan::hatch()
{
_isEgg = false;
}
int main() {
Moclan moclan(6.9, 320);
cout << "Moclan height: " << moclan.height() << endl;
cout << "Moclan weight: " << moclan.weight() << endl;
moclan.isEgg(); // should return false
cout << "moclan.isEgg(), (need false): " << moclan.isEgg() << endl;
Moclan moclanEgg = moclan.layEgg(); // call layEgg()
moclan.isEgg(); // should return false
cout << "moclan.isEgg(), (need false): " << moclan.isEgg() << endl;
moclanEgg.isEgg(); // should return true
cout << "moclanEgg.isEgg(), (need true): " << moclanEgg.isEgg() << endl;
moclanEgg.hatch(); // call hatch()
moclanEgg.isEgg(); // should return false
cout << "moclanEgg.isEgg(), (need false): " << moclanEgg.isEgg() << endl;
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------
The output is:
Moclan height: 6.9
Moclan weight: 320
moclan.isEgg(), (need false): 0
moclan.isEgg(), (need false): 1 // Need a false;
moclanEgg.isEgg(), (need true): 1
moclanEgg.isEgg(), (need false): 0
The main part CAN'T be modified, so only the class and function declaration can be changed.
This is how it's supposed to flow:
After calling Moclan moclanEgg = moclan.layEgg(),
moclan.isEgg() should return false while moclanEgg.isEgg() should return true,
then calling moclanEgg.hatch(), that moclanEgg.isEgg() returns false again.
Thanks for your help!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
