Quick Polymorphism Tutorial

Reply

Join Date: Apr 2004
Posts: 45
Reputation: BlackDice is an unknown quantity at this point 
Solved Threads: 0
BlackDice's Avatar
BlackDice BlackDice is offline Offline
Light Poster

Quick Polymorphism Tutorial

 
0
  #1
Apr 28th, 2004
Polymorphism is what makes using inheritance so powerful ( in my opinion). Here's a quick example of it using objects. This example uses 3 classes: CCar, and CSportsCar and CLuxuryCar which are derived from CCar.

  1. class CCar
  2. {
  3. //constructors and other stuff...
  4. virtual CString GetType()
  5. { return "Car";)
  6. } ;
  7.  
  8. class CSportsCar : public CCar
  9. {
  10. //constructors and other stuff...
  11. CString GetType()
  12. { return "SportsCar";)
  13. };
  14.  
  15. class CSportsCar : public CCar
  16. {
  17. //constructors and other stuff...
  18. CString GetType()
  19. { return "LuxuryCar";)
  20. };


these classes and their GetType() functions are actually pretty useless, but hopefully it will help to illustrate my point. Say you have a dialog class with a pointer to a CCar object declared in it (CCar* m_pCar).

now if your application has a user make a selection, based on that selection you can set the actual type of your object. For instance, your user selected the Luxury Car option:

m_pCar = new CLuxuryCar();

now if you call anywhere throughout your code, the function GetType() while this object is still valid, it will return "LuxuryCar". You can now change the type like so:

m_pCar = new CSportsCar();

You can now call m_pCar->GetType(), and it will return "SportsCar".

When you override a virtual function (like we did with GetType()) there is a vtable created with pointers mapped to which function belongs to the object depending on the type of object it is. Or if the virtual function is good enough, you can still call it without it being overridden in your derived class to use the base class's function. While the preceding function was useless, something like that can still be helpful in determing Run-Time Type Information so that depending on what type of object your pointer actually is at that time, you can determine if something special needs to be done. If anyone likes this post, or doesn't -- I'd appreciate your comments. I'm doing this at the end of my work day, so I can also make it clearer if needed and provide more of an example.:cool:
Last edited by cscgal; Apr 14th, 2006 at 5:27 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 53
Reputation: Natso is an unknown quantity at this point 
Solved Threads: 1
Natso Natso is offline Offline
Junior Poster in Training

Re: Quick Polymorphism Tutorial

 
0
  #2
May 24th, 2004
In batch files you can just have one certain test, eg %[part1]% that is in every line of the first part of the file, then %[part2]% and so on... then when the batch starts have it randomly run part2 or part3 or part4... each part will use the 'find' feature & reconstruct the batch untill the whole batch is remade in a different way. If anybody wants a full code I have one somewhere on my computer & will post it here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Quick Polymorphism Tutorial

 
0
  #3
Jun 6th, 2004
It would be great if you could post code in our code snippets forum. That's much appreciated.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC