Question & Answer: Examine the following incorrect program. Then answer the questions below it. class Animal…..

4. Inheritance (10 points) Examine the following incorrect program. Then answer the questions below it. class Animal public: virtual void print f cout << Im an animal!<endi; virtual void roar privateMethod ) private: void privateMethod cout <<Roar! <<endl; class RedPanda: public Animal public: void print cout << Im a red parda! <<endl: void roar [ cout << Red pandas cen roar, too!<<endl; privateMethod); int main () Animal animallnew Animal Animal *animal2new RedPanda RedPanda * redPanda2 = new RedPanda(); redPandat printtrr. redPanda2->print); animal1->roar animal2->roar redPanda2->roar; a. Some lines in the program above will not compile. Please cross these lines out. b. Assuming the lines you crossed out are no longer in the program, write what this program prints in the space below

Examine the following incorrect program. Then answer the questions below it. class Animal { public: virtual void print() { cout

Expert Answer

 

1) You cannot call private method of class Animal in class RedPanda directly without using object so this needs to be crossed (Inside roar method)

2) RedPanda *redPanda1 = new Animal (); as subclass cannot have reference or pointer to superclass.

3) As we crossed the above point it automatically makes redPanda1->print () erroneous code.

4) And same goes with redPanda1->roar().

5) Here now first redPanda2 (which points to RedPanda class) is calling print function and as this object points to RedPanda class it will call its print function and output “I’m a read panda!”. Then we have animal1 which points to animal class and calls the roar function so it will print Roar. Then next we have animal2 which points to RedPanda class so roar method of RedPanda class will be called and it will print “Red pandas can roar,too!”

Still stressed from student homework?
Get quality assistance from academic writers!