【问题标题】:Rectangle class矩形类
【发布时间】:2010-07-23 19:03:05
【问题描述】:

大家好,我对 c++ 很陌生,可能对我试图解决的这个问题感到头疼。我所要求的只是对我的错误的一个很好的视觉解释和解决方案,甚至更好的是修改后的源代码。感谢所有对我的问题感兴趣的人。

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

  • 两个名为 width 和 height 的双精度数据字段用于指定矩形的宽度和高度。
  • 一个无参数构造函数,创建一个宽度为 1 高度为 1 的默认矩形。
  • 创建具有指定宽度和高度的矩形的构造函数
  • 所有数据字段的访问器和修改器函数
  • 名为 get Area() 的函数返回此矩形的面积
  • 一个名为 getPerimeter() 的函数,它返回 peremter。

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

这是我目前所拥有的:

#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;
}

【问题讨论】:

  • 如果您在代码前添加 4 个空格,它将被格式化为代码。我怀疑您还有其他一些问题(查看以rectangle(double newArea) 开头的代码)。 setPerimeter 函数还有什么用?上次我检查时,矩形没有半径(函数定义以 { /* code goes here */ }; 结尾)
  • 您还应该在发布问题时解释什么不起作用。不是在编译吗?它的某些部分不起作用吗?是不是想不出一部分怎么办?
  • 这是我今天从你那里看到的第三个问题,它包括一个家庭作业和一些代码,格式不正确,并且没有任何迹象表明你实际上发现了什么错误。您能否验证所发布的内容是您所写的,并告诉我们实际出了什么问题?
  • 暑期课程快结束了,他整个学期都没有做任何事情,这是怎么回事。

标签: c++ c++builder


【解决方案1】:

如果您想了解这里发生了什么,请查看这里

#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; 
} 

啊,让我想起了TAing CSCI1100

【讨论】:

  • 我担心我的努力是徒劳的。
  • &lt;sigh&gt; 定义变量时会调用构造函数。声明不会导致调用ctor。见这里:stackoverflow.com/questions/1410563/…
  • 哦,初始化列表呢?为什么那些吸气剂不是const???很抱歉,但仅凭失踪的const 就需要我提供-1
  • @sbi 我会同意你关于 const 和定义/声明的观点。目前初始化列表有点过头了。
  • @James:我教 C++ 多年,我坚信从一开始就做正确的方法。我还没有看到一个容易改掉坏习惯的学生早早地养成了。但是,由于您修复了 const 问题,我仍然删除了我的反对票。
【解决方案2】:

这是一个可行的解决方案,

源代码:

#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;
}

输出:

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

【讨论】:

  • -1 来自我,用于发布homework 问题的可粘贴答案。
【解决方案3】:

第 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
    }

};

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

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多