Question

create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

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.

<rect x="50" y="70" width="25" height="80"/>

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. <circle cx="10" cy="30" r="50"/>

//

// Point.h

//

#ifndef POINT_H

#define POINT_H

#include<iostream>

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 <iostream>

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 <iostream>

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

#include "Shape.h"
// Rectangle class implementation
void Rectangle::draw(void) const{
std::cout<<"<rect x=\""<<position.x<<"\" y=\""<<position.y<<"\" width=\""<<width<<"\" height=\""<<height<<"\"/>";
}
bool Rectangle::overlaps(const Shape& r) const{
  
}
bool Rectangle::overlaps(const Circle& r) const{
  
}
bool Rectangle::overlaps(const Rectangle& r) const{
  
}
bool Rectangle::fits_in(const Rectangle& r) const{
if(r.width >= width && r.height >= height ){
return true;
}else{
return false;
}
}
// Circle class implementation
void Circle::draw(void) const{
std::cout<<"<circle cx=\""<<center.y<<"\" cy=\""<<center.y<<"\" r=\""<<radius<<"\"/>";
}

bool Circle::overlaps(const Shape& r) const{
  
}
bool Circle::overlaps(const Circle& r) const{
  
}
bool Circle::overlaps(const Rectangle& r) const{
  
}
bool Circle::fits_in(const Rectangle& r) const{
if(r.width >= 2*radius && r.height >= 2*radius ){ // if diameter is less than width and height
return true;
}else{
return false;
}
}

I have added definition for fitsin and draw methods as per the description shared above.

Feel free to reach out regarding any queries . And please do rate the answer . Thank you .

Add a comment
Know the answer?
Add Answer to:
create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

  • C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is...

    C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is where the shapes are being stored. I'm assuming an array, but I'm not sure how sizing would work. Any help is appreciated, thanks! I have also attached the .h file we must use. Prompt: The goal of HW2 is to implement classes representing shapes. A given program will use this class to create shapes at arbitrary locations in the x-y plane and move them....

  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement...

    C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT