Thursday, November 06, 2008

c++ - A ready reckoner

1)typedef syntax for function name:
Usage : typedef return-type (classname::*fnname)(argument)
2)Operators that cannot be used for overloading
:: . .* .->
Scope resolution member selection member selection through pointer to function
3)int A::a=0; initializing static variables where a is a static int in class A
4)explicit constructors dissallow use of automatically generated functions
explicit stack(int a)
{

}
main()
{
stack a=10;//will not call statck(int a ) since explicit is specified
}

5.Const members can be initialized only usuing initialization list
stack():a(10) if a is of type const int a;
trick:
stack{
enum{a=10};// initializes a to 10
}

6: const * char a=5; constant pointer
const char * a=5;pointer to a constant
7.Dynamic cast converts base class object to derived class object
8.Virtual destructor is needed in Base class when Base class ptr points to derived object
otherwise derived destructor will not get called
9.Base ptr can be assigned to derived but not vice versa
10.operator new can be overloaded
11.To convert an object of type S to an int then we need to define a conversion operator()
operator int()
{
return this->i; //where i is an int i member of class S
}
void main()
{
int i;
S s;
i=s;//calls operator int
}
12) Making a privately inherited member function public
say fun is public in base class and we inherit this by private inheritance
then declare base::fun(no arguments or return values) in public: of derived
fun will be accesible to objects of derived class

13) say top is a global var and top is also a variable inside a class then
::top inside the class refers to the global variable
14)protected member cannot be accessed from main
15)derived():base() is the same as derived() cause anyway derived object will first call base class constructor
16)class D:public B1,public B2
construction:B1,B2,D
destrcution:D,B2,B1
17)Friends of a base class are not inherited by any classes derived from that base class
18)The name of a friend function or class first introduced in a friend declaration is not in the scope of the class granting friendship (also called the enclosing class) and is not a member of the class granting friendship
19)
using directive - using namespace std //provides access to all members
using declarative-using A::x
explicit A::x
20)
When operator() is overloaded...The function call a('z') is interpreted as a.operator()('z').
similarly A(obj_B) will be obj_B.operator A()
21)A pseudo destructor has no effect at all
22)Malloc fails with std::bad_alloc
23)Virtual function mechanism does not work within the constructor
i.e when a derived object is created first base class constructor is called and if a virtual fn is called from within
the base class constructor it will still call only the base class version since the derived object is still not constructed
24)Operator new(size_t sz)
X * ptr=new X; is equivalent to new(Size(X))
The statement delete ptr calls X::operator delete(void*) delete when overloaded accepts a void pointer
25)const data are initialized in the initialization list....there is no const class
26)copy constructor gets called in call and retun by value
26a) When an object is passed to a function,the constructor does not get called even though a local temp object is created
27)class A
{
void display const();
};
defn: A::display()const
{}
28)
A copy constructor of a class A is a non-template constructor in which the first parameter is of type A&, const A&, volatile A&, or const volatile A&, and the rest of its parameters (if there are any) have default values.

29)

template syntax: template class mycontainer { ... };
specialization specialization syntax:template <> class mycontainer { ... };

30)

template
T mypair::getmax ()
Confused by so many T's? There are three T's in this declaration: The first one is the template parameter. The second T refers to the type returned by the function. And the third T (the one between angle brackets) is also a requirement: It specifies that this function's template parameter is also the class template parameter


31)
A function try block on main() does not catch exceptions thrown in destructors of objects
with i)static storage duration, or ii)constructors of namespace scope objects
i) ex.
void main()
{
A a;
static B b;
}
if exception is thrown in Destructors of A and B only A's will be called.B is staic so will not be called
ii)
namespace N {
class C {
public:
C() {
cout << "In C()" << pilocal =" const_cast">(const_int_arg);
*piLocal = iVal; //able change value of const int
piLocal is int* type.
static_cast:any standard conversion
reinterpret_cast:converison to any other type
int i;
char *cpPtr = "Hello World";
i = reinterpret_cast (cpPtr);
37)
Sample s;
s=i;(overload operator() to return int)
s=s+10(overload +operator accepting sample parameter and provide one argument constructor)
38)
const function prevents the modification of the data members of the class within it
39)
conversion operators have the same name and return tyoe of the object to whic they are converted to
ex.operator int(){
return this->i;}
operator circle(){ //inside operator circle() is in shape class
return circle(data);} circle c;shape s;
c=s;//calls conversion operator
40)
function pointer with typedef
int fun(int,int);
typedef int (*pftr)(int,int);
void main()
{
pftr pf=fun;
}
function pointer without typedef//not that impor
void main()
{
int(*pftr)(int,int);//prototype
*pftr=fun
}
(*pftr)(10,2)

