Can some one please help what the order of destruction is when I am using virtual functions. Does it start with the base class and then derived class?
Since I don't see how virtual function change any objects' destruction order, I assume you're referring to the order of destruction for base classes and data members in a virtual inheritance scenario.
Sub-objects are constructed
Destruction is simply the opposite of construction, so you only need to memorize the above.
However, the above four rules are in that order because that makes sense, and if you understand why this order makes sense, you will not even have to memorize those four rules, but can infer them from your understanding (as I just did). So let's examine that order:
First the derived, then the base. No difference wrt the non-virtual cases.
Additional note. When you have inheritance and virtual methods, you have to declare destructors as virtual, otherwise you can have undefined behavior at deletion.
Example, suppose Derived is derived from Base, and you allocate Derived with the following line:
Base *o = new Derived();
delete(o);
If this case occurs in your code, and Base has no virtual destructor, the resulting behavior is undefined. Typically, only the destructor of Base will be called. The destructor of Derived will not be called, because you are calling delete on a Base pointer. However, the program might crash instead. Once you are in the realm of undefined behavior, all bets are off and your running code is doomed. To prevent chaos the Base destructor must be virtual.