In: Computer Science
You are to write a class named Rectangle. You must use separate
files for the header (Rectangle.h) and implementation
(Rectangle.cpp) just like you did for the Distance class lab and
Deck/Card program. You have been provided the declaration for a
class named Point. Assume this class has been implemented. You are
just using this class, NOT implementing it. We have also provided a
main function that will use the Point and Rectangle classes along
with the output of this main function. You must implement your
Rectangle class such that this main function will compile and
output exactly as in the example given. The Rectangle display
represents the 4 corner Points of the Rectangle. You may declare
any private data members and declare and implement any private
helper functions for the Rectangle class. You may not change or add
anything to the Point class.
Exact output needed:
p: (1,1)
r1:
(0,2)(5,2)
(0,0)(5,0)
r2:
(1,5.1)(4.2,5.1)
(1,1)(4.2,1)
r1:
(2,-1)(7,-1)
(2,-3)(7,-3)
Given:
**************************************Main.cpp**************************************
#include "Point.h"
#include "Rectangle.h"
#include <iostream>
using namespace std;
int main() {
Point p = Point(1.0, 1.0);
Point q;
Rectangle r1 = Rectangle(q, Point(5.0, 2.0));
Rectangle r2 = Rectangle(p, 3.2, 4.1);
cout << "p: "; p.display(); cout << endl;
cout << "r1:" << endl;
cout << r1;
cout << "r2:" << endl;
cout << r2;
cout << endl;
r1.move(2.0, -3.0);
cout << "r1:" << endl;
cout << r1;
cout << endl;
return 0;
}
**************************************Point.h**************************************
#ifndef __POINT_H__
#define __POINT_H__
class Point {
private:
double xCoord;
double yCoord;
public:
Point();
Point(double, double);
void display() const; // Display point as (x,y)
double getX() const; // Returns the x coordinate's value
double getY() const; // Returns the y coordinate's value
// Moves the Point dx units along the x axis and dy units along the
y axis
void move(double dx, double dy);
};
#endif
********************NEED: Rectangle.h and Rectangle.cpp file
Screenshot
Program
Point.h
//Create class point
#ifndef __POINT_H__
#define __POINT_H__
#include<iostream>
using namespace std;
class Point {
private:
double xCoord;
double yCoord;
public:
Point();
Point(double, double);
void display() const; // Display point as (x,y)
double getX() const; // Returns the x coordinate's
value
double getY() const; // Returns the y coordinate's
value
// Moves the Point dx units along the x axis and dy
units along the y axis
void move(double dx, double dy);
};
#endif
Point.cpp
//Implementation of point class
#include "Point.h"
Point::Point() {
xCoord = 0;
yCoord = 0;
}
Point::Point(double x, double y) {
xCoord = x;
yCoord = y;
}
// Display point as (x,y)
void Point::display() const {
cout << "(" << xCoord << ","
<< yCoord << ")";
}
// Returns the x coordinate's value
double Point::getX() const {
return xCoord;
}
// Returns the y coordinate's value
double Point::getY() const {
return yCoord;
}
// Moves the Point dx units along the x axis and dy units along the
y axis
void Point::move(double dx, double dy) {
xCoord += dx;
yCoord += dy;
}
Rectangle.h
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
#include "Point.h"
using namespace std;
class Rectangle {
//Instance members
private:
Point p1, p2, p3, p4;
//Member functions
public:
//Constructor
Rectangle(Point, Point);
Rectangle(Point, double, double);
//Move each points
void move(double, double);
//Output operator overload
friend ostream& operator<<(ostream& out,
const Rectangle r) {
r.p4.display();
r.p3.display();
out<< endl;
r.p1.display();
r.p2.display();
out << endl;
return out;
}
};
#endif
Rectangle.cpp
//Implementation of Rectangle
#include "Rectangle.h"
//Constructors
Rectangle::Rectangle(Point point1, Point point2) {
p1 = point1;
p2 = Point(point2.getX(),point1.getY());
p3 = point2;
p4= Point(point1.getX(), point2.getY());
}
Rectangle::Rectangle(Point point1, double dx, double dy) {
p1 = point1;
point1.move(dx,0);
p2 = point1;
point1.move(0, dy);
p3 = point1;
p4 = p1;
p4.move(0, dy);
}
//Move each points
void Rectangle::move(double dx, double dy) {
p1.move(dx, dy);
p2.move(dx, dy);
p3.move(dx, dy);
p4.move(dx, dy);
}
main.cpp
#include "Point.h"
#include "Rectangle.h"
#include <iostream>
using namespace std;
int main()
{
//Create parameterized object of Point
Point p = Point(1.0, 1.0);
//Create default object of Point
Point q;
//Using different constructors of rectangle create
objects
Rectangle r1 = Rectangle(q, Point(5.0, 2.0));
Rectangle r2 = Rectangle(p, 3.2, 4.1);
//Display point
cout << "p: "; p.display(); cout <<
endl;
//Display Rectangle1
cout << "r1:" << endl;
cout << r1;
cout << "r2:" << endl;
//Display Rectangle2
cout << r2;
//Move rectangle points and dsplay
r1.move(2.0, -3.0);
cout << "r1:" << endl;
cout << r1;
return 0;
}
----------------------------------------------------------------------------
Output
p: (1,1)
r1:
(0,2)(5,2)
(0,0)(5,0)
r2:
(1,5.1)(4.2,5.1)
(1,1)(4.2,1)
r1:
(2,-1)(7,-1)
(2,-3)(7,-3)