我的最后一个问题在2分钟后关闭。我只是向成员函数寻求帮助。我应该有成员函数检查以下内容:
我的私人成员是:double 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; }
答案 0 :(得分:3)
好像你正在开始实施课程,所以我会在那里给你一些指导。您的所有问题都涉及检查某些坐标空间中两个正方形的相对位置。要回答这些问题,您需要了解每个方块的两个方面:
x
,y
坐标(通常是广场的一角))size
)假设你的意思是平方,那么宽度和高度是相等的,所以你只需要一个变量来存储大小。如果你实际上可以有矩形,那么你需要width
和height
。这些人必须是Square
班级的成员,因为每个Square
都应该有自己的位置和规模。您不需要存储广场每个角落的坐标,因为您可以从位置和大小处理它们 - 存储您可以从已有数据中获得的数据称为冗余,通常是您想要避免的。以下是两个不同坐标系中的情况(一个y
下降,另一个y
上升):
x-->
y size
| (x,y).___________________
v | |
| |
| |
| |
size | |
| |
| |
| |
|___________________| (x+size, y+size)
___________________ (x+size, y+size)
| |
| |
| |
| |
size | |
| |
| |
^ | |
| (x,y).___________________|
y size
x-->
作为替代方案,您可以存储两个位置,这两个位置是您方阵的两个对角。但是,如果您确实在处理正方形而不是矩形,则需要手动强制执行不变点,即两个点之间的点必须相距相同的距离。矩形没有这个不变量,所以这对它们更适用,但是翻译等操作变得更复杂(你现在必须移动两个点而不是一个点)。
您不需要perimeter
或area
个会员功能。没有一个问题要求你知道这些事情。通过对两个正方形的位置和大小进行简单比较,可以回答所有问题。
是否将它们设为私有是一个设计问题。通过将它们设为私有,您可以通过Square
的界面控制对它们的访问。然后,您可以提供公共会员功能,以便检查某些问题的答案,例如bool Square::contains(const Square& otherSquare)
,您可以将其称为square.contains(otherSquare)
。这是可能的类定义的开始:
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
}
// ...
};
或者,您可以在命名空间范围内使用函数(不是Square
的成员),并使其成为Square
的朋友,或者使Square
的位置和大小成员公开。对于这样一个简单的例子,这不会是一个问题。
我是否应该在头文件中包含它以获取坐标和大小的x和y?
double getX( ) const { return x; } double getY( ) const { return y; } double getSize( ) const { return size; }
这些功能通常称为 getters 。它们提供对类的私有成员变量的访问。在您的情况下,您不需要提供这些getter,因为您编写的函数是Square
的成员函数。 Square
的成员函数可以访问任何其他Square
的私有成员,因此您无需通过这些getter访问。
但是,如果您希望某些其他类能够访问给定x
的{{1}},y
和size
值,那么你需要提供吸气剂(或让会员公开)。
答案 1 :(得分:0)
从这样的事情开始,然后从那里开始
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) {
}
}