我有这个硬件问题,我已经被困了几个小时,我似乎无法理解。用实际的问题说出来很难,但我会尽我所能。如何让我的外部成员函数使用引用另一个类的类来查找圆之间的距离?在我的代码中:
//Header
class Point
{
public:
Point();
Point(int, int);
Point(const Point& object);
int getX() const;
int getY() const;
void setX(int);
void setY(int);
double calculate_area();
void print() const;
private:
int x;
int y;
};
class Shape
{
public:
Shape();
double calculate_area();
void print() const;
protected:
double area;
};
class Circle : public Shape
{
public:
Circle();
Circle(const Point&, double);
double getRadius() const;
void setRadius(double);
void calculate_area();
void print() const;
private:
double radius;
Point center;
};
class RTriangle : public Shape
{
public:
RTriangle();
RTriangle(double, double);
double hyp();
void calculate_area();
void print() const;
private:
double side1;
double side2;
};
inline double distance(Circle& other)
{
return sqrt(pow(other.center.x, 2) + pow(other.center.y, 2));
}
#endif
//Implementation.cpp of what i think is important for you guys to see
Point::Point(int inX, int inY) // normal constructor
{
x = inX;
y = inY;
}
Point::Point(const Point &object) // copy constructor needed to use for center of circle
{
x = object.x;
y = object.y;
}
Circle::Circle(const Point& object, double inRad) // center is x & y... radius for circle
{
center = object;
radius = inRad;
}
我的外部成员函数(内联双倍距离)不起作用。这是我第一次尝试这样做,所以如果我不说得好,我会道歉。我本质上只是尝试使用继承点中心的Circle类,并且在第一个类中将点中心定义为x和y。是否可以做这样的事情(特别是使用点中心内的x和y)?我不知道是否可以通过中心访问x和y。
给定一个Point,不在Point中的函数可以使用示例中的public getX()和getY()方法访问其x和y成员。
但是,Circle的中心成员不公开,没有公共访问者。最简单的修复方法是添加一个公共方法
Point getCenter(); //或点const&getCenter()如果你喜欢)
圆圈。