Saturday, June 27, 2015

VC++ interview questions

What is CWinThread class? Explain its advantages..

At the beginning of every application has one and only one thread, called as primary thread. CWinApp which is the user derived class encapsulates this thread. In this case pointer to your CWinApp object is also a pointer to the CWinThread object for the primary thread. Once your application gets going, it can create as many new threads as it wants to manage various tasks. CWinApp is derived from CWinThread. It also allows multiple threads within given application. CWinThread supports two types of threads: 

Worker threads:  It doesn't has message pump i.e. thread that runs in the background
User interface threads: it has message pump i.e. messages received from system.
It can be declared as: 
class CWinThread : public CCmdTarget
The advantages UI thread has is:
It can assign more than one thread at a time like a multi-threading system.
It terminates the application without any hassles.


Explain the important features of VC++

VC++ is a Microsoft product and used as an IDE (Integrated Development Environment). It comes with visual studio. It has tools for coding and debugging visual codes. The main features of this are shown below

Smart pointers
New containers
Polymorphism
Expression parsing
Garbage collection
Exception Handling

We can develop dialog based/SDI/MDI applications in visual c++.


What is CMutex? How can we use it? 

CMutex is a synchronization class which uses synchronization object named as mutex. It allows multiple program thread can share the same resource by taking turns. At the starting of the program it creates a mutex at beginning and then a system gives a unique ID or name for it. After that, any thread which needs the resource can use the mutex to lock the resource. For example access of a file. To use CMutex construct its object when it is needed to be accessed. Give the name of mutex and to the application will own it. You can access mutex when constructor returns again to the start. When you are done accessing the controlled resource then call CSyncObject::Unlock. 
Mutex is a contraction of the words mutually and exclusive. Like critical sections, mutexes are used to gain exclusive access to a resource shared by two or more threads. Unlike critical sections, mutexes can be used to synchronize threads running in the same process or in different processes. Critical sections are generally preferred to mutexes for intraprocess thread synchronization needs because critical sections are faster, but if you want to synchronize threads running in two or more different processes, mutexes are the answer.
Suppose two applications use a block of shared memory to exchange data. Inside that shared memory is a linked list that must be protected against concurrent thread accesses. A critical section won't work because it can't reach across process boundaries, but a mutex will do the job nicely. 

What are frames and CFrameWnd?

Frames contain view windows. Frame windows allow the user the capability to use status bars, toolbars, and splitters.

CFrameWnd contains the implementation to perform the function of a main window as wells as it keeps track of currently active view that is independent of Windows active window or input focus. It also provides the functionality of a Windows single document interface (SDI) overlapped. This is contained in CFrameWnd class.

For example:
class CFrameWnd : public CWnd

Explain the significance of InitApplication() in VC++.

InitApplication() function is a Boolean method to display an output. This gives output as true if application executes correctly or else it gives an error. This function calls RegisterWindow() which registers the components used in the program or in the execution. The representation of it is as follows:

MSG msg;
If ( ! InitApplication (hInstance))
return(false);
where the hInstance is the handle that identifies the current instance of the application and it takes the current executing parameters. 

Explain modal and modeless dialog box? What is the difference between modal and modeless dialog box?

Modal dialog box is the dialog box in which user response is required before continuing with the program. The Paragraph dialog box of WordPad is a good example of it when it is displaying, the user cannot use any other part of WordPad unless he or she closes this object first

Modeless dialog box is the dialog box in which the screen remain stable and available to all the user for use at any time but it doesn’t stop their and allow other user work to be carried out without any interference. For example The Find dialog box of WordPad.

The difference between Modal and modeless dialog box are:-

Modal captures the message loop and doesn’t move on to other objects until the current object is closed but modeless doesn’t capture it.

Modal doesn’t allow its parents window to be accessed until all other windows are closed but it is allowed in modeless.

Example of Modal is Save and Save As in Microsoft word until they are opened you can not do anything until you close the window whereas Find and Replace can simultaneously be executed together.

What is command routing in VC++? Explain its significance..

The commands working are having limitation to make message-map connections between commands and their handler functions. Command messages are mostly routed to its class objects or other objects but windows messages are usually sent to the main frame window. The framework is used to route commands through a standard sequence of command-target objects. In return command-target checks its message map to see if it can handle the incoming message or not. Order in which command-target routes command is:-

-> to its currently active child command-target object.

-> to itself.

-> to other command targets.

This mechanism of command routing reduces the overall cost which would have increased if it would have been done without this interface because the framework generates commands only when it interfaces with the user.