Features of C++ 11

Features of C++ 11

Introduction

In the previous tutorial, we discussed some of the Features of C++ 11:

  • auto and decltype
  • nullptr

In this article, we are going to cover all the remaining features of c++ 11

Let’s understand each feature in detail.

override

In C++ 11, developers introduced override identifiers to keep a track of overriding errors. If the compiler comes across the override identifier in any part of the code, then it understands that this is the overridden function and checks for the exact same function in the base class. If there is no match between the base and the derived class, the compiler pops up an error. This overriding may be done with methods and variables too. The compiler checks whether the complete overriding is done between the base class and the derived class.

Let’s look at a sample program implementing override identifier

#include <iostream>
using namespace std;
class Parent {
public:
    virtual void func() { cout << "I am in base" << endl; }
};
class derived : public Parent {
public:
    void func()
    {
        cout << "I am in derived class" << endl;
    }
};
int main()
{
    Parent p;
    derived d;
    cout << "Overriding done successfully" << endl;
}

Final identifier

 the final identifier in C++ allows us to prevent the overriding of virtual functions from the derived class to the base class. In Java, the final keyword is also used along with variables in order to make sure the values are assigned only once.

See also  Stacks in Cpp

Let’s look at a sample program implementing the final identifier.

#include <iostream>
using namespace std;
class Parent {
public:
    virtual void fun() final {
        cout << "fun() in Base";
    }
};
class Child : public Parent {
    void fun() {
        cout << "fun() in Derived\n";
    }
};
int main() {
    Child c;
    Parent &p = c;
    p.fun();
    return 0;
}

As we are trying to override a virtual function with a final specifier mentioned, the compiler throws an error mentioning “overriding final function”.

Strongly-typed Enums

enum is a user-defined datatype that can be assigned with some limited values. These values are assigned by the user. The keyword used is enum.

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

#include <bits/stdc++.h>
using namespace std;
main()
{
    enum Oper { sum, sub, mul };
    Oper operation = sum;
    switch (operation) {
    case sum:
        cout << "addition operation";
        break;
    case sub:
        cout << "subtraction operation";
        break;
    case mul:
        cout << "multiplication operation";
        break;
    default:
        cout << "other operation";
    }
}

The output of the program is.

Lambdas

The lambda expression is an anonymous function that provides a brief and functional syntax, which is used to write anonymous functions. The syntax of the lambda expressions is as follows.


                          [  capture clause  ]  (  parameters  ) - > return type

                            {
                                         //definition of the method
                            }

Let’s look at a sample program implementing lambda expressions.


#include<iostream>
#include<vector>
#include<algorithm> 
using namespace std;
int main() {
   vector<int> v {1,2,3,4,5};
   for_each(v.begin(), v.end(), [](int x) {
      cout <<"square of "<<x<< " is " <<x*x <<endl;
   });
}

The output of the program is.

See also  Operator Overloading in c++

In the above program, we included the vector class which is used to create a vector, and we also included the <algorithm> header which contains the for_each loop.

In the above program, we created a vector and the for_each loop is iterated over the given vector by passing the starting and ending indices of the vector.

begin() and end()

begin() and end() are the functions used to return an iterator pointing to the first element and the last element respectively of a container.

The syntax of the begin() and end functions are.

                                                       Vectorname.begin()

                                                       Vectorname.end()

Let’s look at a sample program implementing begin() and end() functions.

#include <iostream>
#include <vector>
using namespace std;
main() {
    vector<int> myvector{ 1, 2, 3, 4, 5 };
    for (auto it = myvector.begin(); 
         it != myvector.end(); ++it)
        cout<<" "<<*it;
}

The output of the program is.

In the above program, a vector is created and by using begin() and end() functions, we iterated over the container.

Smart pointer

The smart pointer looks like a normal pointer. Unlike a normal pointer, the smart pointer can destroy the object’s memory when the object goes out of space. Smart pointers are also used in the management of resources.

CONCLUSION:

that’s it from this tutorial. We have learned about the different features of the C++11 version. Hope you guys found it interesting. Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.