943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1626
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 12th, 2009
0

Intelligent Polymorphysm with OOP

Expand Post »
Hello everyone! I am trying to use polymorphism to have a little physics simulator that draws objects to the screen.

Each object has properties like mass, friction, position, velocity, acceleration and such similar things, as well as a pointer to a SHAPE object which describes the shape of the object.

The SHAPE class by itself has nothing in it, but I have several classes that inherit from shape.

I have a Circle class that stores the radius. I have a Rectangle class that stores the width and height. I'm also going to implement more classes, but i'm working with these for now.

A problem I'm encountering is drawing these classes. I understand that I could easily have the SHAPE class have a virtual method for drawing the shape, and simply call that method on the pointers later. However, I'm trying to keep my graphics and physics as separate as possible.
I do have functions for drawing circles and rectangles that work just fine when I give them the right information. For example,
drawCircle(double radius) would draw a a circle to the screen, and drawRectangle(double width, double height) would draw a rectangle. What I'm trying to accomplish is to have a drawShape(SHAPE* s) function that will call the appropriate draw function based on what type the shape really is.

Right now, I have drawShape(SHAPE* s), drawShape(Circle* c), and drawShape(Rectangle* r).
I was hoping to just call drawShape(pointer) and have the appropriate function be called automatically based on what the pointer really points to. But instead, the SHAPE function (default one that just draws a dot) is called all the time, since the pointer is of type SHAPE (even though it points to a Circle or Rectangle).

How would I make this polymorphism work for nonmenber functions without having a virtual draw() function in each shape?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Zcool31 is offline Offline
46 posts
since Mar 2009
Jun 12th, 2009
2

Re: Intelligent Polymorphysm with OOP

Read up on dynamic_cast. It does what you're asking for (keeping in mind that adding new shapes won't be as simple as creating new polymorphic classes), though I'm sure some OOP nazi will wander in and give you an overly complicated design pattern that will also solve the problem.
Last edited by Narue; Jun 12th, 2009 at 5:00 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 12th, 2009
0

Re: Intelligent Polymorphysm with OOP

dynamic_cast seems to do what I want it to, but it would only work if I already know what the pointer really points to. This is not the case. When I get a SHAPE*, I don't know if it's pointing to Circle, Rectangle, or SHAPE, and I need code that will determine which of these it is and call the appropriate function.
Reputation Points: 10
Solved Threads: 1
Light Poster
Zcool31 is offline Offline
46 posts
since Mar 2009
Jun 12th, 2009
2

Re: Intelligent Polymorphysm with OOP

>but it would only work if I already know what the pointer really points to
Riiiight. Now you're starting to get from point A to point B. What did we do before the language dynamically made choices for us? We used switch statements! And if chains! Whooo!

Seriously, dynamic_cast evaluates to a null pointer if the type you requested isn't compatible. Meaning if you try to cast a SHAPE to a Circle, but it's really a Rectangle, you'll get a null pointer. So you try again with some "evil non-OOP" construct like this:
C++ Syntax (Toggle Plain Text)
  1. if ( Circle *p = dynamic_cast<Circle> ( pshape ) ) {
  2. // Dooby dooby doo with a circle
  3. }
  4. else if ( Rectangle *p = dynamic_cast<Rectangle> ( pshape ) ) {
  5. // Dooby dooby doo with a rectangle
  6. }
  7. else {
  8. // Fail gracefully
  9. }
Last edited by Narue; Jun 12th, 2009 at 5:40 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 12th, 2009
0

Re: Intelligent Polymorphysm with OOP

This might come in handy as well:

http://www.cplusplus.com/reference/s...nfo/type_info/
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jun 12th, 2009
0

Re: Intelligent Polymorphysm with OOP

If you classes all extend SHAPE then drawShape(SHAPE* s) will work because of liskov substitution
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 12th, 2009
0

Re: Intelligent Polymorphysm with OOP

Hey, there is a difference in the number of arguments. So if you pass the required number of arguments, the corresponding function will be evaluated.
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
Jun 12th, 2009
0

Re: Intelligent Polymorphysm with OOP

Click to Expand / Collapse  Quote originally posted by s_sridhar ...
Hey, there is a difference in the number of arguments. So if you pass the required number of arguments, the corresponding function will be evaluated.
Overloading is not the same as polymorphism
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 13th, 2009
0

Re: Intelligent Polymorphysm with OOP

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
Overloading is not the same as polymorphism
What i said is just polymorphism
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
Jun 13th, 2009
0

Re: Intelligent Polymorphysm with OOP

>What i said is just polymorphism
No. What you said was overloading (function overloading to be exact). Read http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ point number 2.(What is polymorphism?).
In short, while discussing in the domain of C++, polymorphism is the term used to describe the dynamic polymorphism (virtual classes and function and related...) while overloading is the term used to describe static polymorphism.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help says isn't declard in scope don't see why
Next Thread in C++ Forum Timeline: mouse window locking





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC