1

Please see the following code:

#include<iostream.h>
#include<string.h>
class father
{
    char fname[20];
    public:
        father()
        {
            strcpy(fname,"eshwarappa");
        }
        void show()
        {
            cout<<"Father's name = "<<fname<<endl;
        }
};
class son: public father
{
    char sname[20];
    public:
        son()
        {
            strcpy(sname,"yajurappa");
        }
        void show()
        {
            cout<<"Son's name = "<<sname<<endl;
        }
};
int main()
{
    father *bp;
    father f;
    bp=&f;
    bp->show();
    son s;
    bp=&s;      //Valid assignment
    bp->show();
}

OUTPUT:

Father's name = eshwarappa
Father's name = eshwarappa

Why is it that the base class pointer bp makes a call to base class' method show() even when it contains the address of the object of derived class son.

P.S. I know this can be solved by making the function show() as virtual in the base class but I just want to know why it doesn't work this way.

2
  • Your example doesn't have any son objects anyway. May 14, 2015 at 2:53
  • It does. Look closely :) May 15, 2015 at 20:45

1 Answer 1

8

Because making a function virtual incurs a non-zero runtime cost. Part of the philosophy of C++ is "you only pay for what you use"; i.e. you don't pay the cost of a virtual function unless you've explicitly asked for it by writing virtual in your code.

As for why it incurs that runtime cost, the short answer is that a father pointer by itself does not say whether it's actually pointing to a father or a son or some other derived class. The only way to know what type it's actually pointing to at runtime is for every father object to carry around an extra piece of data that identifies what type it is. That means objects with virtual methods take up (slightly) more memory.

In most implementations that extra piece of data is a single pointer to a "vtable". If you gave father a virtual method, then every father object that gets created at runtime will have a pointer to the father vtable, and every son object that gets created at runtime will have a pointer to the son vtable. Assuming show() is the only virtual method, both vtables will contain a single function pointer to their respective implementations of show(). That means dereferencing a few pointers every single time you call show(), which means virtual methods can be (slightly) slower than non-virtual methods.

You might argue that one extra pointer on every object and a little more pointer chasing is not a significant cost. That depends on the class and the application. If you're doing scientific computations with 1000x1000 matrices, you might not want an extra 4 bytes on every single matrix/vector/etc object and an extra couple dereferences on every add/multiply/divide operation. C++ lets you decide exactly when that cost does and doesn't matter.

On the other hand, some other OOP languages (typically ones with garbage collection) have chosen to make everything "virtual by default." It's a trade-off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.