如何使用类创建矩形

How to use classes to create rectangles

本文关键字:创建 何使用      更新时间:2023-10-16

我需要创建一个程序,该程序使用类从用户输入中创建三个三角形,包括以下规范:

  1. 私人数据应由4名成员组成: 长度 宽度 面积 周边

  2. 具有 1 个具有以下参数的构造函数:长度和宽度。

  3. 有 2 个突变体: 设置长度以设置/修改长度 和设置宽度以设置/修改宽度。

  4. 有 5 个访问器: getLength 以返回长度

    getWidth 返回宽度

    获取周长以返回周长

    获取区域以返回区域

    printObjet 打印矩形的表示形式(使用相同的 前身的输出标准)

对于额外的学分,存储您在矢量中创建的 10 个矩形对象,并通过矢量进行排序以打印出 10 个矩形对象。一旦我学会了如何完成主要任务,我相信我可以自己完成,尽管额外的帮助不会出错。

使用 DimChtz 的更改进行编辑后,代码如下。

#include <iostream>
using namespace std;
// Rectangle class declaration.
class Rectangle
{
private:
double width;
double length;
double area;
double perimeter;

public:
void setWidth(double);
void setLength(double);
double getWidth();
double getLength();
double getArea();
double getPerimeter();
void printObjet();
void calc();
Rectangle();
};
//beginning class constructor
Rectangle::Rectangle() {
this->width = 0;
this->length = 0;
this->area = 0;
this->perimeter = 0;
}
// main class constructor
Rectangle::Rectangle(double width, double length)
{   this->width = width;
this->length = length;
this->calc();
}
//calc calculates the area and perimeter from width and length.
void Rectangle::calc() {
this->area = this->width * this->length;
this->perimeter = 2 * (this->width + this->length); }
// the class functions definition
// setWidth assigns its argument to the private member width.
void Rectangle::setWidth(double width)
{   this->width = width;
this->calc(); }
// setLength assigns its argument to the private member length.
void Rectangle::setLength(double length)
{   this->length = length;
this->calc(); }
// getLength returns the value in the private member length.
double Rectangle::getLength()
{   return length; }
// getWidth returns the value in the private member width.
double Rectangle::getWidth()
{   return width; }
// getPerimeter returns parameter of box.
double Rectangle::getPerimeter()
{   return  this->perimeter; }
// getArea returns area of box.
double Rectangle::getArea()
{   return this->area; }
// printObjet prints the data of the rectangle
void Rectangle::printObjet()
{   cout << "  Width = " << getWidth() << endl;
cout << "  Length = " << getLength() << endl;
cout << "  Perimeter = " << getPerimeter() << endl;
cout << "  Area = " << getArea() << endl << endl; }

// the main program
int main()
{
double width;     // Local variable for width.
double length;   // Local variable for length.
// create object using constructor
Rectangle   box1;
Rectangle   box2;
Rectangle   box3;
// Get the 1st rectangle's width and length from the user.
cout << "Enter the length of the 1st rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 1st rectangle:  ";
cin >> width;
cout << width << endl;

// Store the width and length of the rectangle in the box1 object.
box1.setWidth(width);
box1.setLength(length);
// Display box1 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box1.printObjet();

// Get the 2nd rectangle's width and length from the user.
cout << "Enter the length of the 2nd rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 2nd rectangle:  ";
cin >> width;
cout << width << endl;
// Store the width and length of the rectangle in the box1 object.
box2.setWidth(width);
box2.setLength(length);
// Display box2 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box2.printObjet();
// Get the 3rd rectangle's width and length from the user.
cout << "Enter the length of the 3rd rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 3rd rectangle:  ";
cin >> width;
cout << width << endl;
// Store the width and length of the rectangle in the box1 object.
box3.setWidth(width);
box3.setLength(length);
// Display box3 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box3.printObjet();
return 0;
}

编译错误现在是

34:1: error: prototype for 'Rectangle::Rectangle(double, double)' does not match any in class 'Rectangle'
Rectangle::Rectangle(double width, double length)
5:7: error: candidates are: constexpr Rectangle::Rectangle(Rectangle&&)
class Rectangle
5:7: error:                 constexpr Rectangle::Rectangle(const Rectangle&)
27:1: error:                 Rectangle::Rectangle()

1)事实上,你不使用areaperimeter。每次修改width和/或length时,都应同时计算areaperimeter

Rectangle::Rectangle(double width, double length) {
this->width = width;
this->length = length;
this->calc();
}
void Rectangle::setWidth(double width) {
this->width = width;
this->calc();
}
void Rectangle::setLength(double length) {
this->length = length;
this->calc();
}
double Rectangle::getPerimeter() { return  this->perimeter; }
double Rectangle::getArea() { return this->area; }

现在你需要一个新的私有函数:

void Rectangle::calc() {
this->area = this->width * this->length;
this->perimeter = 2 * (this->width + this->length);
}

2)你需要稍微改变一下printObjet

void printObjet();
// ...
void Rectangle::printObjet() {
cout << "  Width = " << getWidth() << endl << "  Length = " << getLength() << endl << "  Perimeter = " << getPerimeter() << endl<< "  Area = " << getArea() << endl << endl;
}

然后更改:

cout << box1.printObjet();

自:

box1.printObjet();

3)你需要这个:

Rectangle();
// ...
Rectangle::Rectangle() {
this->width = 0;
this->length = 0;
this->area = 0;
this->perimeter = 0;
}

或者简单地:

Rectangle::Rectangle() : width(0), length(0), area(0), perimeter(0) {  }

甚至:

Rectangle::Rectangle() : Rectangle(0.0, 0.0) {  }