## Description

In this programming assignment, you will create classes that model 2D geometric shapes: Rectangle2D, and Line2D.

These shapes share an abstract base class Shape2D that defines a common set of methods. The child classes also have their own methods that are appropriate for the shapes, and store their data in different ways. Rectange2D and Circle2D are defined by a bounding box with a top-left x and y, and a width and height. Line2D is defined by its two end points.

To understand the differences in how C++ and Java represent class hierarchies, you will implement these classes in both C++ and Java. Here are descriptons of the classes and the methods that they implement.

C++ Version

Define these classes in the CS_5004 namespace. Provide a unit test file and main program in the file “TestShapes.cpp”. Use your C++ implementation of Point2D from programming assignment 2.

class Shape2D

Define the pure abstract class Shape2D in a file “Shape2D.h” All of the methods of Shape2D are pure virtual, which means that they are virtual and are not implemented by Shape2D. Instead, child classes of Shape2D must implement these methods in a way that is appropriate for those classes.

Note: You will need to add a “forward reference” to class Rectangle2D in “Shape2D.h” just before defining Shape2D:

class Rectangle2D;

This is because Rectangle2D is a subclass of Shape2D, but the definition of Shape2D refers to Rectangle2D. C++ is unable to automatically resolve this kind of “circular reference.”

Method name Type Parameters Description

virtual getBounds const (pure virtual) Rectangle2D& Rectangle2D& bounds Returns the bounding box for the shape through the result parameter and the return value.

virtual setBounds (pure virtual) none float x, float y, float width, float height Sets the bounding box for the shape with top-left point (x, y), and dimensions (width, height)

virtual setBounds (pure virtual) none const Point2D& upperLeft, float width, float height Sets the bounding box for the shape with top-left point (x, y), and dimensions (width, height)

virtual setBounds (pure virtual) none const Rectangle2D& bounds Sets the bounding box for the shape

virtual containsPoint const (pure virtual) bool const Point2D& point Determines whether shape contains the point

class Rectangle2D : Shape2D

Define the concrete class Rectangle2D in a file “Rectangule2D.h” and implement the methods in a file “Rectangle2D.cpp”. All constructors, methods and the base class are public.

Hint: The constructors can call setBounds().

Field name Type Description

private x float the X coordinate of the top left of the rectangle

private y float the Y coordinate of the top left point of the rectangle

private width float the width of the rectangle

private height float the height of the rectangle

Method name Type Parameters Description

public Rectangle2D none none Constructs a rectangle at (0, 0) with dimensions (0, 0)

public Rectangle2D none float x, float y, float width, float height Constructs a rectangle with top-left point (x, y), and dimensions (width, height)

public Rectangle2D none const Point2D &topLeft, float width, float height Constructs a rectangle with top-left point, and dimensions (width, height)

public Rectangle2D none const Rectangle2D& rect Constructs a rectangle with location and dimensions of a rectangle

getX const float none Returns the X of the top left corner of the rectangle

getY const float none Returns the Y of the top left corner of the rectangle

getWidth const float none Returns the width of the rectangle

getHeight const float none Returns the height of the rectangle

getBounds const (override) Rectangle2D& Rectangle2D& bounds Returns the bounding box for the rectangle through the result parameter and the return value.

setBounds (override) none float x, float y, float width, float height Sets the bounding box for the rectangle with top-left point (x, y), and dimensions (width, height)

setBounds (override) none const Point2D& upperLeft, float width, float height Sets the bounding box for the rectangle with top-left point (x, y), and dimensions (width, height)

setBounds (override) none const Rectangle2D& bounds Sets the bounding box for the rectangle

containsPoint const (override) bool const Point2D& point Determines whether shape contains the point

class Circle2D : Shape2D

Define the concrete class Circle2D in a file “Circle2D.h” and implement the methods in a file “Circle2D.cpp”. All constructors, methods and the base class are public.

Hint: The constructors can call setBounds().

Field name Type Description

private x float the X coordinate of the top left of the circle

private y float the Y coordinate of the top left point of the circle

private width float the width of the circle

private height float the height of the circle

Method name Type Parameters Description

public Circle2D none none Constructs a circle at (0, 0) with dimensions (0, 0)

public Circle2D none float x, float y, float width, float height Constructs a circle with top-left point (x, y), and dimensions (width, height)

public Circle2D none const Point2D &topLeft, float width, float height Constructs a circle with top-left point, and dimensions (width, height)

public Circle2D none const Circle2D& rect Constructs a circle with location and dimensions of a circle

getX const float none Returns the X of the top left corner of the circle

getY const float none Returns the Y of the top left corner of the circle

getWidth const float none Returns the width of the circle

getHeight const float none Returns the height of the circle

getBounds const (override) Rectangle2D& Rectangle2D& bounds Returns the bounding box for the circle through the result parameter and the return value.

setBounds (override) none float x, float y, float width, float height Sets the bounding box for the circle with top-left point (x, y), and dimensions (width, height)

setBounds (override) none const Point2D& upperLeft, float width, float height Sets the bounding box for the circle with top-left point (x, y), and dimensions (width, height)

setBounds (override) none const Rectangle2D& bounds Sets the bounding box for the circle

containsPoint const (override) bool const Point2D& point Determines whether shape contains the point

class Line2D : Shape2D

Define this concrete class Line2D in a file “Line2D.h” and implement the methods in a file “Line2D.cpp”. All constructors, methods and the base class are public.

Field name Type Description

private x1 float the X coordinate of the first point of the line

private y1 float the Y coordinate of the first point of the line

private x2 float the X coordinate of the last point of the line

private y2 float the Y coordinate of the last point of the line

Method name Type Parameters Description

public Line2D none none Constructs a line with points (0, 0), (0, 0)

public Line2D none const Point2D& point1, const Point2D& point2 Constructs a line with points point1, point2

public Line2D none float x1, float y1, float x2, float y2 Constructs a line with points (x1, y1), (x2, y2)

public Line2D none const Line2D& line Constructs a line with points of a line

public getPoint1 const Point2D& Point2D& point1 Returns the first point of the line

public getPoint2 const Point2D& Point2D& point2 Returns the second point of the line

getBounds const (override) Rectangle2D& Rectangle2D& bounds Returns the bounding box for the line through the result parameter and the return value.

setBounds (override) none const Rectangle2D& bounds Sets the bounding box for the line, keeping the relative positions of the two points.

setBounds (override) none float x, float y, float width, float height Sets the bounding box for the line with top-left point (x, y), and dimensions (width, height), keeping the relative positions of the two points.

setBounds (override) none const Point2D& upperLeft, float width, float height Sets the bounding box for the line with top-left point (x, y), and dimensions (width, height)

containsPoint const (override) bool const Point2D& point Determines whether line contains the point