4

我的最后一个问题在 2 分钟后关闭。我只是在成员函数方面寻求帮助。我应该让成员函数检查以下内容:

  1. 广场是否在另一个广场之外;
  2. 该正方形是否包含另一个正方形;
  3. 该方格是否包含在另一个方格中;
  4. 正方形是否在外部与另一个正方形相切(即,它们的边界是否接触,但除了那些边界点之外,它们彼此外部);
  5. 正方形是否在内部与另一个正方形相切(也就是说,它们在边界上有共同的点,但除了那些边界点,一个正方形包含在另一个正方形中);
  6. 正方形的边界是否与另一个正方形的边界相交。

我的私人成员是:双x,y;

那么我是否应该使用公共成员函数,同时使用 x 和 y 来计算周长和面积?

这就是我到目前为止所拥有的:头文件

#include  <iostream>
#include  <cmath>
class Square
{
int x, y;
int  size;

public:
Square(int x, int y, int size) : x(x), y(y), size(size) { }
~Square() {};
bool isExternal(const Square& rhs);
bool contains(const Square& otherSquare);
bool iscontained(const Square& otherSquare);

bool borderintersect (const Square& otherSquare);
bool bordertangent (const Square& otherSquare);
}

实施

#include "Square.h"


bool Square::isExternal(const Square& rhs) const {
    return (((x < rhs.x) || ((x + size) > (rhs.x + rhs.size)) &&  ((y < rhs.y) || ((y + size) > (rhs.y + rhs.size)) 
};
bool Square::contains(const Square& otherSquare)const {
};
bool Square::iscontained(const Square& otherSquare)const {
};

bool borderintersect(const Square& othersquare)
{
// If this square's bottom is greater than the other square's top
if ((this->y - (this->size / 2)) > (othersquare->y + (othersquare->size / 2)))
{
    return (false);
}
// the reverse
if ((this->y + (this->size / 2)) < (othersquare->y - (othersquare->size / 2)))
{
    return (false);
}
// If this square's left is greater than the other square's right
if ((this->x - (this->size / 2)) > (othersquare->x + (othersquare->size / 2)))
{
    return (false);
}

if ((this->x + (this->size / 2)) < (othersquare->x - (othersquare->size / 2)))
{
    return (false);
}
return (true);

bool Square::bordertangent (const Square& otherSquare)const {
};

测试程序

#include "Square.h"
#include <iostream>
using namespace std;

  int main() {

Square sq1(0, 0, 10);
Square sq2(1, 1, 10);

    if(sq1.isExternal(sq2)) {
    cout<<"Square 1 is external to square 2"<<endl;
}

if(sq1.contains(sq2){
    cout<<"Square 1 contains square 2"<<endl;

return 0;
}

我是否应该将其包含在头文件中以获得坐标的 x 和 y 以及大小?

double  getX( )  const {  return x;  }
double  getY( )  const {  return y;  }
double  getSize( )  const {  return size;  }               
4

2 回答 2

3

It seems like you're tripping up on where to start with implementing the class so I'll give you some guidance there. All of your questions deal with checking the relative positions of two squares in some coordinate space. To answer them, you will need to know two things about each square:

  • Position (x, y coordinates (often a corner of the square))
  • Size (size)

Assuming you definitely mean square, then the width and height are equal, so you only need one variable to store the size. If you can actually have rectangles, then you'll need width and height. These will need to be members of your Square class because each Square should have its own position and size. You do not need to store the coordinates of each corner of your square because you can work them out from the position and size - storing data that you can work out from data you already have is known as redundancy and is generally something you want to avoid. Here is how that would look in two different coordinate systems (one where y goes down and the other where y goes up):

 x-->
y                size
|   (x,y).___________________
v        |                   |
         |                   |
         |                   |
         |                   |
    size |                   |
         |                   |
         |                   |
         |                   |
         |___________________| (x+size, y+size)

          ___________________ (x+size, y+size)
         |                   |
         |                   |
         |                   |
         |                   |
    size |                   |
         |                   |
         |                   |
^        |                   |
|   (x,y).___________________|
y                size
  x-->

As an alternative, you could store two positions which are two opposite corners of your square. However, if you really are dealing with squares, not rectangles, you will need to manually enforce the invariant that the points must be the same distance away from each other along both axes. Rectangles would not have this invariant so this would be more applicable for them, but then operations such as translation become more complicated (you would now have to move two points rather than just one).

You will not need perimeter or area member functions. None of the questions require you to know either of these things. All of the questions can be answered by doing simple comparison between the positions and sizes of the two squares.

Whether you have them be private or not is a design issue. By making them private, you can control access to them through the Square's interface. You could then provide public member functions so that you can check for answers to some of your questions, such as bool Square::contains(const Square& otherSquare) which you would call like square.contains(otherSquare). Here's the beginnings of a possible class definition:

class Square
{
 private:
  int x, y, size;
 public:
  Square(int x, int y, int size) : x(x), y(y), size(size) { }
  bool contains(const Square& other)
  {
    // Do comparisons between x, y, size and other.x, other.y, other.size
  }
  // ...
};

Alternatively, you could have the functions at namespace scope (not members of Square) and either make them friends of Square or make the position and size members of Square public. For such a simple example, this will not be a problem.


Should I include this in the header file in order to get both the x and y of the coordinate and the size?

double  getX( )  const {  return x;  }
double  getY( )  const {  return y;  }
double  getSize( )  const {  return size;  }   

These kinds of functions are commonly known as getters. They provide access to private member variables of a class. In your case, you do not need to provide these getters because the functions you are writing are member functions of Square. The member functions of Square have access to the private members of any other Square, so you don't need to get access through these getters.

However, if you wanted some other class to access the x, y, and size values for a given Square, then you would need to provide getters (or make the members public).

于 2012-12-04T14:46:57.820 回答
0

Start with something like this, and go from there

class Square
{
    double m_x, m_y;
    double m_length;

    public:
    Square(double x, double y, double length) : m_x(x), m_y(y), m_length(length) {}
    ~Square() {};

    bool isExternal(const Square& rhs);
}

--- implementation
bool Square::isExternal(const Square& rhs) 
{
    bool retval = true;
    // do some calculations ..
    return retval;
}


--- main

int main(void)
{
    Square sq1(0, 0, 10);
    Square sq1(1, 1, 10);

    if(sq1.isExternal(sq2) {
    }
}
于 2012-12-04T14:46:51.533 回答