//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:
Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...
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 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 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: 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 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 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. 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++ 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 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...