Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig, \(12.3 .\) Write down all the shapes you can think ofboth two- dimensional and three-dimensionaland form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have base class shape from which class Two imensionalshape and class ThreeDimensionalshape are derived. [Note: You do not need to write any code for this exercise.] We will use this hierarchy in the exercises of Chapter 13 to process a set of distinct shapes as objects of base-class shape. (This technique, called polymorphism, is the subject of Chapter \(13 .\).

Short Answer

Expert verified
The hierarchy starts with `Shape`, with two subclasses: `TwoDimensionalShape` and `ThreeDimensionalShape`, each with specific shape subclasses.

Step by step solution

01

Identify Two-Dimensional Shapes

Two-dimensional shapes have width and height but no depth. Common examples include: - Circle - Square - Rectangle - Triangle - Pentagon - Hexagon - Octagon - Trapezoid - Parallelogram - Ellipse These can be part of the `TwoDimensionalShape` subclass.
02

Identify Three-Dimensional Shapes

Three-dimensional shapes have width, height, and depth. Common examples include: - Sphere - Cube - Cuboid (Rectangular prism) - Pyramid - Cone - Cylinder - Tetrahedron - Octahedron These can be part of the `ThreeDimensionalShape` subclass.
03

Overall Shape Hierarchy Structure

The base class will be `Shape`. From `Shape`, there will be two derived classes: `TwoDimensionalShape` and `ThreeDimensionalShape`. Subclasses derived from `TwoDimensionalShape` include `Circle`, `Square`, `Rectangle`, `Triangle`, `Pentagon`, `Hexagon`, `Octagon`, `Trapezoid`, `Parallelogram`, and `Ellipse`. Subclasses derived from `ThreeDimensionalShape` include `Sphere`, `Cube`, `Cuboid`, `Pyramid`, `Cone`, `Cylinder`, `Tetrahedron`, and `Octahedron`.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Polymorphism
Polymorphism is a powerful concept in object-oriented programming that allows objects to be treated as instances of their parent class. This feature is particularly useful when dealing with shapes because it lets us define a single interface, like `Shape`, and use it to manage diverse behaviors in different shapes.
For instance, you could create a function that calculates the area of a shape, and use the same function to handle circles, squares, and triangles, as they all derive from the base class `Shape`. Polymorphism simplifies code management:
  • Promotes code reusability by allowing a general class to handle various cases.
  • Makes it easier to add new shape types without altering existing code.
  • Facilitates the management of groups of objects through a common interface.
Whenever you call a method on a `Shape` object, the correct version of the method is executed depending on the shape's actual class, making your programs versatile and easily extensible.
Shapes Hierarchy
In programming, establishing a hierarchy is crucial for organizing data. When dealing with shapes, creating a shapes hierarchy helps to categorize and structure different kinds of shapes logically. At the top of this hierarchy, we have the base class `Shape`, which represents the most generalized concept.
Underneath this, shapes can be divided into two major subclasses: `TwoDimensionalShape` and `ThreeDimensionalShape`. These subclasses branch out into more specific shapes, each with its own properties and methods:
  • `TwoDimensionalShape` includes classic shapes like circles, squares, and triangles.
  • `ThreeDimensionalShape` covers shapes like spheres, cubes, and pyramids.
A well-constructed hierarchy makes your program more organized and easier to navigate. It also ensures a clear path for inheritance and polymorphism, where specialized behaviors of shapes can be implemented at different levels of the hierarchy.
Inheritance
Inheritance is a core principle of object-oriented programming, allowing new classes to adopt the properties and methods of existing classes. This is particularly useful in shaping hierarchies where general attributes and behaviors can be defined in a base class like `Shape` and then be automatically available to derived classes such as `TwoDimensionalShape` and `ThreeDimensionalShape`.
The advantages of using inheritance in shape hierarchies include:
  • Code reuse, as common features and properties need to be written only once.
  • Clear understanding of relationships between different classes based on a hierarchy.
  • Simplification of code modifications and maintenance, as changes to a base class can propagate to derived classes.
With inheritance, you can create more complex and nuanced classes based on simpler ones, enriching the functionality of the shapes within your program while keeping the code clean and efficient.
2D Shapes
Two-dimensional shapes, or 2D shapes, have width and height but lack depth. These shapes are flat and exist in the plane, making them simpler to analyze geometrically compared to their 3D counterparts. Common examples are:
  • Circle - Defined by its radius; every point is equidistant from a center.
  • Square - A polygon with four equal sides and right angles.
  • Triangle - Defined by three sides and three vertices.
  • Rectangle - Similar to a square but with opposite sides equal and right angles.
  • Ellipse - An oval shape determined by its major and minor axes.
Each of these shapes can have unique properties and formulas, such as area or perimeter calculations, making them important for many practical applications like graphic design, architecture, and more.
3D Shapes
Three-dimensional shapes, or 3D shapes, have width, height, and depth, making them more complex as they occupy space. They are not flat like 2D shapes and are used extensively in modeling and simulations. Some common 3D shapes are:
  • Sphere - All points are equidistant from its center, like a ball.
  • Cube - All sides are equal squares, forming a regular polyhedron.
  • Cylinder - Composed of two parallel circular bases connected by a curved surface.
  • Cone - Has a circular base and a point called the apex; tapering smoothly from base to apex.
  • Pyramid - A polygonal base and triangular faces converging at a point.
Each 3D shape has properties that can be leveraged in volume and surface area calculations, making them critical in fields such as construction, 3D modeling, and virtual reality.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Draw an inheritance hierarchy for classes ouadrilateral, TRapezoid, Parallelogram, Rectangle and Square. Use quadrilateral as the base class of the hierarchy. Make the hierarchy as deep as possible.

State whether each of the following is true or false. If \(f a / s e,\) explain why. a. Base-class constructors are not inherited by derived classes. b. A has-a relationship is implemented via inheritance. c. \(A\) car class has an \(i s\) -a relationship with the steeringwheel and Brakes classes. d. Inheritance encourages the reuse of proven high-quality software. e. When a derived-class object is destroyed, the destructors are called in the reverse order of the constructors.

Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee of the CommissionEmployeeBasePlusCommissionEmployee hierarchy to use composition rather than inheritance. After you do this, assess the relative merits of the two approaches for designing classes commissionEmployee and BasePlusCommissionEmployee, as well as for object-oriented programs in general. Which approach is more natural? Why?

Fill in the blanks in each of the following statements: a. _______ is a form of software reuse in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. b. A base class's _______ members can be accessed only in the base- class definition or in derived-class definitions. c. \(\ln a(n)\) _______ relationship, an object of a derived class also can be treated as an object of its base class. d. \(\ln a(n)\) _______relationship, a class object has one or more objects of other classes as members. e. In single inheritance, a class exists in a(n) _______ relationship with its derived classes. f. A base class's _______ members are accessible within that base class and anywhere that the program has a handle to an object of that base class or to an object of one of its derived classes. g. \(A\) base class's protected access members have a level of protection between those of public and _______ access. h. \(C++\) provides for _______ which allows a derived class to inherit from many base classes, even if these base classes are unrelated. i. When an object of a derived class is instantiated, the base class's _______ is called implicitly or explicitly to do any necessary initialization of the base-class data members in the derived-class object. J. When deriving a class from a base class with public inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class. k. When deriving a class from a base class with protected inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class.

( Package Inheritance Hierarchy) Package-delivery services, such as \(\mathrm{FedEx}^{\mathbb{Q}}\), \(\mathrm{DHL}^{@}\) and \(\mathrm{UPS}^{@}\), offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and overnight Package that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP code for both the sender and the recipient of the package, in addition to data members that store the weight (in ounces) and cost per ounce to ship the package. Package's constructor should initialize these data members. Ensure that the weight and cost per ounce contain positive values. Package should provide a public member function calculatecost that returns a double indicating the cost associated with shipping the package. Package's calculatecost function should determine the cost by multiplying the weight by the cost per ounce. Derived class Two DayPackage should inherit the functionality of base class Package, but also include a data member that represents a flat fee that the shipping company charges for two-day-delivery service. TwoDayPackage's constructor should receive a value to initialize this data member. Two ouppackage should redefine member function calculatecost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package's calculatecost function. Class overnightPackage should inherit directly from class Package and contain an additional data member representing an additional fee per ounce charged for overnight-delivery service. overnightPackage should redefine member function calculatecost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test program that creates objects of each type of Package and tests member function calculatecost.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free