nullptr

Let us understand the need of nullptr with help of an example. #include <iostream>using namespace std;class base {public: void display(int num) { cout << “Integer display: “ << num << endl; } void display(char* ptr) { cout << “ptr display: “ << ptr << endl; }}; int main() { base b; b.display(NULL); return 0; } The above code will not compiled. Below error will come: “In function int main():     15:15: error: call of overloaded display(NULL) is ambiguous            b.display(NULL); 15:15: note: candidates are:  …

auto keyword

Let us understand the usage of auto keyword with the help of an example. #include <iostream>using namespace std;int main(){ auto i = 10; auto d = 7.5; cout <<  “Integer and double: ” << i << “ “ << d << endl; return 0;} Above code can be compiled using -std=c++11 flag. Here, we have not defined the type for variable i and d.  When we initialize the variable at the time of declaration, compiler can identify or deduce the type for it. Based on this, C++11 came with the…

Initializer_list

Let us understand the concept of initializer_list with the help of an example: #include <iostream>#include <vector>#include <initializer_list>using namespace std;#define MAX 10class array{public: array(std::initializer_list<unsigned int> list) { index = 0; for(auto item: list) { arr[index++] = item; } cout  << “C’tor” << endl; } void display() { for (unsigned int i = 0; i < index; ++i) { cout << arr[i] << endl; } }private: unsigned int arr[ MAX]; unsigned short index;};int main(){ array arrObj = {0,1,5,7,8}; arrObj.display(); vector <unsigned int> v = {5,8,9,10,20}; cout << “Entries in vector: ” <<…

Return Value Optimization(RVO)

Let us try to understand RVO(Return value optimization) with the help of below example: #include <iostream> using namespace std; class base { unsigned int m_mem1; unsigned int m_mem2; public: base(unsigned int x, unsigned int y): m_mem1(x), m_mem2(y) { cout << “base C’tor” << endl; } ~base() { cout << “base d’tor” << endl; } base(const base& b) { m_mem1 = b. m_mem1; m_mem2 = b. m_mem2; cout << “Copy C’tor” << endl; } static base getNewInstance() { base b(10,20); return b; } }; int main() { base b = base::getNewInstance();…

inline function

inline function is a function which is expanded inline i.e. function definition is copied to a place where it is called.When a function called in program, CPU does following: a) Stores the address of instruction following the function call. b) Push function arguments on the stack c) Transfer the control to the function definition. d) Execute the function e) Transfer the control to the address stored at step a. When execution time of function is less than the switching time from calling to called function, then this becomes overhead. To…

Friend function

Friend function is a function, which can access the private and protected data member of a class. Using keyword friend, we can make function a friend function. Below is the prototype for friend function friend <return-type>  function(<className>); Example: #include <iostream> using namespace std; class base { unsigned int m_int; public: base() { m_int = 20; } friend ostream& operator<<(ostream& s, const base& b); }; //Source File ostream& operator<<(ostream& s, const base& b) { s << b.m_int ; return s; } int main() { base b; cout << b << endl;…

const member function

const member function does not allow to modify nonstatic data member value of an object. Below is the prototype for const function: <return-value> function() const;const function with empty definition look like: <return-value> <className>::function() const {}From the above you can see that with the const keyword, we can make function a const function. const function makes this pointer as pointer to a const because of this reason, we cannot modify object data i.e. nonstatic data member of an object. const function can be called from const object as well as non-const…

Mutable storage class specifier

mutable is the one of the storage specifier in C++. In general, const function does not allow to modify data members of an object who called it. However, with the help of mutable specifier, we can achieve it.Simple example shows use of mutable specifier. #include <iostream>using namespace std;class base { unsigned int m_int; mutable unsigned int m_mutableInt;public: base(unsigned int x, unsigned int y) { m_int = x; m_mutableInt = y; } void read() const { //const function m_int = 40; // Error: This is not allowed. m_mutableInt = 10; //…

Static function

Static function is used to access static data members of a class.Static function is called using class name and scope resolution operator. We do not need an object to call static function. //Header fileclass singleton {    static singleton* m_instance;    singleton() {}    singleton& operator=(const singleton& );    singleton(const singleton& );     public:    ~singleton() {}    static singleton* getInstance(); }; //Source file singleton* singleton::m_instance = NULL;singleton* singleton::getInstance() {  //static function accessing static data member m_instance    if (NULL == m_instance) {        return new singleton;  …

Static member

When we add a static data member in class, it will have only one copy per class.Let us understand with an example.//Header file#include <iostream>using namespace std;class base {     static unsigned int m_classMember; //static data member    unsigned int m_objectMember; //nonstatic data memberpublic:    base() {         m_objectMember = 0;        ++ m_classMember;         ++ m_objectMember;       cout << “Count: “ << m_classMember  << “ “ << m_objectMember <<           endl;    }}; //Source file:unsigned int…