Skip to main content

Posts

Showing posts from May, 2009

Difference between Debug and Release versions?

Debug version: 1. Contains extra debugging information in the exe or dll built 2. Needs respective debug versioned Support files.(MFC Dll’s ended with d letter) 3. Compiled provides the extra memory guards, takes care of pointers without allocated memory. 4. Output file size (dll/exe) is very large. Release version: 1. No debugging information. 2. Optimized for maximum speed. 3. Needs respective release versioned Support files.(MFC Dll’s ended without d letter) 4. Output file size (dll/exe) is very small.

Will the inline function be compiled as the inline function always? Justify.

When we are putting inline keyword for any function or if we are defining function inside the class declaration itself, we are just requesting compiler to treat it as an inline function i. e. instead of making a function call just replace that function call with actual function body. Compiler usually decide whether to treat a requested function as an inline or not based on the size of function. if a funtion is having huge body, with lot of looping and complicated code compiler may just ignore inline request.

Describe the main characteristics of static functions.

static member functions just static data members, work for the class as a whole rather than for a particular object of a class. static member function is associated with a particular class and not with individual objects. You can call a static member function in the normal way, using dot or the arrow, in association with an object. The real way to call a static member function by itself, without any specific object, using the scope-resolution operator, like this: Classname::static_functionsname(); A static member function cannot access ordinary data members, only static data members It can call only other static member functions. static member has no this pointer.

What is name mangling?

Any functions declared in C++ are decorated or renamed before compilation by compiler. This is done to used the function overloading. We can declare function with similar names but with different argument type, number or order. To achieve this compiler internally rename this functions to distinct from each other. This is called as Name Mangling. To prevent C++ compiler from mangling name one can provide extern "C" keword preceding the function declaration. This is the reason why one need to provide extern "C" keywor

What is slicing?

Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object. Explanation: Consider the following class declaration: class base { ... base& operator =(const base&); base (const base&); } void fun( ) { base e=m; e=m; } As base copy functions don't know anything about the derived only the base part of the derived is copied. This is commonly referred to as slicing. One reason to pass objects of classes in a hierarchy is to avoid slicing. Other reasons are to preserve polymorphic behavior and to gain efficiency.

What is a smart pointer?

Answer: A smart pointer is an object that acts, looks and feels like a normal pointer but offers more functionality. In C++, smart pointers are implemented as template classes that encapsulate a pointer and override standard pointer operators. They have a number of advantages over regular pointers. They are guaranteed to be initialized as either null pointers or pointers to a heap object. Indirection through a null pointer is checked. No delete is ever necessary. Objects are automatically freed when the last pointer to them has gone away. One significant problem with these smart pointers is that unlike regular pointers, they don't respect inheritance. Smart pointers are unattractive for polymorphic code. Given below is an example for the implementation of smart pointers. Example: template class smart_pointer { public: smart_pointer(); // makes a null pointer smart_pointer(const X& x) // makes pointer to copy of x X& operator *( ); const X& operator*( ) const; X* operato

VC++ Questions

What preprocessor directive do C++ programmers almost always use to include header files?  What are the characters that enclose a Visual C++ block?  What kind of data must be enclosed in single quotation marks?  What kind of data must be enclosed in double quotation marks?  What sends output to the screen in Visual C++?  What kind of numeric data contains no decimal points?  What kind of numeric data contains decimal points?  Write the four arithmetic symbols.  Is return ever optional? If so, why is it a good idea to learn to always use return?  What are the literals in Listing 4.1? Hint: There are eight if you count the ones embedded in the cout statement.  Which of the following are variables and which are literals? Hint: A variable name cannot have quotation marks around it. If it did, Visual C++ would think it was a character literal (if single quotations were used) or a string literal (if double quotation marks were used).  '1.2' Payroll 4543.23 name 47 "Diane&qu

General Questions & Code related questions

What is whitespace?  What is meant by freeform?  What does it mean to maintain programs?  What does C++ prefer most, uppercase or lowercase letters in programs?  What are comments?  Why are comments necessary?  What does the compiler do when it sees a comment?  What do all C++ comments begin with?  What is the difference between a C-style comment and a C++-style comment?  What happens when you nest C-style comments?  True or false: Longer programs are more difficult to understand than shorter ones.  True or false: You can substitute parentheses for braces if you feel that the parentheses are more readable.  True or false: Comments are not Visual C++ commands.  True or false: You can nest one C++ comment inside another.  Match the special character on the left with that special character's description on the right.  Special Character Description [ Backslash < Left bracket } Right-angled bracket | Right parenthesis \ Forward slash (or just  slash ) ] Left-angled bracket { Left par

What is an opaque pointer?

Answer: A pointer is said to be opaque if the definition of the type to which it points to is not included in the current translation unit. A translation unit is the result of merging an implementation file with all its headers and header files.

How do you link a C++ program to C functions?

using extern "C".  Basically C++ programms mangle the functions names. It decorates the function names to different name provided in source file to support the function overloading. Hence when we try to link C++ programm to C function it shows errors. When we use extern "C" it, tells compiler not the decorate the function name. hence it can easily find the correcct functions used.

Differentiate between a deep copy and a shallow copy?

Answer: Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain ht same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects. Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable. Explanation: Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a sha

What is document-view architecture? Give me one real time example for SDI?

Answer: A MFC application is created using different classes. Each of the class has given a distinct responsibility. 1. Frame window class - Represents the application window. 2. View class- Represents the data to the user in specific understandable format. 3. Document class – Represents the applications data.( Program data used by application during its execution). MFC architecture allows user to represent the data as documents in different views (formats ex. Charts, tabular, textual). View(s) are attached to the application frame window. SDI Example: Let us say we have a application with name MySDI.. In SDI application we have following major classes derive. CMySDIApp-Application class derived from CWinApp. e) CMainFrame - Frame Window class derived from CFrameWnd f) CMySDIView - View class derived from CView (Can have multiple classes) g) CMainDoc - Document class derived from CDocument. Above three classes are bind together using a Template class called as CSingleDocTemplate.

Name few classes in MFC which are not derived from CObject class ?

ActiveX Type Wrappers CFontHolder CPictureHolder Automation Types COleCurrency COleDateTime COleDateTimeSpan COleVariant Run-Time Object Model Support CArchive CDumpContext CRuntimeClass Simple Value Types CPoint CRect CSize CString CTime CTimeSpan structures CCreateContext CPrintInfo CRunTimeClass Support Classes CCmdUI CDataExchange CLongBinary COleDispatchDriver CPropExchange Synchronization Support CSingleLock CMultiLock CRectTracker CWaitCursor Typed Template Collections CTypedPtrArray CTypedPtrList CTypedPtrMap Windows CE Database Classes CCeDBEnum CCeDBRecord.

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

A modal dialog box is popped up over the parent dialog( from which it is envoked) it never allows user to go back to parent (calling) dialog box and work until the popped up dialog box is not exited. Madal dilog box can be displayed as follows. Ex. class MyDlg; // Class derived from CDialog. MyDlg myDlg; // create an object myDlg.DoModal(); A modaless dialogbox is on user can move back and forth between the poped up dialogbox and the parent (calling dialog) For creating a modless dialogbox. needs to follow follwing things. Ex. class MyDlg; // Class derived from CDialog. MyDlg myDlg; // create object myDlg.create(uint DialogId); // Call create method by providing the dialog resource id. provide the message handling and run loop. Difference between modal an modaless dialog box. modal doesnt allow user to go back to parent window and work in it untill it is not exited while modeless allows to work in parent window. Other differnce is of creation and displaying of dialog object.

Explain pointer to the constant and constant pointer ? What is the difference between them ?

Pointer to constant does not allow to change the value at the address pointed by pointer ( *ptr). 1. const int *ptr = 3; 2. int const *ptr; // 1 and 2 both are having same meaning. *ptr = 5; // not allowed Constant pointer does not allow to change the address to which pointer is pointing. int i = 10; int * const ptr = &i; int j =80; ptr = &j; // not allowed. constant pointer to constant data int i = 9; const int * const ptr = &i; int j =90; *ptr =10; // not allowed ptr = &j; // not allowed.

What is a DLL ? What are different dll's supported in VC++ ? Explain different types of dll's.

A DLL is a dynamic link library. It provide a way of sharing common code at runtime . Any appication( exe) can load a dll in to memory at runtime, retrieving the address of the fuctions to be called / accessing resources or variables. client can use the functions, variable or resources and unloads the dll from memory. One can load the dlls either statically or dynamically. Dynamically loading - HANDLE h = LoadLibrary("MyDll.dll"); // load dll typedef FUNPTR (void) (*funcPtr ) (); // create a fuction pointer with matching signature // with that of exported function from dll. FUNPTR show = GetProcAddress( h, "Show"); // Get function address show(); // Call the fuction FreeLibrary(h); // unload dll from memory Statically loading: Include the .h file and specify .lib file to the linker for specified dll. User can access the dll content ( Exported fuctions, resources, variables) at compile time itself. Regular, Extension and pure Win32 DLL (without MFC) Regular DLL Regul

Que What is an overloaded assignment operator ? What will happen if we assigned object to self( object1 =object1) ? What is the difference between cop

Whenever any assignment operation is performed on two already created objects of such class, assignment operator is invoked. By default compiler provides the assignment operator to each class. Ex. MyClass obj1; MyClass bj2; obj2 = obj1; Default assignment operator performs the member by member copy or shallow copy 1. Shallow Copy : If user doesn't provides the overloaded assignment operator, compiler provides one by default to the class. User needs to provide a overloaded assignment operator for a class, if the class is having any memory allocations to member pointers. Ex, class MyClass { int *m_pData ; }; MyClass & operator=( Const MyClass & objectSource) { m_pData = objectSource.m_pData; return *this; } Problems Caused: If there are any memory allocations and user is not providing the user defined overloaded assignment operator, default overloaded assignment operator will be called. It will do a member by member shallow copy. it means it will assign the same memory alloca

What is a copy constructor ?What is the need for it ? What is the Shallow and deep copy ? When it gets called ? Write the signature ? What is the diff

Copy constructor invoked in following threee situations. 1. When a new object is created using existing object. Ex. MyClass obj1; MyClass obj2 = obj1; 2. When any object is passed by value. Ex. void function( MyClass obj1) ; MyClass obj2; Function( obj2); 3. When any object returned by value. Ex. MyClass function( ) { MyClass obj2; Return obj2; } Default Copy constructor performs the member by member copy or shallow copy 1. Shallow Copy : If user doesn't provides the Copy constructor , compiler provides one by default to the class. User needs to provide a Copy constructor for a class, if the class is having any memory allocations to member pointers. Ex, class MyClass { int *m_pData ; }; MyClass ( Const MyClass & objectSource) { m_pData = objectSource.m_pData; } Problems Caused: If there are any memory allocations and user is not providing the user defined Copy constructor , default Copy constructor will be called. It will do a member by member shallow copy. it means

Que What is the use of CCmdTarget? Explain Command or Message routing with SDI & MDI applications?

his class is at the second level on MFC class inheritence hierarchy, derived direcly from CObject class. This class provides the Command routing or message routing through the different MFC framework classes. Command routing or Message routing. Windows event based model -- Let say an MFC application is running and user clicks on a menu item. Follwing sequence of steps are executed. 1. Any event (message) occured on any application running on window machine, is first received by windows operating system. Windows operating system retrives the thread running that application. OS send that message to message queue of the application for which it is created. 2. From own message queue application retrives the message and sends to the window of which handle is specifed.. This message is passed to the different classess for processing as follows. In SDI Application First sent to the Frame window class ( Derived from CFrameWnd). Frame window passes it to View class( Derived from CView or

What is the use of SendMessage() and PostMessage() ? what is the difference between SendMessage() and PostMessage() ?

These are theWin32 API's provided to send a message to window Or a way to notify a window to perform some action. SendMessage() - This message is sent directly to the window. It means it doesn't return until, the "sent message" is not processed( Message handler is not executed). PostMessage() - This message is sent to the application message queue, from where application will pick up the message later on, gets processed. This API returns immediately as soon as it posts the message to message queue. It never waits for the respective message handler to be executed.

What is the top base class for MFC Framework? If I derive a new class from CObject class what are the basic features my derived will get? OR what are

CObject If I derive a new class from CObject class what are the basic features my derived will get? OR what are the main features provided by CObject class? Answer: a) Runtime class identification– know the objects class type at runtime. b) Dynamic object creation.– Create object at runtime. c)Serialization - Storing object to disk or reading object from disk d)Diagnostic support. – provide debug information such as trace, memory dump.