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.

class CCar
{
//constructors and other stuff...
virtual CString GetType()
{ return "Car";)
} ;
 
class CSportsCar : public CCar
{
//constructors and other stuff...
CString GetType()
{ return "SportsCar";)
};
 
class CSportsCar : public CCar
{
//constructors and other stuff...
CString GetType()
{ return "LuxuryCar";)
};

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:

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.

It would be great if you could post code in our code snippets forum. That's much appreciated.

This article has been dead for over six months. Start a new discussion instead.