Simple Programming Help Req

This is a discussion on Simple Programming Help Req within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell, how can i write that program in C++, its little bit Object oriented programming related... but i ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Exclamation 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.


    Thanks in Advance..
    Regards

    Umar

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Heh. Show some work!

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    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.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    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

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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 {
      double radius;
      public: virtual double get_area() { return radius * radius * M_PI; }
    };
    This is simple and works well.

    Intersection, however, is a problem where the correct action depends on two actual types, not one. (It's in fact the typical example of this problem.) You have two shape references, and must decide on an action depending on both their types. Intersection for square and square works differently from square and circle works differently from circle and circle.

    A function that can choose the correct implementation depending on more than one parameter is called a multimethod. C++ has no built-in support for this.

    There are many different approaches to this problem, none completely satisfactory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21