Types of constructors

1. Void constructors or default constructors This has no parameters and is must in case of dynamic allocation of objects. 2. Default parameter constructor A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used. [Read More]

Use the variable closer to its use

Its good programming practice to define the variable closer to its use.
Eg. In cpp, this is better code:
for(int i = 0; i{…}

rather than
int i;


for(i=0;i

How to bound check arrays in cpp / c

Bound checking in cpp /c is headache…. char \*strcpy(char \*dest, const char \*src) { char \*save \= dest; while(\*dest++ \= \*src++); return save; } //main func char *src = “hello to c programming language”; char dest[12]; strcpy(dest,src); //calling function Here we have no bound check on dest size or src size. When we pass it to function it is perfectly alright but problem is dest is array which is just 12 bytes long…but src is larger string… [Read More]

Using == operator in better way in cpp

In cpp, it is possible that instead of
i==5

we can do

i=5

So we assign i = 5 and if it is like
if(cond)
cond gets true.

So better is
5==i
beause == is symmetric.
If someone writes by mistake is
5=i
As we get error = ‘can’t assign value to literal’.