1、Experiment purpose (1)Write the txt file. (2)Class definition. (3)Function application. (4)Selections. (5)Loops 2、Experiment task Project 1: Define the Rectangle2D class that contains: Two double data fields named x and y that specify the center of the rectangle with constant get functions and set functions. (Assume that the rectangle sides are parallel to x- or y-axes.) The double data fields width and height with constant get functions and set functions. A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and 1 for both width and height. A constructor that creates a rectangle with the specified x, y, width, and height. A constant function getArea() that returns the area of the rectangle. A constant function getPerimeter() that returns the perimeter of the rectangle. A constant function contains(double x, double y) that returns true if the specified point (x, y) is inside this rectangle. See Figure a. A constant function contains(const Rectangle2D &r) that returns true if the specified rectangle is inside this rectangle. See Figure b. A constant function overlaps(const Rectangle2D &r) that returns true if the specified rectangle overlaps with this rectangle. See Figure c. Draw the UML for the class. Implement the class. Write a test program that creates three Rectangle2D objects r1(2, 2, 5.5, 4.9), r2(4, 5, 10.5, 3.2)), and r3(3, 5, 2.3, 5.4), and displays r1’s area and perimeter, and displays the result of r1.contains(3, 3), r1.contains(r2), and r1.overlaps(r3). And save all these results in the txt file that is called Result.txt.写一段c++代码
时间: 2024-04-27 21:21:57 浏览: 101
下面是C++代码实现:
#include <iostream>
#include <fstream>
using namespace std;
class Rectangle2D {
private:
double x, y;
double width, height;
public:
Rectangle2D() {
x = y = 0;
width = height = 1;
}
Rectangle2D(double x, double y, double width, double height) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
double getX() const {
return x;
}
double getY() const {
return y;
}
double getWidth() const {
return width;
}
double getHeight() const {
return height;
}
void setX(double x) {
this->x = x;
}
void setY(double y) {
this->y = y;
}
void setWidth(double width) {
this->width = width;
}
void setHeight(double height) {
this->height = height;
}
double getArea() const {
return width * height;
}
double getPerimeter() const {
return 2 * (width + height);
}
bool contains(double x, double y) const {
return abs(x - this->x) <= width / 2 && abs(y - this->y) <= height / 2;
}
bool contains(const Rectangle2D& r) const {
return contains(r.getX() - r.getWidth() / 2, r.getY() - r.getHeight() / 2) &&
contains(r.getX() + r.getWidth() / 2, r.getY() + r.getHeight() / 2);
}
bool overlaps(const Rectangle2D& r) const {
return abs(x - r.getX()) <= (width + r.getWidth()) / 2 &&
abs(y - r.getY()) <= (height + r.getHeight()) / 2;
}
};
int main() {
Rectangle2D r1(2, 2, 5.5, 4.9);
Rectangle2D r2(4, 5, 10.5, 3.2);
Rectangle2D r3(3, 5, 2.3, 5.4);
ofstream outFile("Result.txt"); // 打开文件
if (outFile.is_open()) {
outFile << "r1's area: " << r1.getArea() << endl;
outFile << "r1's perimeter: " << r1.getPerimeter() << endl;
outFile << "r1 contains (3, 3): " << (r1.contains(3, 3) ? "true" : "false") << endl;
outFile << "r1 contains r2: " << (r1.contains(r2) ? "true" : "false") << endl;
outFile << "r1 overlaps r3: " << (r1.overlaps(r3) ? "true" : "false") << endl;
outFile.close(); // 关闭文件
} else {
cout << "Unable to open file" << endl;
}
return 0;
}
运行完毕后,会在当前目录下生成一个名为Result.txt的文件,里面包含了程序输出的结果。
阅读全文