Question
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
Question 2) Use Point class to define another class called Rectangle which has only two data members of type Point named Uppe
Question 3: Write a driver program to test your class. Proceed as follows: Define object poin Change the values of all the at
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 initializer constructor A Copy Constructor Getters and setters for private data member x and y. A function setXYO toset both x and y coordinates of a Point A function getMagnitude) which returns v(xty). You can use the built- in sqrt() function in math library to compute the square root A function print) which prints "(x,y)" of this nstance . A public static getter function for getNumOfPoints float getDistance(Point &p2) that computes the distance between original object and p2 object and returns it Note that All member functions that do not modify data members should be made constant
Question 2) Use Point class to define another class called Rectangle which has only two data members of type Point named UpperLeftPoint and BottomRightPoint a. Define two constructors one is default and one is initializer b. Define member functions named getLength of rectangle c. Define member functions named get Width of rectangle d. Define member functions named getArea of rectangle. Note, the values of the functions are calculated based on the two points of the rectangle e. Define member functions named isLarger(Rectangle &R2), that compares the original object with R2 based on area, and t bool isOverlapped(Rectangle &R2) that returns true if original g. Define member functions named Print0, that prints the returns true if its area is larger, or false otherwise object is overlapped with R2 object and false otherwise rectangle Implement the functions described in the previous two questions and note the following: Make sure to validate the input in the setters If an invalid value is provided, an appropriate error message should be shown
Question 3: Write a driver program to test your class. Proceed as follows: Define object poin Change the values of all the attributes of point Change the position of the two objects you have created using setXYO function. Define an array of five objects of type point and call it myArray Prompt the user to enter the value of each attribute for each object. Move one of the objects you have created to new coordinates using values entered from the user. Print the details of all objects you have created .Print the order in which the destructor was called for each object Create two object, rectl and rect2 of the class Rectangle and print the area using the member functions Print the details of the largest rectangle. Using member function isOverlapped to check if the two rectangle objects you defined are overlapping or not and print an appropriate message UpperLeftP oint (x,y) Rattam Di
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//point.h

#ifndef POINT_H_

#define POINT_H_

#include <iostream>

#include <cmath>

using namespace std;

class Point

{

private:

       int x, y;

       static int numOfPoints;

       const int ID = numOfPoints+1;

public:

       Point();

       Point(int x, int y);

       Point(const Point &point);

       ~Point();

       int getX() const;

       int getY() const;

       void setX(int x);

       void setY(int y);

       void setXY(int x, int y);

       float getMagnitude() const;

       void print() const;

       static int getNumOfPoints();

       float getDistance(Point &p2) const;

};

int Point::numOfPoints = 0;

Point::Point()

{

       x = 0;

       y = 0;

       numOfPoints++;

}

Point::Point(int x, int y)

{

       this->x = x;

       this->y = y;

       numOfPoints++;

}

Point::Point(const Point &point)

{

       x = point.x;

       y = point.y;

       numOfPoints++;

}

Point::~Point()

{

       cout<<"\nDestructor for point : "<<ID<<" called ";

       numOfPoints--;

}

int Point::getX() const

{

       return x;

}

int Point::getY() const

{

       return y;

}

void Point::setX(int x)

{

       if(x >= 0)

             this->x = x;

       else

       {

             cout<<"X cannot be negative. Setting X to 0 ";

             this->x = 0;

       }

}

void Point::setY(int y)

{

       if(y >= 0)

             this->y = y;

       else

       {

             cout<<"Y cannot be negative. Setting Y to 0";

             this->y = 0;

       }

}

void Point::setXY(int x, int y)

{

       setX(x);

       setY(y);

}

float Point::getMagnitude() const

{

       return sqrt((x*x) + (y*y));

}

void Point::print() const

{

       cout<<"("<<x<<","<<y<<")";

}

int Point::getNumOfPoints()

{

       return numOfPoints;

}

float Point::getDistance(Point &p2) const

{

       int x_diff = x-p2.x;

       int y_diff = y-p2.y;

       return(sqrt((x_diff*x_diff)+(y_diff*y_diff)));

}

#endif /* POINT_H_ */

//end of point.h

// rectangle.h

#ifndef RECTANGLE_H_

#define RECTANGLE_H_

#include "point.h"

class Rectangle

{

private:

               Point upperLeft, bottomRight;

public:

               Rectangle();

               Rectangle(const Point &upperLeft, const Point &bottomLeft);

               int getLength() const;

               int getWidth() const;

               int getArea() const;

               bool isLarger(Rectangle &r2) const;

               bool isOverlapping(Rectangle &r2) const;

               void print() const;

};

Rectangle::Rectangle()

{

               upperLeft.setXY(0,1);

               bottomRight.setXY(1,0);

}

Rectangle::Rectangle(const Point &upperLeft, const Point &bottomRight)

{

               this->upperLeft.setXY(upperLeft.getX(),upperLeft.getY());

               this->bottomRight.setXY(bottomRight.getX(),bottomRight.getY());

}

int Rectangle::getLength() const

{

               return(bottomRight.getX()-upperLeft.getX());

}

int Rectangle::getWidth() const

{

               return(upperLeft.getY() - bottomRight.getY());

}

int Rectangle::getArea() const

{

               return (getLength()*getWidth());

}

bool Rectangle:: isLarger(Rectangle &r2) const

{

               return(getArea() > r2.getArea());

}

bool Rectangle:: isOverlapping(Rectangle &r2) const

{

               if(((upperLeft.getX() >= r2.upperLeft.getX() && upperLeft.getX() <= r2.bottomRight.getX()) &&

                                             (upperLeft.getY() >= r2.bottomRight.getY() && upperLeft.getY() <= r2.upperLeft.getY())) ||

                                             ((bottomRight.getX() >= r2.upperLeft.getX() && bottomRight.getX() <= r2.bottomRight.getX()) &&

                                             (bottomRight.getY() >= r2.bottomRight.getY() && bottomRight.getY() <= r2.upperLeft.getY())) ||

                                             ((upperLeft.getX() >= r2.upperLeft.getX() && upperLeft.getX() <= r2.bottomRight.getX()) &&

                                             (bottomRight.getY() >= r2.bottomRight.getY() && bottomRight.getY() <= r2.upperLeft.getY())) ||

                                             ((bottomRight.getX() >= r2.upperLeft.getX() && bottomRight.getX() <= r2.bottomRight.getX()) &&

                                             (upperLeft.getY() >= r2.bottomRight.getY() && upperLeft.getY() <= r2.upperLeft.getY())))

                              return true;

               return false;

}

void Rectangle::print() const

{

               cout<<"Upperleft : ";

               upperLeft.print();

               cout<<endl<<"BottomRight : ";

               bottomRight.print();

               cout<<endl<<"Length : "<<getLength()<<endl<<"Width : "<<getWidth() ;

               cout<<endl<<"Area : "<<getArea();

}

#endif /* RECTANGLE_H_ */

//end of rectangle.h

// main.cpp

#include "rectangle.h"

#include <iostream>

using namespace std;

int main()

{

               Point point1;

               cout<<"Point1 : ";

               point1.print();

               point1.setX(3);

               point1.setY(5);

               cout<<"\nUpdated Point1 : ";

               point1.print();

               point1.setXY(6,3);

               cout<<"\nUpdated Point1 : ";

               point1.print();

               Point myArray[5];

               int x, y;

               for(int i=0;i<5;i++)

               {

                              cout<<"\nEnter x-value for Point-"<<(i+1)<<" : ";

                              cin>>x;

                              cout<<"Enter y-value for Point-"<<(i+1)<<" : ";

                              cin>>y;

                              myArray[i].setXY(x,y);

               }

               cout<<"Enter new x-value : ";

               cin>>x;

               cout<<"Enter new y-value : ";

               cin>>y;

               myArray[2].setXY(x,y);

               for(int i=0;i<5;i++)

               {

                              cout<<"\nPoint-"<<(i+1)<<" : ";

                              myArray[i].print();

               }

               Point upperLeft1(2,7), bottomRight1(10,4), upperLeft2(1,6), bottomRight2(8,3);

               Rectangle rect1(upperLeft1,bottomRight1), rect2(upperLeft2,bottomRight2);

               cout<<"\nRectangle 1 area : "<<rect1.getArea()<<endl;

               cout<<"Rectangle 2 area : "<<rect2.getArea()<<endl;

               cout<<"Larger Rectangle :"<<endl;

               if(rect1.isLarger(rect2))

                              rect1.print();

               else

                              rect2.print();

               cout<<endl;

               if(rect1.isOverlapping(rect2))

                              cout<<"Rectangle1 is overlapping with Rectangle2";

               else

                              cout<<"Rectangle1 is not overlapping with Rectangle2";

               return 0;

}

// end of main.cpp

Output:

Point1 (0,0) Updated Point1 : (3,5) Updated Pointi (6,3) Enter x-value for Point-1: 1 Enter y-value for Point-1 2 Enter x-val

Rectangle1 is overlapping with Rectangle2 Destructor for point 14 called Destructor for point 13 called Destructor for point

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

    C++ Question 2) Use Point class to define another class called Rectangle which has only two data members of type Point named UpperLeftPoint and BottomRightPoint a. Define two constructors one is default and one is initializer b. Define member functions named getLength of rectangle c. Define member functions named get Width of rectangle d. Define member functions named getArea of rectangle. Note, the values of the functions are calculated based on the two points of the rectangle e. Define member...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. . A no-arg constructor that creates a point (0, 0) .A constructor that constructs a point with specified coordinates. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

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

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

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

  • I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++...

    I am having difficulty with completing this task. thank you. Task 10.1 (a) Define a C++ base class named Rectangle containing length and width data members. From this class, derive a class named Box with another data member named depth. The member functions for the base class Rectangle should consist of a constructor and an area() function. The derived class Box should have a constructor, a volume() function and an override function named area() that returns the surface area of...

  • 15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor init...

    15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor initializes the data members to 0. You need to use the operator?: to test whether x and y are positive. If not, assign them to 0. The function printPoint prints the message "(X, Y)" where X and Y are two integers. In the main program, first input the number...

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