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!”