Sunday, January 10, 2010

Good C++ Questions

* Would you be able to explain why/when base classes should have virtual destructors?
Answer : Only polymorpic( class has any virtual functions ) base class should have Virtual Destructor . C ++ specifies that when a derived class object is deleted through a pointer to a base class with a non-virtual destructor, results are undefined If derived class object pointer is accessed as base class pointer .now delete of that pointer(which points to derived object ) will release in Partial free i.e only base object will destroy and derived part will remain as it is . Virtual Destructor ensures proper calling of destructor
Putting virtual Destructor in nonpolymorphic class is an overhead as it will increase Size of every OBJ by 4 byte by inserting VPTR.
* Would you be able to differentiate between inheritance of interface and inheritance of implementation?
Answer: Derivation of Pure virtual function is inheritance of interface Pure virtual funtion will have no implemetation . and derived can implement it on their own . it only derives the interface
Derivation of Simple Member function is inheritance of implementation As derived calss will have same implemetatin of the function as it was in Base class .

* What sorting algorithms do you know? How fast are they?
Answer: I know Merge sort ,Bubble sort ,Quick Sort ,BST (Binary search tree ),
BST is best O(logn). But choice os soritng Algo depends on data size/data pattern also .

* What is "placement new" and why/when would You use it?
Answer: New gets Memory From Heap. But with Placement option we can provide A pre Allocated memory location to save object.
We Use it to void Mem allocation fail which is very rare . It is more used in making Memory pool . It Improves Performance also .

* Should a destructor throw an exception when it detects a problem?
Answer: 1) Destructor Should not throw an error . As Some time Destructor are called in an error scenario
In an Exception Handling from throw(obj) to catch all frames are deallocated automatically Which is called stack Unwinding All local Objs Destructor are called automatically at that point of time if any of the distructor will throw an error . Than its an problem to go to catch or process the thrown error So terminate got called and the whole program will exit

2) If there is and Array of pbjects and all have to dellaocate one by one .
If one in begining will throw an error How others destructor will get called This behaviour is undefined in C++ .
Thing is Destructor should Swallow the Exception in themselves .

* Can you explain the difference between "operator new" and "new operator"?
Answer: new operator is used for Memory allocation and it calls constructor also for .
operator new : Is actual function which only allocates memory Its Prototype is < void* operator (size_t) > , it can be overloaded .

* Where in a hierarchy should you use virtual inheritance?
Answer : In Multiple Inheritance If we know that at some level of inheritance Diamond problem ( A-->B A--->C and B,C-->D now D will have two instance of A) can arise.
To avoid the ambiguity of base object . Virtual Inheritance is required at first level i.e ( A virtual-->B A virtual --->C)

* How can you reseat a reference to make it refer to a different object?
Answer : Its not Allowed. REfrence cannot refer to new OBJECT (address) unlike a pointer.
We can understand like this Reference is a Const Pointer .
int d = 20;
int * const pref = &d ;
now pref++ is not allowed
*pref = 40 ; //is allowed ,pref cant point to any new location.
I hope we understand Diffrence between const pointer and pointer to const .
link : http://blog.voidnish.com/?p=37
Can we overload a Function by changing return type ?
Ans : No Changing of return type is ot considered as change in Function Signature
The C++ compiler identifies a function by its signature (function signature). The function signature is broken down into the following components in that order.

return type ,function label or name and parameters list

The parameter list is further defined as parameter label or name. If the order of the parameters (datatype name) changes even if the function name/label does not change the signature is considered unique. So in C++ it is conceivable to have many different functions/methods with exact same label name (for example Function1) so long as the entire signature is considered unique. So changing the number of parameters their data types the order of them or any combination of these can change the function signature.
Important Note: Changing the return type ALONE is NOT considered change in signature.

1 comment:

Followers