| | |
Intelligent Polymorphysm with OOP
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 33
Reputation:
Solved Threads: 1
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?
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; }
}; 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.
•
•
Join Date: Mar 2009
Posts: 33
Reputation:
Solved Threads: 1
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; }
}; >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:
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)
if ( Circle *p = dynamic_cast<Circle> ( pshape ) ) { // Dooby dooby doo with a circle } else if ( Rectangle *p = dynamic_cast<Rectangle> ( pshape ) ) { // Dooby dooby doo with a rectangle } else { // Fail gracefully }
Last edited by Narue; Jun 12th, 2009 at 5:40 pm.
I'm here to prove you wrong.
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.
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.
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.
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.
>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.
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
(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
![]() |
Similar Threads
- Is VB.NET an OOP? (VB.NET)
- OOP's and GUI's...oh my! (Computer Science)
- basics of OOP (C)
- Seeking Experienced Intelligent PPC Expert (Internet Marketing Job Offers)
- minor problem with vectors and OOP (C++)
- Development of an Intelligent Electronic Market place (IT Professionals' Lounge)
- oop (C)
- Intelligent Redirect (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: need help says isn't declard in scope don't see why
- Next Thread: mouse window locking
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






