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.

#include<iostream>
using namespace std;

void func(int a) {
}

void func(char *name) {
}

int main() {
	func(NULL);
	return 0;
}

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)

#include<iostream>
using namespace std;

void func(int a) {
}

void func(char *name) {
	if(name == nullptr)
		cout<<"nullptr"<<endl;
}

int main() {
	func(nullptr);
	return 0;
}

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.

#include<iostream>
using namespace std;

// c++ 03 
int main() {
	int salary = 20000;
	cout <<salary <<endl;
	return 0;
}

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

#include<iostream>
using namespace std;

//c++ 11
int main() {
	auto salary = 20000;
	cout <<salary <<endl;
	return 0;
}

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.

#include<iostream>
#include<vector>
using namespace std;

vector<int> foo = { 1, 2, 3, 4, 5 };
int main() {

	vector<int>::iterator it;
	for (it = foo.begin(); it != foo.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

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

#include<iostream>
#include<vector>
using namespace std;

vector<int> foo = { 1, 2, 3, 4, 5 };
int main() {
        // the iterator type would be deduced automatically
	for (auto it = foo.begin(); it != foo.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

Range-based for loop (C++11)

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

for (auto i: abc)  
  std::cout << i << ' ';
#include<iostream>
#include<vector>
using namespace std;

vector<int> foo = { 1, 2, 3, 4, 5, 6};
int main() {

	for (auto i :foo) {
		cout << i << " ";
	}
	cout << endl;
	return 0;
}

Ref:

https://en.wikipedia.org/wiki/C%2B%2B11

https://isocpp.org/wiki/faq/cpp11



Related Contents to follow