943,923 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8840
  • C RSS
Apr 28th, 2004
0

Quick Polymorphism Tutorial

Expand Post »
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.
Similar Threads
Reputation Points: 19
Solved Threads: 0
Light Poster
BlackDice is offline Offline
43 posts
since Apr 2004
May 24th, 2004
0

Re: Quick Polymorphism Tutorial

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Natso is offline Offline
51 posts
since May 2004
Jun 6th, 2004
0

Re: Quick Polymorphism Tutorial

It would be great if you could post code in our code snippets forum. That's much appreciated.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002

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: how can i make the program depend on time
Next Thread in C Forum Timeline: Debugging Link errors





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


Follow us on Twitter


© 2011 DaniWeb® LLC