【问题标题】:How to use classes to create rectangles如何使用类创建矩形
【发布时间】:2019-04-04 15:31:12
【问题描述】:

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

  1. 私有数据应包含 4 个成员: 长度 宽度 区域 周长

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

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

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

    getWidth 返回宽度

    getPerimeter 返回周长

    getArea 返回区域

    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) “未使用私有数据中的面积和周长” 每次修改width 和/或length 时都计算areaperimeter,然后返回它们而不是每次都计算。 2)你需要void Rectangle::printObjet(),然后是cout &lt;&lt; " Width = " &lt;&lt; .... 3)你已经使用了这个构造函数:Rectangle box1;
  • @DimChtz 代码甚至没有编译
  • 编译错误太多了。请检查您的代码,复制可以编译的代码,然后粘贴。

标签: c++ class


【解决方案1】:

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) {  }

【讨论】:

  • 在改变你所说的东西后,唯一的编译错误是在第 34 行的第二个矩形“'Rectangle' 的外线定义与'Rectangle' 中的任何声明不匹配”,并且同一段代码不会初始化面积或周长,并且从未使用过。
  • 在编译失败后的实际终端中显示“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: 候选是:constexpr Rectangle::Rectangle(Rectangle&&)"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多