![]() |
Polymorphism
I am part way through a course, and we have just finished overloading a printstats game example. It then says this is not an ideal method, and polymorphism is better. Thing is, even though it talks about polymorphism, it does not give you a working example for the console output game we created. I have read almost every article on google, and still cannot get my head around polymorphism. Could someone please amend the code below, so the stats are printed out using polymorphism. The Vector3.h, and Vector3.cpp, are old reusable codes. I need it to also print out the number of missles the fighter ship has. There is alot more classes involved, but I have reduced the code for brevity. I just think that if I can see polymorphism working on this, I might get a better understanding of it. This is not a project, this is something I am working on.
Code:
// Vector3.hCode:
/ Vector3.cppCode:
// Spaceship.hCode:
// Spaceship.cppCode:
// Main.cpp |
Re: Polymorphism
You need a few more things before polymorphism can work here.
- HumanFighterShip and HumanShip need an implementation of printStats() - an array containing a number of HumanFighterShips and HumanShips. This array should be defined as an array of SpaceShips, e.g. SpaceShip *arr[10]; Assigning one of those two to an array entry works fine, even though the types are technically different. For example: Code:
arr[0]=new HumanFighterShip();Then polymorphism can be demonstrated easily with: Code:
for (int i=0; i<10; i++) |
Re: Polymorphism
Hi Thanks for the reply. I only have 1 instance of human fighter ship at present, but I understand what you are saying about the array. I have re-wittne the code as follows, and any comments would be appreciated. The code runs fine and prints to console all the stats including the additional humanfightership missile stat:
Code:
Code:
Code:
// Spaceship.h |
Re: Polymorphism
Yes, you are now using it. It's "more satisfying" though to do what I suggested and manage a bunch of Human*Ships using a collection of pointers to SpaceShips, but it's not necessary.
|
Re: Polymorphism
I have got the code to work using polymorphism, so I thought I would give an array of human fighter ships a go. The code complies, prints then says test.exe has stopped working, and I cannot figure out why. Can you see anything from the code below?
Code:
// Spaceship.hCode:
Code:
|
Re: Polymorphism
Sorted the problem
Code:
for(int i = 0; i < 5; ++i) |
Re: Polymorphism
Interesting. Did you get any warnings from the compiler, such as "condition is always true"?
If not, try switching warnings to the maximum level, recompile the program with the error still in it, and see what it tells you. Warnings can be a good place to start looking for the reason for unexplained crashes. I tried to spot the error in the code but I missed it. 1 looks too much like i. There should be a lesson for you here regarding choice of variable names. Well done for spotting the error yourself though. Debugging is probably 90% of programming - certainly over half - and too many people post here going "it crashes, find it for me, waaah" without realising they're throwing away half the "fun" of programming. |
Re: Polymorphism
Quote:
Have another question using arrays. I have objects in HumanFighterShip, which are not objects of Spaceship, which are fire laser and fire missiles. The code (snippet) I have at present is as follows: Code:
int main()Code:
//Spaceship.cppCode:
|
Re: Polymorphism
Polymorphism works when you can do everything you want to every object through the interface defined in the base class. Once you start trying to use functionality not defined at the base level, you have to leave it behind, decide what kind of object it is then start doing class-specific stuff, which by definition is not polymorphic.
So, to attempt to answer your question: (1) it is wrong to use a pointer to the base class if you are trying to do stuff not defined in the base class; (2) it is right to use a pointer to the base class if you are using polymorphism. So the next question you need to address is this: can *all* SpaceShips fire lasers and missiles? (What about cargo ships?) If they can, then you can safely drop this stuff into SpaceShip and continue to use polymorphism. If they can't, but you could define a new subclass called ShipWithAttackyStuff from which all SpaceShips that can fire lasers and missiles can be derived, then you could use polymorphism at the level of ShipWithAttackyStuff (which might mean that you need to define something like isShipWithAttackyStuff() in the base class, so that you could then use pseudo-polymorphism when iterating over SpaceShips: call isShipWithAttackyStuff() then depending on the result call SpaceShip::Surrender() or cast the pointer to ShipWithAttackyStuff and polymorph from there.) It's a lot of fun, this polymorphism. Especially when you get into multiple inheritance. |
| All times are GMT +5.5. The time now is 12:14. |