Question: 3. Write a C++ program that uses virtual functions to input, calculate, and display some quantities associated with a variety of 2- and 3-dimensional objects.
3. Write a C++ program that uses virtual functions to input, calculate, and display some quantities associated with a variety of 2- and 3-dimensional objects. Design an abstract base class Shape from which you will derive classes Rectangle, Circle, Triangle, Box, Can, Cone, and Ball. The class Shape should contain pure virtual functions Display, GetDimensions, Area, Perimeter, and Volume. For each of the derived classes, write member functions Display, GetDimensions, Area, Perimeter, and Volume that will do the following:
Display - output the type and dimensions for an object of the class.
GetDimensions - get the dimensions for an object of the class.
Perimeter - Calculate and display the perimeter of an object of a 2-dimensional shape class; do nothing for an object of a 3-dimensional shape class.
Area - Calculate and display the area of an object of a 2-dimensional shape class; calculate and display the surface area for an object of a 3-dimensional shape class.
Volume - Calculate and display the volume of an object of a 3-dimensional shape class; do nothing for an object of a 2-dimensional shape class.
Include appropriate constructors and any other member functions that you think are necessary.
Write a program driver that will allocate an array of 20 pointers to class Shape and then repeatedly prompt the user, up to a maximum 20 times, to enter a shape type, dynamically allocate memory for an appropriate shape object, and then prompt for and read the appropriate dimensions for the shape. After the input of shapes is complete, the program should then loop through the pointer array contents and display the shape type, dimensions, area, perimeter and/or volume for each of the input shape objects.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
