Friday, April 30, 2010

Six important People of C++

Monday, April 26, 2010

Unhandeled Exceptions in C++ terminate

If a Exception is not handled With proper catch Block of same type or  catch(...) is also not there terminate run-time function is called Which calls abort( )   internally .
To Avoid this we can provide our own Termination Handler function  like:

terminate_handler  set_terminate( term_function );
 
This can be  done  any where in the code . BEFORE EXCEPTION IS THROWN.
Also only one Function can be registered for as Handler
_uncaught_handler
Multiple calls of  set_terminate() overwrites previous value.

OUTPUT :
terminate called after throwing an instance of 'char const*'
Aborted
Note : set_terminate is after exception is thrown.

OUTPUT:
Second Handler was called by terminate
Note :first  handler is overwritten

If we throw Derived object will it catch by base or derived ? see code and Answer


Output :
base cought

NEXT Question comes in Mind is  : 
What if inheritance is of PRIVATE or PROTECTED
Ans: in that case object will be cought at Derived catch block only.

Remember : Only Public inheritance is " IS-A "relation ., can be treated as BASE TYPE  

Friday, April 23, 2010

Virtual Function with default Arguments

Can Virtual Functin have default Arguments?
Yes they can bu tthere is a catch here
Remember virtual functions are dynamically bound, but default parameters are statically bound .
Thing is for Runtime efficiency default parameter are not dynamically bounded .



OUTPUT:
der default 4  Passed  5
base default 4  Passed 15

Friday, April 9, 2010

Some CPP certifications

1)Brain Bench, Inc. USA, C++(expert level),
2)Professional Aptitude Council, Inc. California,
C++(advanced):

Thursday, April 8, 2010

Inline Virtual Function

Inline doesn't go with Virtual  . Generally compiler ignores inline directive for virtual function.
Inline has to be done at compile time but Virtual says wait until Run time At compile time compiler doesn't know which function to insert .
 Point    :Virtual function can be inlined if they are called by OBJ (not by pointer ) But we hardly use virtual function without Pointer
   

Some Random C++ question Which I dont want to forget

Q Global object how are the created when?

OUTPUT :
base CTOR
In main
base DTOR
------------------------------------------------------------------------
Q ) Qualifiers availabel in C++ ?
Ans : I always forget mutable , 3 qualifiers are there const ,volatile and mutable .
 What is Mutable .
 Ans :  It specifies  that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.

struct data
{
char name[80] ;
mutable int age ;
}
const data person  = { "Amit", 24 };        // Initialisation
strcpy (  person .name, "Sharma");       // compiler error
person.salaray = 2000 ;                         // Allowed since it is mutable
---------------------------------------------------------------------

Q) What problem does the namespace feature solve ?
 http://www.glenmccl.com/ns_comp.htm

Will it core ??? if yes where


My First Answer was yes De-Referencing NULL pointer will core at line 15
This is wrong function is binded to class not an object compiler just checks the TYPE of pointer and calls Func1 Successfully
It cores at line 16 where in function s=0; is done
Thing is as long as no instance attribute is used in the func(), and the method is not virtual   , NULL pointer  need not to be dereferenced.

Tuesday, April 6, 2010

Virtual Inheritance

Well  we all know why it is Required To resolve Diamond Problem
What if  i have a code like this  where class Bsse has no function and derived twice in Diamond way

Output: in Base In Der1 in Base Der 2 Most Derived
Point is until we access any thing common in base class Explicitly there is NO issue
Lets see Accessing any variable from base : Output : Compilation fails  virtual.cpp: In function `int main()':  virtual.cpp:31: error: request for member `Base_a' is ambiguous  virtual.cpp:6: error: candidates are: int Base::Base_a  virtual.cpp:6: error: int Base::Base_a To solve we need Virtual inheritance .   Output : See the Constructor Sequence
in Base
In Der1
Der 2
Most Derived
Accessing  Base_a 0

how to make Singleton Thread safe

Monday, April 5, 2010

Protected Constructor ??

these are used to make abstract class  .
Class who has Protected Constructor  Can 't be instantiated But available to inherit  

Followers