Smart Pointers Explained With Simple Example

Standard library included with C++ compilers provides some specific resource management pointers classes which are also called as Smart Pointers.

A normal (raw) pointer typically points to an object, but it doesn’t indicate who owns the objects. This leads to the confusion most of the times, that who is supposed to delete the pointer and free the memory.
Also, by looking into the pointer, one can’t guess how the pointer should be deleted.

Smart pointers are designed to overcome these shortcomings of raw pointers.

Types of Smart Pointers

  • unique_ptr -> This pointer class has the exclusive ownership.
  • shared_ptr -> This pointer class has the shared ownership.
  • weak_ptr -> This pointer class is used with shared_ptr and it doesn’t have any ownership. This pointer is typically used to break loops into circular data structures.
(more…)
Smart Pointers Explained With Simple Example Read More

Connected Graph Property Explained With Simple Example

Graph is a data structure which consists of a set of vertices which is called as Node, together with a set of collection of pair of vertices which is called as an Edge.
A graph data structure can be represented as a pair (V, E) where V is a set of nodes called vertices and E is a collection of pairs of vertices called edges. Graphs are used to solve many real-life problems such as fastest ways to go from A to B etc.

A Graph is called connected graph if each of the vertices of the graph is connected from each of the other vertices which means there is a path available from any vertex to any other vertex in the Graph.

Before going ahead have a look into Graph Basics.

(more…)
Connected Graph Property Explained With Simple Example Read More

Topological Sort Explained With Simple Example

Topological sort is a method to sort the vertices in directed acyclic graph in which each node comes before all the nodes to which it has edges going to. Topological sort is mainly used in cases where a certain node can be visited if and only if certain nodes has been visited before.

A Directed Acyclic graph or DAG is a graph which doesn’t have any cycle.

All pairs of consecutive vertices in topological sorted order are connected by edges which forms a directed Hamiltonian Path.

(more…)
Topological Sort Explained With Simple Example Read More

C++11: extern template Explained With Simple Example

In C++11 (Introduction to C++11), extern template is added to optimize the compile time and object size. Earlier C++ compiler (C++ 03) instantiate a template function or class whenever it encounters a fully defined template function or class. If the template is used in multiple files, then compiler must create multiple instances of template function or class only to later discard all the instance but one. This would result in extra compile time and increased object file size. In C++ 03 there was no way to avoid this.

In C++11, extern template feature is introduced to avoid this situation.
Format of extern template is as follows

extern template void DemoTemplateFunction <int> ();
extern template class NUM <type>;
(more…)
C++11: extern template Explained With Simple Example Read More

Early vs Late Binding using Virtual Table and VPtr

Early BindingLate Binding
Happens at compile time.Happens at run time.
Compiler has all information to invoke correct function version at compile time.Compiler doesn’t have information to identify correct function version.
Normal function calls are example of Early Binding.Virtual functions are example of Late Binding.
Functions invoked by object is example of early bindingFunctions invoked by pointers can be example of late binding.
Early binding is efficient as no cost paid at run-time for function resolution.Late Binding is slightly costly as function resolution happens at run time.
Early binding doesn’t provide flexibility of one method multiple interfaces.Late Binding provide flexibility.
Doesn’t have any virtual table or virtual pointer.It uses Virtual Table and Virtual Pointer.
(more…)
Early vs Late Binding using Virtual Table and VPtr Read More

Virtual Functions in C++ Explained With Simple Example

A virtual function is a member function which is defined in Base class and redefined by the derived class. “virtual” keyword must be added before the function declaration to define a function virtual. These functions gets inherited by Derived class from Base class and Derived class can choose to provide it’s own definition.
A pure virtual function is a virtual function which has no definition in Base class. Pure virtual function definition is used to ensure all derived class must override the base class function definition.

Normal syntax to define virtual function is as follows:

virtual int samplefun (int x, char c);
(more…)
Virtual Functions in C++ Explained With Simple Example Read More

C++ Casting Operators Explained With Simple Example

Type casting is the method to convert variable (or expression) of a type to another type. Casting operators are special operators which are used to convert variable of one data type to another data type. Type casting can be done in two ways:

Implicit casting is being done automatically where values are copied to another compatible type. Eg: int to long int or float etc.

Explicit casting is done specifically by the programmer. In C++ there are four types of casting operator available.

(more…)
C++ Casting Operators Explained With Simple Example Read More

reinterpret_cast Casting Operator Explained With Simple Example

This is one of the most complex and dangerous casting operators available in C++. The “reinterpret_cast” operator can convert any type of variable to fundamentally different type. This cast operator can convert an integer to a pointer and so on. This cast operator can also convert variable into totally incompatible type too.

Normal syntax to do reinterpret_cast is as follows:

reinterpret_cast <target-type> (expr)

target-type is the target of the cast whereas expr is being cast into the new target-type.

(more…)
reinterpret_cast Casting Operator Explained With Simple Example Read More

static_cast Casting Operator Explained With Simple Example

This is the simplest type casting operator available in C++. The “static_cast” operator performs a normal cast. This casting operator is basically a substitute for normal casting operator. “static_cast” operator doesn’t do any runtime checks. Hence programmer should consider whether casting is applicable or not.

Normal syntax to do static_cast is as follows:

static_cast <target-type> (expr)

target-type is the target of the cast whereas expr is being cast into the new target-type.

(more…)
static_cast Casting Operator Explained With Simple Example Read More

const_cast Casting Operator Explained With Simple Example

This is one of the most dangerous casting operators. The “const_cast” operator is used to remove the const or volatile property of a variable. The target-type and source-type must be of the same type here.

Normal syntax to do const_cast is as follows:

const_cast <target-type> (expr)

target-type and expr must be of same type pointer or reference.

(more…)
const_cast Casting Operator Explained With Simple Example Read More