Intelligent Polymorphysm with OOP

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 33
Reputation: Zcool31 is an unknown quantity at this point 
Solved Threads: 1
Zcool31 Zcool31 is offline Offline
Light Poster

Intelligent Polymorphysm with OOP

 
0
  #1
Jun 12th, 2009
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?
class NoClass {
	~NoClass () { delete this; }
};
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,687
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Intelligent Polymorphysm with OOP

 
2
  #2
Jun 12th, 2009
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 33
Reputation: Zcool31 is an unknown quantity at this point 
Solved Threads: 1
Zcool31 Zcool31 is offline Offline
Light Poster

Re: Intelligent Polymorphysm with OOP

 
0
  #3
Jun 12th, 2009
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.
class NoClass {
	~NoClass () { delete this; }
};
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,687
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Intelligent Polymorphysm with OOP

 
2
  #4
Jun 12th, 2009
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 375
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Intelligent Polymorphysm with OOP

 
0
  #5
Jun 12th, 2009
This might come in handy as well:

http://www.cplusplus.com/reference/s...nfo/type_info/
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Intelligent Polymorphysm with OOP

 
0
  #6
Jun 12th, 2009
If you classes all extend SHAPE then drawShape(SHAPE* s) will work because of liskov substitution
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Intelligent Polymorphysm with OOP

 
0
  #7
Jun 12th, 2009
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.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Intelligent Polymorphysm with OOP

 
0
  #8
Jun 12th, 2009
Originally Posted by s_sridhar View Post
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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Intelligent Polymorphysm with OOP

 
0
  #9
Jun 13th, 2009
Originally Posted by ShawnCplus View Post
Overloading is not the same as polymorphism
What i said is just polymorphism
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Intelligent Polymorphysm with OOP

 
0
  #10
Jun 13th, 2009
>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.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC