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

C++11: Initializer Lists Explained with Simple Example

In C++11 (Introduction to C++11), Initializer lists are a major addition to the C++ language. In this post we are focusing on Initializer lists with some coding examples.

To understand Initializer lists, we need to first understand the constructors. 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.

(more…)
C++11: Initializer Lists Explained with Simple Example Read More

Python string operations: Indexing and Slicing

In Python, a string is basically an ordered sequence of characters to store text-based information. String data type in python supports lot of methods to process/manipulate the data.

In this article today, we will discuss about some of the basic operations which are supported by python string data type which are very useful to use and comes in handy while programming.

Before moving please have a look into Python String basics.

(more…)
Python string operations: Indexing and Slicing 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