[英]c++ non-member function using a class which references another class
I have this HW question that I have been stuck for hours on that I can't seem to make sense of. 我有一个关于硬件的问题,我已经困扰了好几个小时,似乎无法理解。 It's kind of hard to put to words my actual question but I will try my best. 很难说出我的实际问题,但我会尽力而为。 How can I make my outside-member function find the distance between a circle using a class which references another class? 如何使用引用另一个类的类使外部成员函数找到圆之间的距离? In my code below: 在下面的代码中:
//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;
}
My outside member function (inline double distance) isn't working. 我的外部成员函数(内联双距)不起作用。 This is the first time I have tried doing this so I apologize if I do not word it well. 这是我第一次尝试执行此操作,因此如果措辞不好,我深表歉意。 I am essentially trying to use only the Circle class which inherits point center and point center is defined in the first class as x and y. 我本质上是在尝试仅使用继承点中心的Circle类,并且在第一类中将点中心定义为x和y。 Is it possible to do such a thing (specifically use x and y which is inside Point center)? 有可能做这样的事情(特别是使用Point中心内的x和y)吗? I don't know if it's possible to access x and y through center. 我不知道是否可以通过center访问x和y。
Given a Point, functions not in Point can access its x and y members with the public getX() and getY() methods in your example. 在给定Point的情况下,不在Point中的函数可以使用示例中的公共getX()和getY()方法访问其x和y成员。
However, Circle's center member is not public and has no public accessors. 但是,Circle的中心成员不是公共的,也没有公共访问者。 The most straightforward fix would add a public method 最直接的解决方法是添加一个公共方法
Point getCenter(); 点getCenter(); // or Point const &getCenter() if you like) //或指向const&getCenter()(如果您愿意)
to Circle. 圈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.