This is second tutorial series of “introduction to c++ 11 standard with examples”. Dear reader follow previous post before continuing to read this post.

introduction to c++ 11 standard with examplesintroduction to c++ 11 standard with examples (tutorial 2)

c++ 11 nullptr

In c++ 03 standard null pointer is denoted as NULL which is integer 0. Let see below code and try to compile.

Will the above code compile? obviously no. it will give compile time error.  “error: call of overloaded ‘func(NULL)’ is ambiguous”. The error comes because NULL in c++ 03 is integer zero. calling func with NULL parameter confuse the compiler that which version of func should it infer.

c++ 11 introduces a new keyword (nullptr) for null pointer. Let us see below code (c++11 version of above code)

So nullptr is dedicated to represent null pointer in c++ 11. so when in above code we call func(nullptr) the compiler would be able to search appropriate func to call.

c++ 11 auto keyword

c++ 11 has introduced a new feature “automatic type deduction”. we can declare a variable or an object without specifying its specific type by using auto keyword. for example in c++ 03 standard if we want to declare an integer variable then we have to write declaration as int salary; where the salary is a variable of type integer.

The declaration of variable salary can be used with keyword auto like “auto salary = 10”. the type of salary is deduced from its initialization.

Another example of auto

If we have to print the contents of an stl vector in c++ 03 then we have to write following code.

But in c++ 11 we can print contents of a stl vector as follows.

Range-based for loop (C++11)

c ++11 we can use the new range-based for loop, which looks like this.