We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › function of a class
Page Index Toggle Pages: 1
function of a class (Read 323 times)
function of a class
Sep 3rd, 2006, 12:46am
 
Hello...

I have created a class called Element and inside the Element class I have created a function that controls the relation between two instances of this class ..for example:

void overlap (Element a, Element b)
{
if ( a.topY <= b.topY && b.topY <= a.bottomY )
{
  dy = a.bottomY - b.topY;
}
if ( a.leftX <= b.rightX && b.rightX <= a.rightX )
{
  dx = b.rightX - a.leftX;
 }

 float overlapArea = dx * dy;
}

1.How is it possible to call this function inside the main code ??? (since it refers to two instances,which one will be the one to define it and how will I avoid to call it twice?)

2.How can I get the actual value of the float overlapArea for two instances of the class? The aim is to get the overlapping area of two rectangles and then do the same thing for other pairs of rectangles and have the add them up in order to get the sum..

3.Shouldn't this function be within the class? Can I do it differently ..or in a better way?

Re: function of a class
Reply #1 - Sep 3rd, 2006, 4:37pm
 
It makes sense to have this function in a class.  Doing it this way, you'd want the main program to look something like:

Element e1 = new Element();
Element e2 = new Element();

float something = e1.overlap(e2);

Your function then takes only one argument (an Element object).  It should also have a return type of "float" so that you can return the value back to the calling area:

float overlap(Element other) {
  // code
  return overlapArea;
}
Page Index Toggle Pages: 1