• Create Account

We're offering banner ads on our site from just \$5!

# C++, Passing objects to implementation cpp file

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Novark  Members   -  Reputation: 133

Like
0Likes
Like

Posted 20 January 2010 - 07:54 AM

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!

### #2Madhed  Crossbones+   -  Reputation: 3083

Like
0Likes
Like

Posted 20 January 2010 - 08:08 AM

this->getX()

and

c.getX()

?

### #3ChaosEngine  Crossbones+   -  Reputation: 2473

Like
0Likes
Like

Posted 20 January 2010 - 08:14 AM

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

### #4theOcelot  Members   -  Reputation: 498

Like
0Likes
Like

Posted 20 January 2010 - 08:16 AM

void Circle::intersects(Circle c) { //don't forget return types    return getX() < c.getX();}

edit: drat, ninja'd twice.

### #5Novark  Members   -  Reputation: 133

Like
0Likes
Like

Posted 20 January 2010 - 08:20 AM

Thank you to the above posters, that did the trick :-)

### #6XTAL256  Members   -  Reputation: 103

Like
0Likes
Like

Posted 20 January 2010 - 03:03 PM

Quote:
 Original post by theOcelotvoid 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]

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS