C++, Passing objects to implementation cpp file

Started by
4 comments, last by XTAL256 14 years, 3 months ago
I'm doing something fairly simple - checking the intersection of two circles. In main():

Circle c1 = Circle(0,0,5) //x, y, radius
Circle c2 = Circle(4,0,2)

if (c1.intersects(c2)) {
   cout << "c1 intersects c2";
}
In the circle implementation file:

Circle::Circle() {
	cX = 0;
	cY = 0;
	cR = 10;
}

void Circle::setX(int x) {
	cX = x;
}

void Circle::setY(int y) {
	cY = y;
}

int Circle::getX() const {
	return cX;
}

int Circle::getY() const {
	return cY;
}

Circle::intersects(Circle c) {
   //I need to have access to both c1's X attribute, AND c2's X attribute in order to calculate whether or not they intersect.

   //As a simpler example, let's say that I wanted to compare c1.getX() and c2.getX() and see which one was larger.  How would I go about this?
}
I obviously didn't include the whole implementation file, but this should hopefully be enough to explain my question. I would appreciate any help with this! If I didn't give enough information, feel free to ask. Thanks!
Advertisement
What about

this->getX()

and

c.getX()

?
I don't really see what the problem is.

bool Circle::is_bigger(Circle c) {   return cR > c.cR;}


when you have a non-static member fuction, you have an implicit parameter called "this" which is a pointer to the current object you're working on.

so let's say you have
Circle c1 = Circle(0,0,5) //x, y, radiusCircle c2 = Circle(4,0,2)c1.is_bigger(c2);


in the is_bigger method, the this pointer is pointing to c1. In fact you can explicitly state that you mean that.

bool Circle::is_bigger(Circle c) {   return this->cR > c.cR;}


HTH
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
void Circle::intersects(Circle c) { //don't forget return types    return getX() < c.getX();}


edit: drat, ninja'd twice.
Thank you to the above posters, that did the trick :-)
Quote:Original post by theOcelot
void Circle::intersects(Circle c) { //don't forget return types    return getX() < c.getX();}

You mean:
bool Circle::intersects(Circle c) {    return getX() < c.getX();}

[grin]
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement