Q: What is auto_ptr?
In MFC we can use a Global Mutex to create a one.
Copy Constructor will be called – fn returns an object, passed an object to a fn, throws an exception of Object type.
Here in the above example, both the objects are pointing to same memory location. See below -
Global variable and Static Variable init – Constructor calls
Q: Overloading Prefix ++ and Postfix ++.
Postfix operators are declared with a dummy int argument (which is ignored) in order to distinguish them from the prefix operators, which take no arguments:
Q: Namespaces – Unnamed.
Unnamed is single across the translation units.
Q: Why do we have sort,find function specific to std::map ,std::list even if we have global std::sort ,std::find functions.
A: Owns a dynamically allocated object and perform automatic cleanup when the object is no longer needed. Get fn will return the Value and Release will actually release the auto-ptr. Reset fn will delete the former object and takes the new one.
- Wrap Pointer Data members
- Sources and Sinks
Q: What is Virtual Inheritance?
A: Using Virtual Base classes in Multiple Inheritance.
Q: Explicit constructors
A: Constructors are defined as Explicit when there is no need for the implicit construction is needed. For an instance any constructor defined as one single parameter will be assigned to the object – implicit conversion. At times this is error prone as we might not need it such as assigning it to null. By declaring the constructor explicit we will enable this phenomenon.
Q: What are the different casts available in C++. What are their uses? static_casts vs dynamic_casts?
A: Different casts that are available in C++ are –
1. reinterpret_cast : used for Pointer to Pointer and Pointer to Int and viceversa
2. static_cast: Pointer to Pointer. used for implicit casting and Inverse casting. For instance derived to base is allowed implicitly where as the otherwise is not yet allowed can be casted using static casting however the result is not checked for validity.
3. dynamic_cast: Pointer References to Objects, Checked for validity. Refererences will throw bad cast exception pointers will return NULL if the cast is not fruitful.
Q: What is a specialization?
A: Specialisation is a kind of inheritance where in which the more generalized relationship gets narrowed down to the fullest atomicity of it. For instance Vehicle to Car is specialization.
Q: Describe private and protected inheritance.
Private: All public and protected members in parent class are inherited as private. Protected: All public and protected members in parent class are inherited as protected.
Both allow overriding the virtual fns in the parent class. However the protected inheritance exposes the parent members to its Derived -> Derived classes. (Grand Children)
Q: What are the different uses of the const keyword?
const actually makes a variable constant, which can not be changed during runtime. Uses will include – function parameters passing through reference to const, pointer to const, where you externally specify the values are not supposed to be changed or passed to the functions which has a potential ability to change it. Some samples of using const –
const Student* p – Student object can not be changed i.e. const (…) Student* const p - pointer can not be changed but object can be. const Student* const p – Ptr and Obj can not be changed.
const Student& p – Student Object can’t be changed. Student& const p – references are already constants so this is trash. Student const& p – equivalent to const Student& p.
Q: Design Patterns: singleton and factory
Singleton: a class which provides only one object to exist. Implementation should be –
Class Singleton { Protected: Singleton() {} Private: static Singleton* _instance; Public: // Function to create the object static Singleton* Instance() { if(_instance == 0) _instance = new Singleton(); return _instance; } // Function to Destroy the Object void Destroy() { delete _instance; _instance = 0; } |
Q: What are Virtual constructors?
These are not directly supported by the C++. Can be implemented through some Factory Design Pattern in which the base class does init and so on.
Q: What are virtual destructors?
Virtual destructors are needed for a class which means to be a base for some classes. When the destructor is made virtual the destruction calls will happen from derived to the base as it happens with a static derived object instantiation.
Base *b = new Derived; delete b; //calls the destructor of base only if destructor is not virtual.
Q: What is the difference between the stack and the heap [in c++]
Stack is runtime in CPP. Block entry and exit decides the life of the stack. The requirements for variables to be allocated to the stack are – Memoy needed has to be identified at compile time.
Heap is a free store. Memory can be decided at runtime. Memory allocated with new and deallocated with delete. Manual memory mgmt is required.
Q: What are Virtual Functions?
A virtual function is a fn which implements the runtime polymorphism. Whenever a class has a virtual function declared in it , the compiler adds a hidden member var which is a pointer to the vtable i.e. vPtr. When a class is derived out of the class the hidden member is update with the vTable address. Compiler at the time of Constructor call of the derived class object will initialize the addresses in vTable.
Q: What is a Pure Virtual function?
Virtual functions are the functions in which the base class holds an optional implementation of the member where if a child class has some implementation for the same member then Child’s implementation will be called. However if the child is not having the implementation then parent’s implementation is evoked. While at the parent level if the behaviour is not decided and child has for sure the same behaviour then those behaviours are implemented as pure virtual functions where in parent class the function is declared as virtual and 0 is assigned.
It becomes mandatory for any class being derived from a class which holds atleast one pure virtual will need to implement the pure virtual function.
Q: What are Abstract Classes?
Abstract classes are the classes with at least one pure virtual function. As they are not full we can not implement the class.
Q: What are Friend functions?
Friend Functions are the non member functions which can access the private and protected members of a class.
Q: What is an Interface in Java?
An interface in java is nothing but the collection of methods without definitions. Can be implemented through a class.
Q: What are Smart pointers?
Smart Pointers are the objects that store the Dynamically allocated objects (Heap). They were similar to normal pointers except that they automatically delete the object pointed as the need for them is not anymore exists. The templates are -
Scoped_ptr – Similar to Auto Ptr, However it is impossible to transfer ownership, non-copyable.
Scoped_array – For Arrays - Heap
Shared_ptr – Standard – Shared Ownership
Shared_array – shared array
Week_ptr – weak reference to a pointer managed by shared ptr.
Intrusive_ptr – reference count is maintained.
Q: Describe Exception Handling briefly
It is basically surrounded around try, catch, and throw in C++.
Q: Mutable Keyword
Key word used to suppress the const keyword mentioned in const functions so that the variable can be modified though the function is const.
Q: Difference btw Copy Constructor and Assignment Operator?
Copy Constructor initializes the memory and Assignment operator is used for the existing memory. Both are similar for simple classes but classes with composition and for more complex classes these are different.
class thing { public: item * ptr; int a; }; thing *object = new thing(); thing *objectcpy = new thing(object); |
Here in the above example, both the objects are pointing to same memory location. See below -
class thing { public: item * ptr; int a; thing& operator = (const thing& other) { a = other.a; ptr = new item(other.ptr); return *this; } thing (const thing & other) { a = other.a; ptr = new item(other.ptr); } }; thing *object = new thing(); thing *objectcpy = new thing(object); |
Q: What runs before main()?
Global variable and Static Variable init – Constructor calls
Q: Overloading Prefix ++ and Postfix ++.
Postfix operators are declared with a dummy int argument (which is ignored) in order to distinguish them from the prefix operators, which take no arguments:
Class& Operator ++(); //prefix Class& Operator ++(int unused_temp); //postfix
Q: If exception thrown from destructor what happens.
Never throw an exception from Destructor is the basic rule. As the Stack Unwinding will be in progress throwing the exception will force to loose some information with respect to the compiler.Q: Why do we need to have private inheritance?
- When you need to model the composition
- When you need to override certain methods of Parent.
Q: Tell a case where you would prefer using Templates over Inheritance, also vice versa.
Templates are preferred for generic Programming.
Q: There was a question around using mutex and an exception being fired in the middle of it. The mutex is locked, how will you handle this situation?
- Using try catch finally in exception handling
- Wrapping up the Mutex object in a class, in the destructor unlocking the same.
Q: Difference between new and malloc
- malloc will not call constructor
- forces a typecast
- malloc is a fn and new is an operator
Q: Namespaces – Unnamed.
Unnamed is single across the translation units.
Q: std::list std::map internal implementation
Q: Why do we have sort,find function specific to std::map ,std::list even if we have global std::sort ,std::find functions.
MFC _ FAQ
Q: Document/View Architecture
Document / View Architecture revolves around the Document Template and Document, View and FrameWnd. Document Stores the Data. View is the Canvas on which the display happens. Framewnd contains the view. Document template specifies the relationships amongst these. There are two types – SDI and MDI.
Q: Command Target.
CCmdTarget: The base class for the MFC MessageMap architecture. CWinApp is derived from it. CView, CDocument, CWnd, CFrameWnd are some more. Command Targets are the candidate objects of the framework who implemented message maps.
Q: Command Routing
MDIFRameWindow :– MDIChildWindow -> This Frame Window -> Application Object
MDIChildWindow :- View -> FrameWnd -> Application Object
View :- View -> Document
Document :- Document -> Document template
MDI Flow --> MainFrameWindow -> Child Window’s View -> View’s Document -> Document Template -> Framewnd -> Application
Q: Multithreading Creation of threads.
Worker Threads – to do some background work. Will have a thread function, prototype should be -
UINT fnThread(LPVOID param)
CWinThread mythread = AfxBeginThread (fnThread, &info);
Userinterface Threads – Works with an interface element. Message map exists.
1. Define a Dyna class (DECLARE_DYNCREATE )
2. Use – AfxBeginThread(RUNTIME_CLASS(DYNA_CLASS)) to create CWinThread obj.
Q: Synchronization objects like Critical Section, Mutex
1. CEvent: Set, reset, Lock. Autoreset and Manual.
2. CCriticalSection: within the same app. Lock and Unlock
3. CMutex: Across processes
4. CSemaphore: maintains a resource count. Inits with the max count and current count
Q: What is Waitforsingleobject.
The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters the wait state until the object is signaled or the time-out interval elapses. It at times changes the values of the objects also. Such as semaphore resource count.
Q: What is CSingleDocTemplate.
Document template used for SDI app’s. gets registered with WinApp while creation.
Q: CView derived classes like CListView ,CFormView.
Control Simplifying classes for being used as views in DV arch.
Q: Drag/Drop using MFC
The drag-and-drop can be obtained in two ways; the first is OLE and the second is using the MFC. MFC supports the functionality of drag-and-drop without using OLE. To enable drag-and-drop in your application, you'll have to perform the following tasks:
- The CWnd::DragAcceptFiles () enables you to accept the drag-and-drop functionality for your application. The only parameter to the function is of the BOOL type and which, by default, is set to FALSE to disable the drag-and-drop functionality. Setting it to TRUE will enable your application to handle WM_DROPFILES message for the CWND class.
- The DragAcceptFiles function can be called of any CWnd derived class; handling the WM_DROPFILES will enable your application for drag-and-drop.
- The override of CWnd::OnDropFiles(hDropInfo) consisting
o DragQueryFile(hDropInfo, -1, NULL, 0) returns the number of files
o DragQueryFile(hDropInfo, , , ) will return the file name into the string
o ProcessFileName() will copy it.
o DragFinish will end the process.
Q: Printing in MFC
OnPreparePrinting() - override DoPreparePrinting – PageCount other print related setup
OnBeginPrinting() – Just Before Begin – For allocating Fonts and resources
OnPrepareDC() – called before each page is printed. Used for Viewport and clipping
OnPrint() – before OnDraw() – For Header/Footer – whatever is not there in OnDraw
OnEndPrinting() – when finished printing - for deallocations
Var m_bContinuePrinting should be set to FALSE if the print is to be stopped in OnPrepareDC
Q: DLL’s in MFC
DiskSpace Saving, Flexible Upgradation and Support, Language independent, Extension DLLs, Resource DLLs.
Implicit Linking (Static – link the .Lib and use extern declarations) and Explicit Linking (Dynamic – LoadLibrary and GetProcAddress).
Regular DLL -> Statically Linked and Dynamically Linked to MFC. ExtensionDLL - > Extends the use of MFC. AFX_EXT_CLASS
Q: What are DECLARE_DYNCREATE, DECLARE_DYNAMIC, DECLARE_SERIAL macros
DECLARE_DYNAMIC This macro will Add CRuntime-information to a class. isKindOf()
DECLARE_DYNCREATE This macro adds and fills the 'm_pfnCreateObject' factory function.
DECLARE_SERIAL This macro adds a.o. registration of the class. Use of << and >>
Q: Explain CWinApp class – general methods, what OnIdle do?
Called when the Application Message Queue is empty. Returns 0 enabling further processing. lCount variable is the parameter denoting howmany times the application was idle. This is Zeroed when a new message is received and processed.
Q: How to change background of a control in dialog box?
Using WM_CTLCOLOR.
Q: Why do we declare extern “C” to call a function of dll in C++ or MFC?
By causing C++ to drop name decoration, the extern "C" syntax makes it possible for a C++ module to share data and routines with other languages.
Q: ADO ,SQL
No comments:
Post a Comment