Question
c++ Assuming the following inheritance: class Plant { public: Plant ( ); ~Plant ( ); void grow ( ); // only defined in base class
c++
Assuming the following inheritance:
class Plant {
public:
Plant ( );
~Plant ( );
void grow ( ); // only defined in base class Plant, not re-defined in derived class Cactus
} ;
class Cactus : public Plant {
public:
Cactus ( );
~Cactus ( ) ;
void produce_spines ( ); // For your information: spines are similar to thorns
// This function is Cactus-specific as not every plan produces spines
} ;
1. Declare a pointer to a Plant object and properly initialize it.
2. Dynamically instantiate (create) a Cactus object using default constructor and assign the address of the object to the Plant pointer declared in step 1.
3. Invoke the grow member function for the newly created Cactus object.
4. Invoke produce_spines member function for the Cactus object. You may declare new variable if needed.
5. You're done with the Cactus object let's free its memory. Make sure when Cactus object is destroyed both Cactus' constructor and Plant's constructor are called.
6. Assuming now the Plant::grow member function must be redefined in Cactus class. The requirement is that if the grow function is called via a Plant pointer then the Cactus::grow version will be called instead. What else do you need to modify in the Plant class after the feature above is implemented? Rewrite the class declarations for both Plant and Cactus classes to show your modification.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started