197k views
2 votes
Using C++, create a program that demonstrates the implementation of polymorphism though an abstract class and a virtual function. The program should use FieldOfPlay as the abstract class with Football and Basketball as the derived classes. The program should:

- Show polymorphism through implementation of at least one abstract class (FieldOfPlay) and at least one virtual function.

- Each derived class should calculate the area of the field (a football field is 360 feet by 159.9 feet)(a basketball court is 94 feet by 50 feet)(All football fields have the same area and all basketball courts have the same area), give the name of the FieldOfPlay (Lambeau Field for football, and Fiserv Forum for basketball), what city it's in (Green Bay for football, and Milwaukee for basketball), and show the capacity (81,441 for Lambeau Field, and 17,385 for Fiserv Forum).

- Explain the logic of the application through comments

* Make sure the application utilizes the standard form of the C++ header files, meaning the class definitions will be in the .h files and the implementation of the functions will be in the .cpp files.

1 Answer

5 votes

Final answer:

The implementation of polymorphism in C++ through an abstract class 'FieldOfPlay' and virtual functions is demarcated with derived classes 'Football' and 'Basketball' for calculating field area, and providing specific facility details.

Step-by-step explanation:

To demonstrate the implementation of polymorphism through an abstract class and a virtual function in C++, an abstract class FieldOfPlay will be defined with two derived classes: Football and Basketball. Polymorphism allows the use of a single interface to represent different underlying forms (data types). The virtual function in the abstract class will be used by the derived classes to calculate the area of their respective playing fields, and provide other specific details such as the name of the field, the city it's in, and its capacity.

FieldOfPlay.h

This header file will contain the abstract class FieldOfPlay with a pure virtual function for calculating the area, and virtual functions for getting the name, city, and capacity of the field.

Football.cpp and Basketball.cpp

In the implementation files Football.cpp and Basketball.cpp, we will define methods to calculate the area of a football field (360 feet by 159.9 feet) and a basketball court (94 feet by 50 feet), and implement the getters for the field name, city, and capacity. This information will be specific for Lambeau Field for football and Fiserv Forum for basketball.

Example Usage:

In the main function, an array of pointers to FieldOfPlay will be created that points to instances of Football and Basketball. By calling the virtual function through these pointers, you demonstrate polymorphism, as the program will decide at runtime which function to call based on the object type.

User AperioOculus
by
8.4k points