C++ Programming Assignment 6 Dynamic Pets The objective of this lab is to demonstrate dynamic allocation and deallocation of memory. Instructions: Write a class called Pet that contains an animal’s name, type, and weight.  Include a default constructor and destructor for the class.  The constructor should print out the following message:  “Creating a new pet”.  The destructor should print out the following message:  “In the Pet destructor.”  Include appropriate get/set functions for the class. In main(), prompt the user to enter the number of pets in his or her household.  Dynamically create a built-in array based on this number (not a vector or object of the array class) to hold pointers to Pet objects. Construct a loop in main() that executes once for each of the number of pets that the user indicated.  Within this loop, ask the user to enter the name and type of pet.  Using a random number generator, generate a weight between 1-100 pounds. Seed this random number generator with 100.  Next, dynamically create a Pet object (remember that this requires the use of the “new” keyword which returns a pointer to the location in memory where this pet object was created.)  Create each object using the default constructor of the class, and call the set functions to store the name, type, and weight of each pet.  Store each Pet pointer in the array. After all of the pet objects have been constructed and added to the array, print out the contents of the array. Because the program uses dynamic memory to store the array as well as the objects in the array, be sure to de-allocate all of the memory before exiting. A sample of the program running is shown below

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question

C++ Programming Assignment 6

Dynamic Pets

The objective of this lab is to demonstrate dynamic allocation and deallocation of memory.

Instructions:

Write a class called Pet that contains an animal’s name, type, and weight.  Include a default constructor and destructor for the class.  The constructor should print out the following message:  “Creating a new pet”.  The destructor should print out the following message:  “In the Pet destructor.”  Include appropriate get/set functions for the class.

In main(), prompt the user to enter the number of pets in his or her household.  Dynamically create a built-in array based on this number (not a vector or object of the array class) to hold pointers to Pet objects.

Construct a loop in main() that executes once for each of the number of pets that the user indicated.  Within this loop, ask the user to enter the name and type of pet.  Using a random number generator, generate a weight between 1-100 pounds. Seed this random number generator with 100.  Next, dynamically create a Pet object (remember that this requires the use of the “new” keyword which returns a pointer to the location in memory where this pet object was created.)  Create each object using the default constructor of the class, and call the set functions to store the name, type, and weight of each pet.  Store each Pet pointer in the array.

After all of the pet objects have been constructed and added to the array, print out the contents of the array.

Because the program uses dynamic memory to store the array as well as the objects in the array, be sure to de-allocate all of the memory before exiting.

A sample of the program running is shown below:

How many pets do you have?
3
What is the name of your pet?
Buttercup
What type of pet is Buttercup?
dog
Creating a new pet
What is the name of your pet?
Charlie
What type of pet is Charlie?
cat
Creating a new pet
What is the name of your pet?
Porky
What type of pet is Porky?
pig
Creating a new pet
These are the pets you have:
Buttercup
dog
Pet name:
Pet type:
Pet weight:
66
Pet name:
Charlie
Pet type:
cat
Pet weight:
17
Porky
pig
Pet name:
Pet type:
Pet weight:
16
In the pet destructor
In the pet destructor
In the pet destructor
Press any key to continue .
Transcribed Image Text:How many pets do you have? 3 What is the name of your pet? Buttercup What type of pet is Buttercup? dog Creating a new pet What is the name of your pet? Charlie What type of pet is Charlie? cat Creating a new pet What is the name of your pet? Porky What type of pet is Porky? pig Creating a new pet These are the pets you have: Buttercup dog Pet name: Pet type: Pet weight: 66 Pet name: Charlie Pet type: cat Pet weight: 17 Porky pig Pet name: Pet type: Pet weight: 16 In the pet destructor In the pet destructor In the pet destructor Press any key to continue .
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

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
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L