Question
C++
Question 2) Use Point class to define another class called Rectangle which has only two data members of type Point named Uppe
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am assuming my own point class because it is not given in the question description. I am also assuming my own algorithm for printing the rectangle as no information is given regarding how the rectangles have to be printed.

#include <bits/stdc++.h>
using namespace std;
class Point{
    private:
        int xcord , ycord;
    public:
        Point(int x, int y) : xcord(x) , ycord(y) {};
        int getX(){
            return xcord;
            }
        int getY(){
            return ycord;
            }
};

class Rectangle{
    public:
        Rectangle() {};
        Point *UpperLeftPoint , *BottomRightPoint;
        Rectangle(int x1,int y1,int x2,int y2){
            if(x1<=x2 and y1>=y2)
            {
                UpperLeftPoint = new Point(x1,y1);
                BottomRightPoint = new Point(x2,y2);
            }
            else if(x2<=x1 and y2>=y1)
            {
                UpperLeftPoint = new Point(x2,y2);
                BottomRightPoint = new Point(x1,y1);
            }
            else
            {
                cout << "ERROR: Invalid Input Points";
            }
        }
        int getLength(){
            return BottomRightPoint->getX() - UpperLeftPoint->getX();
        }
        int getWidth(){
            return UpperLeftPoint->getY() - BottomRightPoint->getY();
        }
        int getArea(){
            return getLength() * getWidth();
        }
        bool isLarger(Rectangle &R2)
        {
            // returns true if this rectangle is larger than input rectangle.
            return getArea() > R2.getArea();
        }
        bool isOverlapped(Rectangle &R2)
        {
            // Checking if one of the rectangles is on the left side or above the other.
            if(UpperLeftPoint->getX() > R2.BottomRightPoint->getX())
                return false;
            if(UpperLeftPoint->getY() < R2.BottomRightPoint->getY())
                return false;
            if(R2.UpperLeftPoint->getX() > BottomRightPoint->getX())
                return false;
            if(R2.UpperLeftPoint->getY() < BottomRightPoint->getY())
                return false;
            return true;
        }
        void Print()
        {
            int l = getLength() , w = getWidth();
            for (int i=0; i<w; i++)
            {
                cout << "\n";
                for (int j=0; j<l; j++)
                {
                    if (i == 0 || i == w-1 || j== 0 || j == l-1)
                        cout << "@";
                    else
                        cout << " ";
                }
            }
        }
};
int main()
{
    Rectangle* r1 = new Rectangle(3,8,6,2);
    Rectangle* r2 = new Rectangle(4,7,6,2);
    if(r1->isOverlapped(*r2))
        r1->Print();
}

Add a comment
Know the answer?
Add Answer to:
Question 2) Use Point class to define another class called Rectangle which has only two data memb...
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
  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • 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...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Implement a new Generic class called a ShapeBox. This class will have two instance data, a...

    Implement a new Generic class called a ShapeBox. This class will have two instance data, a Shape object (as in the graphics Shape like Circle, Rectangle, Ellipse), and a double to store the Shape's area. The specific type of Shape will be specified via Generics so that your class will need a placeholder for the type of Shape. This placeholder must extend Shape. Additionally, ShapeBox will implement the Comparable interface requiring that you implement a compareTo method to compare this...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

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