The objective of this assignment is to give you some practice using inheritance, virtual functions, and polymorphism.  With a focus on dynamic binding, this program shifts the focus from design time static binding of functions to objects to run-time (or dynamic binding).  It leverages the use of virtual functions and introduces the concept of abstract and concrete classes in its implementation of pure virtual functions as it also demonstrates polymorphic behavior.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The objective of this assignment is to give you some practice using inheritance, virtual functions, and polymorphism.  With a focus on dynamic binding, this program shifts the focus from design time static binding of functions to objects to run-time (or dynamic binding).  It leverages the use of virtual functions and introduces the concept of abstract and concrete classes in its implementation of pure virtual functions as it also demonstrates polymorphic behavior.

 

Instructions 

 

  1. Create a base class called Insect. All insects belong to an Order [i.e. “Hemiptera” (ants),  “Siphonaptera” (fleas), “Termitoidae” (termites), “Gryllidae” (crickets), etc.] and have a size that is measured in millimeters (use a double data type). Provide a default constructor that initializes the size to zero and outputs the message “Invoking the default Insect constructor” and another constructor that allows the Order and size to be set by the client. This other constructor should also output the message “Invoking the 2-argument Insect constructor.”  Also create a destructor for this class that outputs the message “Invoking the default Insect destructor.” Your Insect class should have a function called Eat that cannot be implemented.  That is, it should be declared as a purely virtual function. Your class should also have Get and Set methods to allow the order and size to be accessed.

 

  1. From the Insect class, derive Ant, Locust, Butterfly, and Termite classes. The derived classes should each have constructors and destructors that output an appropriate message (e.g., the Ant constructor outputs “Invoking Ant constructor,” and the Ant destructor outputs “Invoking Ant destructor”). The constructor of each derived class should allow the Order and size of the Insect to be set (think member initialization list). The derived classes should each have a member function called Eat that overrides the Insect Eat version. Ant Eat should output “As an ant, I eat everything,” Locust Eat should output “As a locust, I eat leaves,” Butterfly Eat  should output “As a butterfly, I eat nectar,” and Termite Eat should output “As a termite, I eat wood.”

 

  1. Write a main function that uses the Insect and derived classes as needed to do the following. You must perform the actions below in the sequence described (i.e., do not take a shortcut around using dynamic memory allocation/deallocation and virtual methods since they are the whole point of the lab).

 

  1. Use the rand() function in a formula to generate a random size in millimeters based on the insect selected.

The range of sizes for each insect are as follows:

      Ant:                 .01 to 1.0 millimeters

      Locust:                        10.5 to 50.0 millimeters

      Butterfly:        40.0 to 75.5 millimeters

      Termite:           1.5 to 5.5 millimeters

  1. Your program should use a seed value of 100 for the random number generator. You should set the seed only once at the beginning of main().  Use the srand() function to set the seed.

 

  1. Prompt the user to make an Insect selection [e.g. (1) for Ant, (2) for Locust, (3) for Butterfly, and (4) for Termite] and to enter an Order for the insect. Dynamically create an Ant, Locust, Butterfly, or Termite object (depending on what the user entered) and initialize it with a constructor to which is passed its Order and size. Save the object (use an array called “insects” – credit will not be awarded if you use a vector for this).  You may hardcode the size of the array to hold 3 elements.  That is, you don’t need to dynamically create this array unless you want to.  If you do dynamically allocate it, though, be sure to deallocate its memory properly.

 

  1. Repeat steps a. and b. 2 more times. You do not know what insects the user will select or in what order, so you must figure out how to create and store the appropriate objects. Notice that there are 4 insects from which to choose, but the user will only make 3 selections. Therefore, one insect will not be chosen.

 

 

  1. After the user has entered all 3 selections, execute another loop that cycles through the 3 selections and invokes the Eat function and also displays the Order and size of the insect. If you have done it properly, each of your outputs will correspond to the type of Insect the user selected in the order he or she entered them.
Your input screen should look something like this:
Main Menu
1 - Ant
2 .
Locust
3
Butterfly
4 - Termite
Enter selection:
As each insect is selected, you should be prompted to enter the insect's Order:
Enter selection: 3
Enter the Order associated with the insect you selected: Lepidoptera
As each insect is created, you should see messages like the following:
Invoking Insect 2-argument constructor
Invoking Butterfly 2-argument constructor
Your final output screen shout look something like this:
Transcribed Image Text:Your input screen should look something like this: Main Menu 1 - Ant 2 . Locust 3 Butterfly 4 - Termite Enter selection: As each insect is selected, you should be prompted to enter the insect's Order: Enter selection: 3 Enter the Order associated with the insect you selected: Lepidoptera As each insect is created, you should see messages like the following: Invoking Insect 2-argument constructor Invoking Butterfly 2-argument constructor Your final output screen shout look something like this:
My Order is Lepidoptera, and I am 43.65 millimeters long.
As a butterfly, I eat nectar.
My Order is Termitoidae, and I am 1.63 millimeters long.
As a termite, I eat wood.
My Order is Caelifera, and I am 25.14 millimeters long.
As a locust, I eat leaves.
Invoking the Butterfly destructor
Invoking the Insect destructor
Invoking the Termite destructor
Invoking the Insect destructor
Invoking the Locust destructor
Invoking the Insect destructor
Press any key to continue . . .
Transcribed Image Text:My Order is Lepidoptera, and I am 43.65 millimeters long. As a butterfly, I eat nectar. My Order is Termitoidae, and I am 1.63 millimeters long. As a termite, I eat wood. My Order is Caelifera, and I am 25.14 millimeters long. As a locust, I eat leaves. Invoking the Butterfly destructor Invoking the Insect destructor Invoking the Termite destructor Invoking the Insect destructor Invoking the Locust destructor Invoking the Insect destructor Press any key to continue . . .
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education