41)If a pure virtual fn in Base is not implementedin Derived,the Derived class also becomes abstract and we cannot create objects of the Derived class also
42)Unexpected throws bad_exception if the fn calling unexpected include std::bad_exception throw in the throw list
If not included Unexpectd calls its default i.e terminate
43)sequence
try=>throw=>catch=>unexpected=>terminate=>abort
..................... (MyUnexpected)=>(Myterminate)

44)Dynamic cast:
conversion between same types,conversion from base to derived if base*ptr points to derived
const_cast convert from const to ordinary
static_cast standard casting i.e int to double etc...
reinterpret cast -fully different int to char etc...
45)
The STL provides 11 containers, divided into four categories.
The sequential containers include the- vector (dynamic array), list, and deque.
The container adapters -queue, priority_queue, and stack.
The associative containers-map, multimap, set, and multiset.
The final container, the bitset, is in a class of its own.
46)

STL algorithms:
Utility-min,max,swap
Non-Modifying-find,find_if
Numeric:accumulate()
Comparison:equal,mismatch,lexicographic_compare()
operational:for_each()

47)const objects can call only const member function
48)In the diamond problem,the most derived i.e the class lowest in the list should initialize the VB class.


Extra:

1)Call to pure virtual function directly will result in runtime exception
class test
{
A()
{
fun();
}
virtual void fun()=0;
};
2)In the diamond problem the Virtual Base class value will be the one assigned by the constructor at the bottom of the diamond.
3)Base class pointers cannot be passed to functions which take Derived class pointer.

4)In templates for the ones with non type arguments like template class test
the following will work
const int t;
test since t is const int if it is int t then it will give compilation error.const can be signed or unsigned
5)
Note that in const pointers, "const" always comes after the "*". For example:

int *const p1 = q; // constant pointer to int variable
int const* p2 = q; // pointer to constant int
const int* p3 = q; // pointer to constant int
6)int can be converted to signed char,unsigned char or char
fn('A') will call int if char not available...it will not call short fn(0) will call fn(int)
7)Fn<>(2,3) will also call Fn(T t,U u)
8)An ordinary non template function is always given priority over the template instantiation function
so fin(2,3) will call Fn(Tt,Uu) over Fn(in,int)...no compilation eror
9)bitset::count gives no.of set bits
10)In function overloading ,if there is polymorphism(base ptr pointing to derived) then the matching is as follows
i)If base version has stronger match(send char and if base has double and derived has int...double>int match to char)
then base version gets called
ii)if base and derived have equal strngth then base version is called
11)ordinary fn always given priority over similar overloaded template ones
12) class X
{
int f(X&){}
}
main(){ f(10);//error will call only if f takes const X& or X and not X&}
13) w.r.to 12
int &x=1;//error
const int &x=1;//correct
14)return by reference is ok as long as the refernce does not point to a local object
15)Bufferis same as Buffer
int& f(){int a;return a;//problem)
16)A pure virtual fn should be overridden by derived class and till it is overridden the intermediate derived calsses are all abstarct classes i.e we cannot create objects of those classes(even if other functions are there)
17)In templates,
i)first instatiation is done,if exact match is there with a user defined specialisation(here int)then int is called
ii)If there is no exact match then it calls the template instantiation(even though 'a',b' can be converted to int,still
template version is called since instatiation gives char,char
Hint:do instantiation and treat is as if that function is physically there and then apply operator overloding rules.

2 Comments:

Blogger Shini said...

Good to document, your errors and your code, you are already following the best practices of Software Development :D Document, your code for future reference! :D

11:02 PM  
Blogger The Visitor said...

30)

template
T mypair::getmax ()


Confused by so many T's? There are three T's in this declaration: The first one is the template parameter. The second T refers to the type returned by the function. And the third T (the one between angle brackets) is also a requirement: It specifies that this function's template parameter is also the class template parameter


@KS: There is just one T in the code segment you've shown.

To take care of the missing T s you need to remember that in html you lose the content between unrecognised elements.

You need to use ampersandlt; T ampersandgt; to represent angle brackets < T >

To get your code right replace lessthan and greater than symbols with appropriate characters entities.

See below:
30)

template
<class>
T mypair<t>::getmax ()

10:54 PM  

Post a Comment

<< Home