Exercise 7.1
Design a class hierarchy to represent primitive geometric shapes. Use the concept specialization. The base class of the hierarchy shall be named shape. Possible specializations of a shape are circle, ellipsis, triangle, quadrangle/rectangle, or square.
Hint: Create the class hierarchy as a UML (structure) diagram before creating any source files. Does the class hierarchy violate the Liskov Substitution Principle? Which classes are concrete? Which classes are abstract?

Exercise 7.2
Design the data representation for each of the shape specializations. Create attributes in the respective classes (e. g., a circle can be represented by its center point and its radius).
Hint: It might be beneficial to use the math3d library from exercise 4 for basic attributes in 3d space (e. g., point, vector).

Exercise 7.3
Add the following properties for all shapes to the base class:
▶ shapes have names,
▶ shapes are able to compute their area, and
▶ shapes are able to compute their bounding box.
Decide if concrete implementations belong to the base class or need to be deferred to derived classes.
Hint: Extend the UML diagram(s) of the class hierarchy with the respective methods for the requested proper- ties. Define responsibilities before amending the C++ source files.

Exercise 7.4
Objects of type shape_container must be able to hold shapes. Create the class shape_container and provide methods for inserting and removing shape objects. Test your implemention.

Exercise 7.5
A shape_container now needs to provide the following functionality: output the names of all contained shapes (using a single call) and compute the sum of all contained shapes. Implement and test your additions.

Exercise 7.6
Extend the class shape_container with the behavior of deep copy and shallow copy. The method shape_container::clone_deep creates a new shape_container, which contains true copies of the shapes in the original shape_container. In contrast, the method shape_container::clone_shallow creates a new shape_container, which contains (pointer to) shapes that are identical to the shapes in the original shape_container.

Exercise 7.7
Extend the base class of the class hierarchy for shapes with a reference-counting mechanism. Reference counting is used to avoid deleting objects, which are still in use by (other) shape containers. The functionality for insertion/removal of shapes from a shape_container can be used to manage the reference counter of a shape object. Implement the methods shape::ref and shape::unref to increment and decrement the reference counter, respectively.
Hint: You should have completed and understood exercise 7.6 before attempting to solve exercise 7.7.

Exercise 7.8
Replace the use of a shape_container with the composite design pattern. Extend the shape hierarchy with the class composite. Objects of type composite are shapes, which are composed of shapes. Provide methods for inserting and removing shapes from composite objects. Using a composite class allows now for creating hierarchical structures at run time. Create a (small) scene of shapes distributed over several composite objects. Ensure that the functionality level of the shape_container variant is preserved with the composite variant.
Hint: Hierarchical structures using composite objects suggest a parent-child relationship. Aggregate children in composite instances using an appropriate container type from the C++ standard library and justify the choice of container.

Exercise 7.9
Use the visitor design pattern to compute the complexity of a scene. Lookup the visitor design pattern and famil- iarize yourself with it and its behavior. Extend the shape hierarchy with the method accept(shape_visitor&). Implement the base class shape_visitor, which defines the interface of derived concrete visitors. Derive a class triangle_count_visitor from the base class shape_visitor. triangle_count_visitor determines the number of triangles in a run-time hierarchy of shapes. Apply an instance of triangle_count_visitor to a (non-empty) scene of your choice.
Hint: The triangle_count_visitor assumes that a rectangle/quadrangle will be represented by two triangles and that a circle or ellipsis is represented by 24 triangles.

Exercise 7.10
Develop, based on exercise 7.9, one or more additional visitor specializations. Describe requirements and functionality of these specializations as well as possible design alternatives.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

using namespace std;

class Point {
       int x, y;

class Shape {
       string name;
       int referenceCounter;

class Ellipse: public Shape {
       Point centerPoint;
       double semiMajorAxisLength;
       double semiMinorAxisLength;

class Circle: public Ellipse {

class Rectangle: public Shape {
       Point lowerLeftPoint;
       double width;
       double height;

class Square: public Rectangle {

class Triangle: public Shape {
       Point point1, point2, point3;

int main() {
    cout << "At this point we can't test anything yet. The exercise just ask for design.\n";

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats