Single Responsibility Principle explained with simple example

Single Responsibility Principle (SRP) is a software design principle which states that every module, class or function of a software should have one and only one responsiblity/functionality to perform. As stated by Robert C.Martin, “A class should have one and only one reason to change“.

Before going ahead we should know why do we need software design principle and what is software design principle.

(more…)
Single Responsibility Principle explained with simple example Read More

Software design principle explained with simple example

Software design principle consists a set of guidelines invented by Robert C.Martin to avoid bad software design. These guidelines consists of five design principles for object oriented software development. These design principles are:

Together these are called SOLID design principles.

(more…)
Software design principle explained with simple example Read More

Why do we need Software Design Principles

Object oriented software development often becomes victim of non-maintainable, unreliable etc in long run in most of cases. Lot of people might put bad coding skills behind that state (which is true sometime) but bad software design is the actual culprit which will make excellent coders write bad code because software design doesn’t allow them to do so.


Always remember knowing what not to do is more important than what to do !!!

(more…)
Why do we need Software Design Principles Read More

Call by value and Call by reference explained with simple example

This is one of very basic concept of C/C++ programming language which is kind of building block of any program or software. Often unknowingly programmers make mistakes in these concept and introduce bugs in softwares 🙂

Basically in C, there are two ways to pass data to a function:

1) Call by value: In this case only value will be passed to the function and the function will store this value in its own local variable. This function will work on this copied data (not original) and set some new value based on some operations. But since this function is working on copied data and not on original data, hence it can’t update the original data in this method.

2) Call by reference: In this case address of the data is passed to the function and function will define a pointer to access this memory location. Since, this function has direct access to the original data memory location, hence it can update the value of original data. Thus, this method is called Call/Pass by reference as we are passing reference of the memory instead of direct data.

(more…)

Call by value and Call by reference explained with simple example Read More

Difference between constexpr vs inline functions

constexpr functions are implicitly inline functions only. However, inline functions are simply removes the overhead of function calls by pasting the function body at the called place. But, these code will be executed at run time. Hence, performance gain will be because of reduced function calls.

constexpr functions are evaluated at the compile time to ensure no computation at runtime and hence gives better performance optimization.

Any member functions can be made inline. However, only those functions whose return types and argument types are all of literal types.

For more information related to constexpr click here.

Difference between constexpr vs inline functions Read More

Difference between constexpr vs const

constexprconst
If a member function is declared as constexpr then it will be treated as const as well.If a member function is declared as const then it will not be treated as constexpr.
If a variable is declared as constexpr then it will be treated as const as well.If a variable is declared as const then it will not be treated as constexpr.
constexpr is mainly used for optimization purpose where computation can be done at compile level to optimize run time performance.However const is mainly used to ensure that there are no accidental changes done by the member method to member data.

For more information related to constexpr click here.

Difference between constexpr vs const Read More

constexpr explained with simple example

In C++11(Introduction to C++11), constexpr is added which improves the run-time performance of the code as it allows the compiler to compute the associated code at compile time if possible. constexpr specifier can be used with both variables and functions where only constant expressions are used which can be evaluated at compile time.

Few important points:

  1. A constexpr function return type and argument type must be literal types.
  2. A constexpr function cannot be virtual but it can be recursive.
  3. A constexpr function cannot have uninitialized variables. Till C++11 it can have only one return statement while from C++14 it can have more than one return statement.
  4. Any variable can be declared as constexpr, if it is initialized. In case of constructor initialization, the constructor also must be declared as constexpr.
  5. constexpr can also be used with references if the referenced object is initialized.
  6. Any constexpr function can only call other constexpr functions.
  7. constexpr function can also be used with non-const variables. No need to define two different functions for same.

(more…)

constexpr explained with simple example Read More

Move Assignment Operator explained with simple example

In C++11 (Introduction to C++11) , move assignment operator are added which can improve any code’s performance drastically. Typically a move assignment operator is same as copy/move constructor but before reusing the memory blocks from passed object, it releases all the memory which is owned by this object. Since, memory reallocation is avoided in case of move assignment operators. Thus it gives better performance.

However while implementing move assignment operators we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move assignment.

(more…)

Move Assignment Operator explained with simple example Read More

std::move explained with simple example

std::move is a C++11(Introduction to C++11) utility function which is used to convert an lvalue to rvalue expression to forcefully call the move constructor or move assignment operator which will improve the program’s performance drastically. This utility function is only change lvalue into rvalue expression that’s it. This function doesn’t do any kind of memory movement.

Implementation of this function is as follows:

From C++11 onwards:

template< class T >
typename std::remove_reference<T>::type&& move( T&& t );

From C++14 onwards:

template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );

Let’s take an example of usage of this move function:

(more…)
std::move explained with simple example Read More

Move Constructors explained with simple example


In C++11 (Introduction to C++11) , move constructors are added which can improve any code’s performance drastically. Typically a move constructor is same as copy constructor which will create a new instance based on the passed object. But, the idea behind move constructor is to avoid memory reallocation and use as many memory from the passed original object because the original object is about to be deleted as it has been provided as a temporary object.

Since, in move constructors we avoid memory reallocation while creating new instance while will improve the code’s performance as we know memory allocation is quite costly.
While implementing move constructors we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move constructor.

(more…)

Move Constructors explained with simple example Read More