Question: create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not

create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below.

Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg description of the Shape on standard output.

Rectangle class The Rectangle class is derived from Shape and represents a rectangle. The position of the Rectangle is its lower left corner. void draw(void) is a virtual function that writes the svg description of the Rectangle on standard output, e.g.

Circle class The Circle class is derived from Shape and describes a circle. The position of the Circle is its center. void draw(void) is a virtual function that writes the svg description of the Circle on standard output, e.g.

//

// Point.h

//

#ifndef POINT_H

#define POINT_H

#include

class Point

{

public:

Point(void) : x(0), y(0) {}

Point(int xin, int yin) : x(xin), y(yin) {}

int norm2(void) const { return x*x + y*y; }

Point operator+(const Point& rhs) const;

Point operator-(const Point& rhs) const;

int x, y;

};

std::ostream& operator<<(std::ostream& os, const Point& p);

std::istream& operator>>(std::istream& is, Point& p);

#endif

//

// Shape.h

//

#ifndef SHAPE_H

#define SHAPE_H

#include "Point.h"

#include

class Circle;

class Rectangle;

class Shape

{

public:

virtual ~Shape(void) {}

virtual bool overlaps(const Shape& s) const = 0;

virtual bool overlaps(const Circle& r) const = 0;

virtual bool overlaps(const Rectangle& r) const = 0;

virtual bool fits_in(const Rectangle& r) const = 0;

virtual void draw(void) const = 0;

};

class Rectangle : public Shape

{

public:

Rectangle(void): position(Point(0,0)), width(0), height(0) {}

Rectangle(Point p, int w, int h) :

position(p), width(w), height(h) {}

virtual ~Rectangle(void);

virtual bool overlaps(const Shape& r) const;

virtual bool overlaps(const Circle& r) const;

virtual bool overlaps(const Rectangle& r) const;

virtual bool fits_in(const Rectangle& r) const;

virtual void draw(void) const;

const Point position; // position of the lower left corner

const int width, height;

};

class Circle : public Shape

{

public:

Circle(void): center(Point(0,0)), radius(0) {}

Circle(Point c, int r) : center(c),radius(r) {}

virtual ~Circle(void);

virtual bool overlaps(const Shape& s) const;

virtual bool overlaps(const Circle& r) const;

virtual bool overlaps(const Rectangle& r) const;

virtual bool fits_in(const Rectangle& r) const;

virtual void draw(void) const;

Point center;

int radius;

};

#endif

//

// testShape.cpp

//

#include "Shape.h"

#include

using namespace std;

int main()

{

Point center(10,30);

const int radius = 50;

Circle c(center,radius);

c.draw();

Point llcorner = Point(50,70);

const int width = 25;

const int height = 80;

Rectangle r1(llcorner,width,height);

r1.draw();

Rectangle r2(llcorner+Point(20,30),width,height);

r2.draw();

Circle c1(Point(60,160),10);

c1.draw();

Circle c2(llcorner-Point(5,-5),100);

c2.draw();

cout << boolalpha << r1.overlaps(r2) << endl;

cout << boolalpha << r2.overlaps(c1) << endl;

cout << boolalpha << c2.overlaps(r2) << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!