Question

In: Computer Science

You are to write a class named Rectangle. You must use separate files for the header...

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

Solutions

Expert Solution

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)


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT