Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Subject: Polymorphism Write a complete C++ program and create 8 classes that form a class tree as shown in Figure 1. Every class has only

image text in transcribed

Subject: Polymorphism Write a complete C++ program and create 8 classes that form a class tree as shown in Figure 1. Every class has only one no-argument constructor, no property. Each class may have an eat0 method and/or a grow) method Both have no return and no parameter. Inside any no argument constructor, any eat0 method or any growO method, there is only one output statement to print Animal eat0 ErowO 143 Sardine groBass et Cat eat0 grow0 constructor of xxx class", "eatO method of xxx class or "growO method of xxx class", respectively. Here Lion eatOxxx" represents the current class. Add a line break at the end of the printout. There is no need to split the code due to the simplicity of all classes. Place all 8 classes, main function, and all other functions in a single source file. Hint: the virtual keyword should be applied to overridden methods at the highest tree level such that the dynamic binding could be activated. In this case, only apply the virtual keyword to regular methods in the root class, i.e. the 1iger Figure 1 Animal class. Without the virtual keyword, the program will do static binding instead Add another function above main with given code below void display(const Animal al) a1.eat In the main0 function, create one object for each leaf classes, i.e. classes Sardine, Bass, Tiger, and Lion. Then call both eat0 and growO from each object. After successful debugging and running the project, carefully observe the printout and write your own explanation to a text file named "readme.txt". Note that the "readme.txt" accounts for 30 points. Place this file in the source file folder ofyour project. The file should have at least 80 words. The file should clearly explain why the printout is as observed. In details, answer following questions: 1) What is polymorphism? (5 points) 2) How to enable dynamic binding? (5 points) 3) What are rules of dynamic binding? (10 points) 4) How rules of dynamic binding affect the printout? (10 points) Subject: Polymorphism Write a complete C++ program and create 8 classes that form a class tree as shown in Figure 1. Every class has only one no-argument constructor, no property. Each class may have an eat0 method and/or a grow) method Both have no return and no parameter. Inside any no argument constructor, any eat0 method or any growO method, there is only one output statement to print Animal eat0 ErowO 143 Sardine groBass et Cat eat0 grow0 constructor of xxx class", "eatO method of xxx class or "growO method of xxx class", respectively. Here Lion eatOxxx" represents the current class. Add a line break at the end of the printout. There is no need to split the code due to the simplicity of all classes. Place all 8 classes, main function, and all other functions in a single source file. Hint: the virtual keyword should be applied to overridden methods at the highest tree level such that the dynamic binding could be activated. In this case, only apply the virtual keyword to regular methods in the root class, i.e. the 1iger Figure 1 Animal class. Without the virtual keyword, the program will do static binding instead Add another function above main with given code below void display(const Animal al) a1.eat In the main0 function, create one object for each leaf classes, i.e. classes Sardine, Bass, Tiger, and Lion. Then call both eat0 and growO from each object. After successful debugging and running the project, carefully observe the printout and write your own explanation to a text file named "readme.txt". Note that the "readme.txt" accounts for 30 points. Place this file in the source file folder ofyour project. The file should have at least 80 words. The file should clearly explain why the printout is as observed. In details, answer following questions: 1) What is polymorphism? (5 points) 2) How to enable dynamic binding? (5 points) 3) What are rules of dynamic binding? (10 points) 4) How rules of dynamic binding affect the printout? (10 points)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago