Monday, March 2, 2015

C++, VC++ and MFC interview questions.

1.) How many constructor can a class have?

A class can have any number of constructors. However after creating a constructor explicitly the default constructor becomes invalid for that class. We will need to define a constructor with zero argument explicitly.


2.)What is the difference class and structure?

The main difference between structure and class is that members in structure is public by default where as members in class is private by default.

3.) What is Singleton class?

Singleton class is required when only one object of a class needs to be created throughout the life of the program.

example:
#include
using namespace std;

class Singleton
{
private:
static bool bInstanceflag;
static Singleton *pSingle;
Singleton()
{
private constructor
}

public:
void method();
static Singleton* GetInstance();
}

bool Singleton::bInstanceflag = false;
static Singleton::pSingle = FALSE;
Singleton* Singleton::GetInstance()
{
if(!bInstancefalg)
{
pSingle = new Singleton;
bInstanceflag = true;
return pSingle;
}
else
{
return pSingle;
}
}


void main()
{

}

4.)  What is base class of MFC Library?

CObject is the base class for the Microsoft Foundation Class Library. At most 80% of MFC classes are derived from CObject.

CObject provides

– Serialization support
– Run-time class information
– Object diagnostic output
– Compatibility with collection classes

CObject does not support multiple inheritance. Your derived classes can have only one CObject base class, and that CObject must be leftmost in the hierarchy. It is permissible, however, to have structures and non-CObject-derived classes in right-hand multiple-inheritance branches.
You will realize major benefits from CObject derivation if you use some of the optional macros in your class implementation and declarations.

The first-level macros, DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC, permit run-time access to the class name and its position in the hierarchy. This, in turn, allows meaningful diagnostic dumping.

The second-level macros, DECLARE_SERIAL and IMPLEMENT_SERIAL, include all the functionality of the first-level macros, and they enable an object to be "serialized" to and from an "archive."

For information about deriving Microsoft Foundation classes and C++ classes in general and using CObject, see Using CObject and Serialization.

5.) What is the Difference between ASSERT and VERIFY?

ASSERT evaluates the expression only in the debug version and will throw an exception if the result is 0 and the program termintes.
VERIFY evalutes the expression in Debug and Release version also and if the result is 0, will throw an exception only in Debug mode.

6.) Difference between PostMessage and SendMessage?

The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.

7.) What is the difference between PeekMessage and GetMessage?

We can use the PeekMessage function to examine a message queue during a lengthy operation. PeekMessage is similar to the GetMessage function, both check a message queue for a message that matches the filter criteria and then copy the message to an MSG structure. The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.


8.) Difference between Modal and Modeless Dialog?

Modal dialog box captures the message loop, whereas model less does not. Call DoModal to create the dialog window and its controls for a modal dialog. If you wish to create a modeless dialog, call Create in the constructor of your Cdialog class.


9.) What is the use of the class CCmdTarget?

It is the base class for the MFC library message map architecture. Which maps commands/messages to the member functions to handle them. Classes derived from this are CWnd,CWinApp,CFrameWnd,CView, CDocument

10.) What is the difference between hInstance and hWnd?

hWnd is the window’s handle and is how the OS defines a window when talking about it inside the PC.
hInstance is the OS’s handle for the program when running it.


11.) What is the difference between hinsrtance and hprevinstance in WinMain function?

hInstance used to have the handle of the current instance

hPrevInctance used to have the handle of last instance, hPrevInstance is NULL if only one instance is running


12.) Difference between Window’s OnPaint and View’s OnDraw overridable functions?

Window’s OnPaint function executes corresponding to each WM_PAINT messages. But in the case of document-view architecture when a view receives a WM_PAINT message, it invokes view’s OnDraw function. Actually the framework fields the WM_PAINT message, creates a CPaintDC object, and calls the view’s OnDraw function with a pointer to the CPaintDC object.

The fact that the view doesn’t have to construct its own device context object is a minor convenience. The real reason the framework uses OnDraw is so that the same code can be used for output to a window, for printing, and for print previewing. When a WM_PAINT message arrives, the framework passes the view a pointer to a screen device context so that output will go to the window. When a document is printed, the framework calls the same OnDraw function and passes it a pointer to a printer device context.

13.) Command routing in MFC?

One of the most remarkable features of the document/view architecture is that an application can handle command messages almost anywhere. Command messages is MFC’s term for the WM_COMMAND messages that are generated when items are selected from menus, keyboard accelerators are pressed, and toolbar buttons are clicked. The frame window is the physical recipient of most command messages, but command messages can be handled in the view class, the document class, or even the application class by simply including entries for the messages you want to handle in the class’s message map. The flow of command messages from Active View to DefWindowProc is know as command routine.



14.) How Message Map works in an MFC application?

The message map functionality in an MFC application works by the support of 3 Macros, DECLARE_MESSAGE_MAP, BEGIN_MESSAGE_MAP, and END_MESSAGE_MAP and the WindowProc function implementation.

MFC’s DECLARE_MESSAGE_MAP macro adds three members to the class declaration: a private array of AFX_MSGMAP_ENTRY structures named _messageEntries that contains information correlating messages and message handlers; a static AFX_MSGMAP structure named messageMap that contains a pointer to the class’s _messageEntries array and a pointer to the base class’s messageMap structure; and a virtual function named GetMessageMap that returns messageMap’s address. BEGIN_MESSAGE_MAP contains the implementation for the GetMessageMap function and code to initialize the messageMap structure. The macros that appear between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP fill in the _messageEntries array, and END_MESSAGE_MAP marks the end of the array with a NULL entry.

Thus corresponding to each message a class can traverse through it’s and base classes message map entries to find a handler till it to reach the DefWindowProc.

15.) What is the difference between thread and process?

In the Microsoft Win32 environment, every running application constitutes a process and every process contains one or more threads of execution. A thread is a path of execution through a program’s code, plus a set of resources (stack, register state, and so on) assigned by the operating system.

No comments: