Search This Blog

Tuesday, July 13, 2010

Constructor Destructor and Virtual Keyword (C++)

One of the favorite interview question is -
Why destructor should be always virtual and constructor should never be virtual?

Let's discuss these 2 questions separately.

Before we start keep this class hierarchy in mind -

                                 Base
                                   ^
                                    |
        -------------------------------------------
       |                            |                             |
Derived1               Derived2                Derived3



1. Why is constructor never virtual?
While creating any object programmer always 'knows' which object he is creating i.e. Which class we want to instantiate.
e.g. Base *pBase = new Derived3();  // Though the pointer is of Base class type the programmer very
                                                         // well knows to write new Derived3()

In addition, also note that though virtual table in not specific to class instance (object), to call virtual function of any class first we need valid object of that class.
i.e. Virtual function can not be called unless we have constructed the object.

2. Why is destructor be always virtual?
There might be function of the type
void do_something_and_then_delete(Base *pBase) {
     // Do something with pBase

     delete pBase;
}

Now it is easy to understand that user might write some code like -
Base* pBase = new Derived1();
do_something_and_then_delete(pBase);
Base* pBase = new Derived2();
do_something_and_then_delete(pBase);
Base* pBase = new Derived3();
do_something_and_then_delete(pBase);

In all 3 cases correct destructor needs to get called.
User always knew name of the class to instantiate, but while deleting user did not have idea about which is the exact destructor he wants to call.
If the destructors of Derived classes are not virtual then the destructor of Base class will get called and it will lead to memory leaks and might be some other problems in the system.

If you have any better answer to improve this post please comment, I will modify the post.

Monday, June 14, 2010

Want to download website?

The best solution is to use wget.
For information regarding how to use? see -
http://en.wikipedia.org/wiki/Wget
If wikipedia is not sufficient use - man wget.

I have used this on Linux and on Cygwin (on windows).

wget is also available directly on windows -
1. Setup is available here -
http://gnuwin32.sourceforge.net/packages/wget.htm

2. wget is avaible with GUI for windows -
http://sites.google.com/site/jackparke/wgetgui
or
http://sourceforge.net/projects/wget-gui-kiwi/

Tuesday, May 25, 2010

C++ is multi paradigm programming language

I might have read this sentence at least 10-15 times in different C++ books, but did not understand (and appreciate) at those times, what it means :(.

This link might help you - Programming paradigm

In my experience typical (big) software product written in C++ uses C++ in different way (techie word : uses different programming paradigms). The application side part is moreover in OOP and the core algorithm side part is moreover functional (both coded in C++ :)).

I wont write much on this subject (as I do not understand this subject well), but once you start looking at your C++ code taking into account programming paradigms used at different levels, you will feel more proud being a C++ developer :).

I got following comment from Yogeshwar Shukla on Buzz. Adding it to original post (with little modifications)  -
References to some books
i) Chapter 2. The C++ Programming Language. - Bjarne Stroustrup (very terse)
ii) Part I and II of Object Oriented Software Construction by Bertrand Meyer (very elaborate). This not really introduce you to the meaning of paradigm but explains OO paradigm.
iii) The best discussion is in -
Concepts, Models and Techniques in Programming. It has a formal specification of programming paradigm and the entire book is devoted to discussion of paradigms in computer science known as yet


I will try to add few more good links, in future.