# Simple Programming Help Req

• 01-27-2008
umar7001
Simple Programming Help Req
Can anyone tell, how can i write that program in C++, its little bit Object oriented programming related... but i really need to develop logic in C++

Here is the statement:

Write three classes names: circle, square and triangle, derived from class shape. Write down a function named intersect in an appropriate class which will take two of the shapes as parameters and tell whether they overlap or not.

here is the way, i have started:

Code:

1.        class shape{
2.        protected:
3.
4.        public:
5.
6.        };
7.
8.        class triangle : public shape {
9.        private:
10.
11.        public:
12.
13.        };
14.
15.        class square: public shape {
16.        private:
17.
18.        public:
19.
20.        };
21.
22.        class circle: public shape {
23.        private:
24.
25.        public:
26.
27.        };
28.
29.        int main() {

Now i really need to know how can i develop the logic that take two of the shapes as parameters and tell whether they overlap or not.

Regards

Umar
• 01-27-2008
twomers
Heh. Show some work!
• 01-27-2008
avalanche333
What is ment by overlap? I am guessing thats a triangle fits into square or circle, or square fits into a circle?

You can have a protected variable say m_value in shape and have constructors for each class. Constructor will set the value to say 1 for triangle, 2 for square, 3 for circle. You will also need a getter in shape to be able to access the m_value.

You would also have an public bool intersect(Shape s1, Shape s2) in Shape class.

This function would look like somthing like this:

Code:

bool intersect(Shape s1, Shape s2) {
return s1.getValue() > s2.getValue;
}

Thats it, again this is assuming I understand the meaning of overlap.
• 01-27-2008
Elysia
Overlap means that a shape is within the bounds of another. So a circle's edge that that actually goes into another circle's inner, so to speak, for example.
Or mathematically:
Circle c1;
Circle c2;
if ( (c1.pos - c1.radius) <= (c2.pos + c2.radius) ) bOverlap = true;
So in other words, if the position of circle one minus its radius (so effectively we'd get its left edge) is lesser than or equal to the position of circle 2 plus its radius (essentially, it's right edge), then we have overlap.
• 01-27-2008
avalanche333
ah gotcha.

Well you can still take the approach I suggested but instead of having a m_value, you would use the coordinates or location that you required and change the algorithym used to find if they are intersecting. All of this should still be done in the shape class so all subclasses will have access to this function.
• 01-27-2008
umar7001
really thanks for the quick help guys, so far i have tried to develop little bit logic as Elysia said, but how do the program knows that square, triangle are overlaping?

Regards

Umar
• 01-27-2008
CornedBee
Very tricky, very tricky. The problem you're facing is called multimethods. Here's the gist of it:

Given a shape reference, you can use a virtual function to perform a function based on the actual type of the object. E.g. you can calculate the area:
Code:

class shape {
public: virtual double get_area() const = 0;
};
class square : public shape {
double side;
public: virtual double get_area() { return side * side; }
};
class circle : public shape {