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:

Dani AI

Generated

Nice, concise primer from — useful for showing the runtime-dispatch idea. A few practical caveats and modern C++ tweaks make that pattern safe and maintainable in real projects:

  • Always give a polymorphic base a virtual destructor. Deleting a derived object through a base pointer without one is undefined behavior.
  • Avoid manual new/delete ownership in UI/dialog members; prefer std::unique_ptr/std::shared_ptr so reassigning a pointer won’t leak.
  • Prefer behavior (virtual functions) over string-based type checks for RTTI. If you must check concrete type, use dynamic_cast (classes must be polymorphic) or typeid rather than a hand-rolled GetType() string.
  • Watch object slicing: store or pass around pointers/references to preserve derived behavior — assigning a derived object to a base object by value slices off the derived part.
  • Use override (and final when appropriate) so the compiler catches signature mistakes.

Example patterns (modern, safe):

#include <memory>
#include <iostream>
#include <string>

struct Car {
    virtual ~Car() = default;                 // important
    virtual std::string getType() const { return "Car"; }
};

struct SportsCar : Car {
    std::string getType() const override { return "SportsCar"; }
};

int main() {
    std::unique_ptr<Car> car = std::make_unique<SportsCar>();
    std::cout << car->getType() << '\n';
}

If type-specific logic is needed:

if (auto s = dynamic_cast<SportsCar*>(car.get())) {
    // sports-car specific handling
}

Finally, the example in the first post has a couple of small syntax/duplication issues; posting corrected snippets in the code snippets forum as suggested will make it easier for others to copy and run.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.