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…

this pointer

Let us understand with an example how and where this pointer is used. class employee { unsigned int m_age; unsigned int m_salary; employee(unsigned int age, unsigned int salary) { m_age = age; // same as this->m_age = age; m_salary = salary; //same as this->m_salary = salary }void update(unsigned int age, unsigned int salary) { m_age = age; // same as this->m_age = age; m_salary = salary; //same as this->m_salary = salary }}; int main() { employee emp(40, 45000); emp.update(40, 50000); return 0; } In the above example, employee object viz.…

Constructor & Destructor

Constructor :As name suggest, it construct an object. Constructor is used to initialize the data members of a class.Consider a class below: #include <iostream>#include <string> using namespace std;class employeeData { unsigned int m_salary; char* m_name;public: ~ employeeData() { //Destructor if (m_name) { delete []m_name; } } employeeData() { // default constructor cout << “employeeData default C’tor” << endl; m_salary = 0; m_name = NULL; } employeeData(unsigned int salary, char* name) { //Parameterized constructor cout << “employeeData parameterized C’tor” << endl; m_salary = salary; unsigned int len = strlen(name); m_name =…

Assignment Operator

Let us understand assignment operator with the help of an example of employeeData class. #include <iostream>#include <string>using namespace std;class employeeData { unsigned int m_salary; char* m_name;public: ~ employeeData() { if (m_name) { delete []m_name; } } employeeData() { cout << “employeeData default C’tor” << endl; m_salary = 0; m_name = NULL; } employeeData(unsigned int salary, char* name) { cout << “employeeData parameterized C’tor” << endl; m_salary = salary; unsigned int len = strlen(name); m_name = new char[len +1]; strncpy(m_name, name, len); m_name[len+1] = ‘\0’; } //Overloaded Assignment Operator employeeData& operator=(const…