Singleton vs Static class

The singleton design pattern ensures that at any point of time a class has one and only one instance which can be accessed globally. This design pattern is useful in cases where exactly one instance is needed to co-ordinate between different modules of a software. Singleton design pattern can be used as a logger class or memory/thread pool classes for which only one instance of object is needed.

(more…)
Singleton vs Static class Read More

Proxy vs Observer design pattern

The Proxy design pattern allows to define a “proxy” class which will act as a wrapper object which will be called by the client to access the product object without exposing the details. This proxy class can also be used to add extra functionality without changing the product behavior. This design pattern can be used in cases to provide additional security access to an existing product or providing an interface for remote resources. General class diagram of proxy pattern is shown below.

(more…)
Proxy vs Observer design pattern Read More

C++11: Override and Final keyword

The explicit override and final keywords in C++11 (Introduction to C++11) enhances the polymorphism capabilities of C++. These new features provide extra control over virtual functions to the programmer. In this post, we will discuss about these new features and their usage.

Override

The explicit override keyword overrides the marked virtual function in derived class as override version of base class virtual functions. This ensures that the overrides are done by the programmer intentionally which reduces the chances of unwanted mistakes by providing clarity and hence improves the code readability and maintainability.

(more…)
C++11: Override and Final keyword Read More

C++11: Uniform Initialization explained with Simple example

In C++11 (Introduction to C++11), “Uniform initialization” syntax is introduced to initialize variables which provides a consistent syntax for all types of data variables initialization. This syntax is defined for all types of objects, arrays etc. In this post we are focusing on Uniform initialization syntax with some coding examples.

Earlier there were multiple ways to initialize variables. For example: basic data type variables could be initialized using assignment operator whereas class objects could be initialized using a constructor.

In C++11, a uniform way of initializing variables is introduced which is called “Uniform Initialization” syntax.

Let’s have a look at the sample code example to understand the usage of uniform initialization.

(more…)
C++11: Uniform Initialization explained with Simple example Read More

std::bitset explained with simple example

A Bitset is a standard template library class provided by C++ which can be used to define a fixed length sequence of bits. A value of zero indicates bit is unset whereas 1 indicates bit is set.
This class provides similar functionality as array of Boolean values in more space efficient way. There are lot of useful memory functions also provided by the library which can be used by the programmers to use it directly.

Let’s have a look into std::bitset template class definition.

template <size_t N> class bitset;
Where N = length of bitset (in bits).
(more…)
std::bitset explained with simple example Read More

std::vector explained with simple example

Vector in C++ are part of Standard Library Container Classes which basically has the functionality of dynamic array which are flexible in size.
C++ STL provides a template class definition for Vectors which are very efficient, refined, and easy to use.
It also has lots of very useful member functions which can be used directly in programs for faster implementation.

Let’s have a look into std::vector class template definition.

template <class T, class Allocator = allocator<T>> class vector
where T = data type that is being stored and allocator defines the allocator functions.
(more…)
std::vector explained with simple example Read More

Difference between Copy constructors and Assignment operator

Copy constructor is a special kind of constructor which initializes an object using another object. Compiler always provides a default copy constructor but if needed it can be over-ridden by programmer to meet the requirements.

Assignment operator is basically assigning the new values to already created object. In this case object initialization doesn’t happen as it is already done.

For eg:

class Base{
};

Base b1;
Base b2 = b1; /* Copy constructor as initialization also needs to done */
Base b3;
b3 = b2; /* Assignment operator as object is already created */
(more…)
Difference between Copy constructors and Assignment operator Read More

Difference between Constructors and Destructors

Constructor

Constructors are special member functions of a class which is used to initialize the object in desired way. Constructors has the same name as that of class. There could be multiple constructors defined in a class which could be selected based on invoking conditions.

Types of constructor

  • Default constructor – Any constructor without argument is called default constructor.
  • Parameterized constructor – Any constructor in which argument can be passed is called Parameterized constructor.
  • Copy constructor – This is a special kind of constructor which uses an existing object to create a new object.

Destructor

Destructor is a special member function of a class which is used to delete the object. Destructor also has the same name as that of class prefixed with “~” sign. In any class, one could define only one destructor. Destructor doesn’t take any argument.

(more…)
Difference between Constructors and Destructors Read More