简体   繁体   English

C ++非成员函数,使用引用另一个类的类

[英]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.

相关问题 从类内部调用非成员函数,但非成员函数将类作为输入(C++) - Call non-member function from inside class, but the nonmember function takes class as input (C++) 在C ++中如何继承父类的非成员函数,这只是在文件中定义的? - In C++ how to inherit a parent class' non-member functions, which is simply defined in the file? 为什么我不能在类之外重载C ++转换运算符,作为非成员函数? - Why can't I overload C++ conversion operators outside a class, as a non-member function? c ++ --direct—访问非成员函数体中的类成员 - c++ --direct— access of class members in non-member function bodies 模板类的C ++静态非成员函数返回对象 - C++ static non-member function returning object of a template class 在C ++中使用非成员方法 - Using non-member methods in C++ 我们将非成员函数放在C ++的哪个文件中? - In which file do we put non-member function in C++? 如何在C ++中为模板类包含非成员运算符-重载? - How do in include a non-member operator- overloading for a template class in c++? 我可以在C ++中声明一个非成员函数const吗? - Can I declare a non-member function const in C++? C ++:非成员函数的定义/声明的位置 - C++: location of definition/declaration of non-member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM