简体   繁体   中英

constructor.function(constructor) How do you pass an object of the same class to a function?

So, I just got out of an exam and the final question had a question that required you to find the find whether two circles overlapped given their centerpoints (x,y) and radii. You were then supposed to call the function using the constructors for the circles and you functions like

c1.overlaps(c2); .

I'm not sure how you would pass the values for both c1 and c2 into the function. Looking at some other code just now it seems like my function declaration should have started with

void overlaps(Circle &a)

The argument Circle &a would reference Circle c2 from above but I don't know how Circle c1 and its valued would be passed in.

Here's the code I wrote:

#include <string>
#include <cmath>

using namespace std;

class Circle
{
    private:
        double x, y, radius;
    public:
        Circle(double &x, double &y, double &radius);
        {
            void overlaps(this);
        }
};

void overlaps(this)
{
    this -> c2;
    double x3 = c2.x;
    double y3 = c2.y;
    double rad3 = c2.radius;
    double z = pow(x3-x,2) + pow(y3-y,2);
    double r = sqrt(z);
    if(c2.radius + radius > r)
        cout<<"The circles overlap.\n";
    else
        cout<<"The circles do not overlap\n";
};

int main()
{
    double x1, x2, y1, y2, rad1, rad2;
    cin>>x1>>y1>>rad1;
    cin>>x2>>y2>>rad2;
    Circle c1 = new Circle(x1, y1, rad1);
    Circle c2 = new Circle(x2, y2, rad2);
    c1.overlaps(c2);
}

I hope you didn't write the above code in your exam :)

In essence, function void overlaps(Circle &a) should be a member function in class Circle .

As you mentioned in your question, when calling c1.overlaps(c2) , the explicit argument a would be used as a reference to variable c2 .

And as you didn't mention in your question, when calling c1.overlaps(c2) , the implicit argument *this would be used as a reference to variable c1 (though you wouldn't have to explicitly use it).

So you should basically define class Circle as follows:

class Circle
{
private:
    double x, y, radius;
public:
    Circle(double x, double y, double radius);
    bool overlaps(const Circle& a) const;
};

Then, you should implement function overlaps as follows:

bool Circle::overlaps(const Circle& a) const
{
    double z = pow(a.x-x,2) + pow(a.y-y,2);
    double r = sqrt(z);
    return a.radius + radius > r;
};

Finally, in function main , you can call function overlaps as follows:

int main()
{
    double x1, x2, y1, y2, rad1, rad2;
    cin>>x1>>y1>>rad1;
    cin>>x2>>y2>>rad2;
    Circle c1 = new Circle(x1, y1, rad1);
    Circle c2 = new Circle(x2, y2, rad2);
    if (c1.overlaps(c2))
        cout<<"The circles overlap.\n";
    else
        cout<<"The circles do not overlap\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM