A good example to begin with.

class A
{
    public:
    virtual const char* GetName() { return "A"; }
};

class B: public A
{
public:
    virtual const char* GetName() { return "B"; }
};

class C: public B
{
public:
    virtual const char* GetName() { return "C"; }
};

    class D: public C
{
public:
    virtual const char* GetName() { return "D"; }
};

int main()
{
    C cClass;
    A &rBase = cClass;
    cout << "rBase is a " << rBase.GetName() << endl;

    return 0;
}

During this example the output is :

rBase is really a C

Listed here are the steps of how it operates:

rBase is really a pointer of type A, therefore it would go to class A and searches for GetName(). But GetName() is virtual there so compiler inspections all classes from a and C and takes GetNAme() function in the most derived class ie C

But, my doubt is the fact that the way the compiler know the child of sophistication A and just how does it have the ability to proceed to class B and so forth from Parent class to child class ? A young child know it's parent however the parent does not know it's children (I believe!).

From the perspective the right steps of execution must have been:

rBase is really a pointer of type A, therefore it would go to class A and searches for GetName(). But GetName() is virtual there therefore the compiler inspections what class the pointer is pointing to. C class object within this situation, so would go to C class and inspections whether it has function GetName() so uses it. When the function wasn't contained in C class (assume) the compiler can certainly trace C's parent and appearance for the similar which may go on until it reaches to A (presuming all classes except A does not contain GetName()).

Now, this appears a far more logical method because moving backward (child to parent) within an inheritance tree appears more achievable than continuing to move forward (parent to child).

Regards,

The second formula you describe is "correct" for the reason that it might effectively resolve things by doing this. However, the compiler utilizes a nice trick to fast-forward that formula. Essentially, it utilizes a research generally known as a virtual method table, frequently abbreviated to "vtable".

Essentially, cases of classes with virtual techniques hold a pointer for their class' virtual method table. The compiler maps virtual method names to offsets within the virtual table, so that invoking an online table doesn't need an intricate formula: all you have to is definitely an array research then a phone call in the resulting address.

rBase is really a pointer of type A, therefore it would go to class A and searches for GetName(). But GetName() is virtual there so compiler inspections all classes from a and C and takes GetNAme() function in the most derived class ie C

No, this isn't how it operates. The compiler when producing the code to call virtual GetName() through pointer to A just puts something similar to "call the seventh entry within the virtual table connected using the object". When producing a derived class that overrides GetName(), the compiler will put its GetName() implementation only at that entry within the virtual table.

By doing this parents class need not know its children. It is the children responsibility to fill their virtual table properly.