C++ 矩形类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3321486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:37:52  来源:igfitidea点击:

Rectangle class

c++c++builder

提问by John

Hi everyone Im very new to c++ and may be in over my head on this problem im trying to solve. A good visual explanation and solution to my errors or even better a revised source code is all that I ask of. Thanks to everyone who invest there interest into my question.

大家好,我对 C++ 很陌生,可能在我试图解决的这个问题上不知所措。我所要求的只是一个很好的视觉解释和对我的错误的解决方案,或者更好的是修改过的源代码。感谢所有对我的问题感兴趣的人。

Heres the problem: Design a class named rectangle to represent a rectangle. The class must contain:

问题来了:设计一个名为 rectangle 的类来表示一个矩形。该类必须包含:

  • Two double data fields named width and height that specify the width and height of the rectangle.
  • A no-arg constructor that creates a default rectangle with width 1 and height 1.
  • A constructor that creates a rectangle with the specified width and height
  • The accessor and mutator functions for all data fields
  • The function named get Area() that returns the area of this rectangle
  • A function named getPerimeter() that returns the peremter.
  • 两个名为 width 和 height 的双精度数据字段,用于指定矩形的宽度和高度。
  • 一个无参数构造函数,它创建一个宽度为 1 和高度为 1 的默认矩形。
  • 创建具有指定宽度和高度的矩形的构造函数
  • 所有数据字段的访问器和修改器函数
  • 返回此矩形面积的名为 get Area() 的函数
  • 一个名为 getPerimeter() 的函数,它返回 peremter。

Draw the UML diagram for the class. Implement the class. Write a test progranm that creates two rectangle obejects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the second. Display the properties of both objects and find their areas and perimeters.

为该类绘制 UML 图。实现类。编写一个测试程序,创建两个矩形对象。为第一个对象分配宽度 4 和高度 40,为第二个对象分配宽度 3.5 和高度 35.9。显示两个对象的属性并找到它们的面积和周长。

Heres what I have so far:

这是我到目前为止所拥有的:

#include <iostream>
using namespace std;

class Rectangle
{    
public:      
  double height;

public:
  double width;
  Rectangle()
  {
      width = 4;        
  }

  rectangle(double newArea)

  double height;
  height()
  (
      height = 40
      {
          {
          area = height* width;
          }


  double getArea()
  {
    return Area;
  }

  bool isOn()
  {
    return on;
  }


  double getPerimeter()
  {
    return Perimeter;
  }

  void setPerimeter(double radius)

  cout << "The area of the  Rectangle" 
 << rectangle1.area<<"is"<<rectangle1.getArea()<< endl;
 cout<<"The area of  the Rectangle"
 <<rectangle.area2.area<<"is"<<rectangle2.getArea()<<endl; 


  return 0;
}

回答by James

If you'd like to learn whats going on here, check this out

如果你想了解这里发生了什么,看看这个

#include <iostream> 
using namespace std; 

class Rectangle{ 
private:        // In nearly all cases you want to keep your member variables private
  double height;    // This allows you to be certain of how they are accessed. 
  double width;     // This provides a level of security to the class.

public:         // Only need one public:
  // Constructors are called when you define a new variable of type Rectangle
  Rectangle()       // No arguments means this constructor takes no extra input when called.
  { 
    width = 1.0;        // Sets width and height to 1
    height = 1.0;
  } 
  Rectangle(double a_width, double a_height) {  // Constructor needs exact(case-sensitive) match of class-name
    /* Set width and hieght to the arguments passed into the constructor in here.*/         
  } 
  // Accessor / Mutator methods are more easily called get/set methods.
  double getHeight(){   // Get height called on rectangle class
    return height;      // Returns the value of the class member "height"
  }

  /*Make a method to get the width here.*/

  // In c++ you can return the results of equations directly
  // i.e. 
  // int x = 2;
  // return x*x;  // returns 4
  // Try something simliar for getArea() and getPerimeter();

  double getArea() const 
  { 
      return /*area equation goes here*/ ;
  } 
  double getPerimeter() const
  {                     
    return /*perimeter equation goes here*/; 
  } 
}
  // You should split these displaying statements into the main function,
  // Which is what you meant to do, right? ;)
  int main(){
    // In order to use your new class, you need to declare a variable of its type
      Rectangle rectangle1;     
      // When you declare a new variable without any arguments its default constructor is called.
      // This means that the rectangle class decides to use the height = 1; width = 1; constructor from above.
      // You can check this using your getHeight() and getWidth(), when you implement them.

      cout << "The area of the Rectangle is" << rectangle1.getArea() << endl; 

  return 0; 
} 

Ah reminds me of TAing CSCI1100

啊让我想起了TAing CSCI1100

回答by josh

Here is a working solution,

这是一个有效的解决方案,

Source Code:

源代码:

#include <iostream>

using namespace std;

class Rectangle {
    private:
        double width;
        double height;
    public:
        Rectangle(double w=1, double h=1);

        double getWidth();
        void setWidth(double w);

        double getHeight();
        void setHeight(double h);

        double getArea();
        double getPerimeter();
};

Rectangle::Rectangle(double w, double h):width(w),height(h) {}

double Rectangle::getWidth() { return width; }

void Rectangle::setWidth(double w) {width = w; }

double Rectangle::getHeight() { return height; }

void Rectangle::setHeight(double h) { height = h; }

double Rectangle::getArea() { return width*height; }

double Rectangle::getPerimeter() { return 2*(width+height); }

int main()
{
    Rectangle r1(4,40);
    Rectangle r2(3.5,35.9);

    cout << "Rectangle #1 :: width[" << r1.getWidth() << "] height[" << r1.getHeight() << "] area[" << r1.getArea() << "] perimeter[" << r1.getPerimeter() << "]" << endl;
    cout << "Rectangle #2 :: width[" << r2.getWidth() << "] height[" << r2.getHeight() << "] area[" << r2.getArea() << "] perimeter[" << r2.getPerimeter() << "]" << endl;
}

Output:

输出:

Rectangle #1 :: width[4] height[40] area[160] perimeter[88]
Rectangle #2 :: width[3.5] height[35.9] area[125.65] perimeter[78.8]

回答by Brendan Long

Step 1: Remove all code that isn't in the spec:

第 1 步:删除不在规范中的所有代码:

class Rectangle {

private:
    double height;
    double width;

public:
    Rectangle() {
        // fill in code here
    }

    Rectangle(double width, double height) {
        // fill in code here
    }

    double getArea() {
        // fill in code here
    }

    double getPerimeter() {
         // fill in code here
    }

    double getWidth() {
        // fill in code here
    }

    void setWidth(double newWidth) {
        // fill in code here
    }

    double getHeight() {
        // fill in code here
    }

    void setHeight(double newHeight) {
        // fill in code here
    }

};

The rest of your code is just making this more complicated. You don't need an area or perimeter variable, and a constructor accepting an area as an argument doesn't make sense (you seem to be treating your rectangle like a circle).

您的其余代码只是使这变得更加复杂。您不需要面积或周长变量,并且接受面积作为参数的构造函数没有意义(您似乎将矩形视为圆形)。