Features of C++ 11

Features of C++ 11

INTRODUCTION

In this tutorial, we are going to learn about the Features of  C++ 11 in an elaborate way.

C++ is an object-oriented programming language that is a middle-level language. C++ was created, designed, and developed by Bjarne Stroustrup at bell laboratories in new jersey. Earlier, C++ was named C with classes. The danish scientist wanted a flexible and dynamic language that was similar to C with all its features, including active type checking, basic inheritance, default functioning argument, classes, inlining, etc. C with classes was renamed C++ in 1983, resembling one higher level than C.

The main features that are remained as the highlights of the C++ 11 version are.

  • auto and decltype
  • nullptr
  • Override and Final
  • Strongly-typed Enums
  • Lambdas
  • Non-Member begin() and end()
  • Smart Pointers

Let’s understand each feature in detail.

auto keyword

The auto keyword specifies that the type of variable that is being declared will be automatically defined by the initializer. The main use of the auto keyword is to avoid long initializations in the case of creating iterators for the containers. In the case of functions, the auto keyword deducts the return type of the function based on the return type expression during runtime.

Let’s look at a sample program implementing the auto keyword.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    auto x = 4;
    auto y = 3.37;
    auto ptr = &x;
    set<string> st;
    st.insert({"hi","hello","bye","hi" });
    cout<<typeid(x).name()<<endl<<typeid(y).name()<<endl<<typeid(ptr).name()<<endl;
    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";
        cout<<endl;
     return 0;
}

The output of the program is.

See also  OBJECT-ORIENTED PROGRAMMING IN C++(2)

In the above program, we initialized an integer value, a double value, a pointer, and an iterator over a string. Here we used typeid(parameter).name()  function which returns the data type of the parameter. According to the sequence of initialization, the compiler returned i, d, and pi respectively for three datatypes which means i means integer, d means double and pi means a pointer to an integer respectively. And in the case of the iterator, it returned by ignoring the duplicate values.

decltype

  • decltype inspects the type of the expressions. It extracts the type from the variable. Decltype is a sort of operator that evaluates the type of the passed expression.

Let’s look at a sample program implementing decltype.  

#include <bits/stdc++.h>
using namespace std;
int fun1() { return 10; }
char fun2() { return 'h'; }
int main()
{
    decltype(fun1()) x;
    decltype(fun2()) y;
    cout << typeid(x).name() << endl;
    cout << typeid(y).name() << endl;
    return 0;
}

The output of the program is.

In the above program, fun1() returns an integer, so the return type assigned to the fun1() is i, resembling an integer. And in the case of fun2(), it returns a character, so the return type of fun2(0 is c, resembling a character.

nullptr

nullptr is a special keyword that is introduced in C++ 11. this nullptr can be sued everywhere where NULL is used. nullptr is the same as NULL as nullptr can be implicitly convertible and can be compared with any pointers. Whereas NULL can not be implicitly converted and can be compared with only integral types.

Let’s look at a sample program implementing nullptr.

#include <iostream>
using namespace std;
main() {
nullptr_t p1, p2;
if(p1 >= p2)
  cout << "pointer 1 is greater than pointer 2 value" << endl;
else
  cout << "pointer 2` is greater than pointer 1 value" << endl;
char *ch = p1;
if (ch == nullptr)
  cout << "the pointer has null value" << endl;
else
  cout << "the pointer ch has not null value" << endl;

The output of the program is.

See also  DESTRUCTORS IN C++

In the above program, we used pointer variables with the value, and we can validate the variable using nullptr.

#include <iostream>
using namespace std;
main() {
nullptr_t p1, p2;
if(p1 >= p2)
  cout << "pointer 1 is greater than pointer 2 value" << endl;
else
  cout << "pointer 2` is greater than pointer 1 value" << endl;
char *ch = p1;
if (ch == nullptr)
  cout << "the pointer has null value" << endl;
else
  cout << "the pointer ch has not null value" << endl;
}

The output of the program is.

In the above program, we used pointer variables with the value, and we can validate the variable using nullptr.

There are some more features in the c++ 11 version, we will learn them in the next article. Thank You!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh