Tuesday, May 11, 2010

Unexpected Exception

How to specify Exception?
 func()                    // this can throw any Excpeption
 func()  throw()      // this Will NOT throw any Excpeption
 func()  throw (bad_alloc , X )  // this can throw only specified  Excpeption

unexpected() is called when function throws exception other  than specified 

It uses     _unexpected_handler  which can be changed  
unexpected_handler old_hand  = set_unexpected( newfunction);
 
void newfunction( ) 
{
   cout << "Hi" << endl;
   terminate( );
}
 

Sunday, May 9, 2010

Auotmatic Type Conversion and use of Explicit Keyword

1. By Constructor Conversion :
        See this situation .

Output :
Constructor B called
Hi inside Function
--------------------------------------------------------------------------------
Preventing constructor conversion.
This Behaviour of automatic calling of constructor . can be stopped if constructor is explicit.
Explicit Constructor: Constructor with explicit keyword.It prevents compliler from performing implicit (Unexpected) type conversions.


After this   func (a); will not get compiled .

Output:Compliation Fails
auotTypeConversion.cpp: In function `int main()':
auotTypeConversion.cpp:18: error: conversion from `A' to non-scalar type `B' requested

2. Operator Conversion :

OUTPUT:
inside conversion operator B--> A
inside A constructor i=10
Function call

Saturday, May 8, 2010

Exception Handling

Q How to catch all type of Exception ?
Ans Three dots try { } catch(...)
three dots is called   Ellipsis

Friday, May 7, 2010

When to return const refrence ?

a)When do we return by refrence?
Ans:. it avoids copying . But never return reference of local object .
     So that functions can be l-values
     We do this when we want to modify  lvalue  . we do this in operator overlaoding of " ="  to support a=b=c  chaining 
MyClass & operator=(const MyClass &rhs);
MyClass & operator+=(const MyClass &rhs) 
 
b) Returning just const
As const return can be assigned to nonconst variable and changed .
But if function is used in expression then temporary return as it is const will be prohibited .
struct foo
{
void bar() {}
void barfoo() const {}
};

foo foobar1() {return foo();}
const foo foobar2() {return foo();}

int main()
{
foobar1().barfoo(); //perfectly legal, calling const member function of non-const object
foobar1().bar(); //perfectly legal, calling non-const member function of non-const object
foobar2().barfoo(); // perfectly legal, calling const member function of const object
foobar2().bar(); // this willl not compile
}

In operator overloading we use it often to make sure
const MyClass MyClass::operator+(const MyClass &other) const


(a+b)=c // this kind of things will not get compile
We make sure (a+b).func()  is allowed only if func()is const function .
this is called const correectness

c)When to return const refrence
This make lot of sense .


http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

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
   

Followers