Programming Concepts Question:
Download Questions PDF

Tell me why constructors does not supports visual functions?

Answers:

Answer #1
Constructor does not support virtual functions because we need an object to invoke a virtual method in the first place and more over constructors are used to initializing objects,which is a static type.Virtual functions are invoked on the dynamic type of the object,hence the object may not be properly initialized when a constructor call is made.Since the derived part of the object would not be initialized during the execution of base class constructor,hence calling any virtual method from base class constructor does not work well.

Answer #2
Unlike object oriented languages such as Smalltalk or Python, where the constructor is a virtual method of the object representing the class (which means you don't need the GoF abstract factory pattern, as you can pass the object representing the class around instead of making your own), C++ is a class based language, and does not have objects representing any of the language's constructs. The class does not exist as an object at runtime, so you can't call a virtual method on it.
This fits with the 'you don't pay for what you don't use' philosophy, though every large C++ project I've seen has ended up implementing some form of abstract factory or reflection.

Download Programming Concepts Interview Questions And Answers PDF

Previous QuestionNext Question
What is multithreading?Explain what are mutants and explain mutation testing?