Sunday, 21 June 2015

Methods of choosing overloading vs. default arguments in C++

Methods of choosing overloading vs. default arguments in Cpp with example:

Both function overloading and default arguments provide a convenience for calling function names. However, it can seem confusing at times to know which technique to use. For example, consider the following tool that is designed to automatically manage blocks of memory for you:

Efficient Use of Destructors to Prevent Resource Leaks with examples in C++

Efficient Use of Destructors to Prevent Resource Leaks with examples in Cpp with example

When the exception thrown by a program that is not caught, program execution immediately stops.
In C language this situation is handled by setjmp and longjmp.
The longjmp behaves differently with the C++ language. It fails to call destructors for local objects when it adjusts the stack.

How to use destructors to prevent resource leaks?

Yes, we need to learn about this clearly, to avoid memory leaks.
A reasonable approach to this task is to define an abstract base class, ANIMAL, plus concrete derived classes for Dogs and Cats. A virtual function, process property, and handles the necessary species-specific processing:

Thursday, 18 June 2015

Reading data from CSV file and writing it into the PostgreSQL database in C++

Reading data from CSV file and writing it into the PostgreSQl database in Cpp with examples:

In this we are going to learn how we are using Postgres database using the languages C/C++.

First of all we need to understand the procedure and the functions used in Postgress database creation and manipulation etc.

APIS that are used to work with tPOSTGresSql data base:

To access the database from your C/C++ program, we are going to use a library called libpq-fe. The most basic functions that we'll need in order to access the database are the following:

PQconnectdb – makes the connection to the database
PQexec – executes the required queries
PQgetvalue – get the required values

#include <postgresql/libpq-fe.h>
,we need to include the header file into the application, where we are going to use the Postgres database related functions

Declare the connection with the PostGreSql:

Template Arguments versus Template Parameters

Template Arguments versus Template Parameters

In this post I would like to clarify what is the difference between the template arguments and template parameters. Here is the sample syntax
template <Type T, int iIndex>

class ArrayInClass {


    T TempArrayObj[iIndex];


The below is the sample plain class.

Abstract factories in C++ with example

Abstract factories in Cpp with example

The Abstract Factory pattern looks like the factory objects we have seen previously, with not one but several factory methods.
Each of the factory methods creates a different kind of objects.

The idea is that at the point of creation of the factory object, you decide how all the objects created by that factory will be used.

Here is an example suppose you are creating a general-purpose gaming environment and you want to be able to support different types of games. Here is how it might look using an abstract factory:

Polymorphic factories

Polymorphic factories in Cpp with examples

The static factory( ) method in the previous example forces all the creation operations to be focused in one spot, to that’s the only place you need to change the code. This is certainly a reasonable solution, as it throws a box around the process of creating objects. However, the Design Patterns book emphasizes that the reason for the Factory Method pattern is so that different types of factories can be subclassed from the basic factory (the above design is mentioned as a special case). However, the book does not provide an example, but instead just repeats the example used for the Abstract Factory. Here is ShapeFactory1.cpp modified so the factory methods are in a separate class as virtual functions:

Multiple dispatching in C++

Multiple dispatching in Cpp with examples

AS of we know that C++ performs only single dispatching.
That is, if you are performing an operation on more than one object whose type is unknown C++ can invoke the dynamic binding mechanism on only one of those types.

The solution for the above problem is multiple dispatching.

Remember that polymorphism can occur only via member function calls, so if you want double dispatching to occur, there must be two member function calls: the first to determine the first unknown type, and the second to determine the second unknown type.

With multiple dispatching, you must have a virtual call to determine each of the types.

 Here is a working example for multiple dispatching:

Visitor Design Pattern

Visitor Design Pattern in Cpp with examples:

This pattern is a type of multiple dispatching.

It is possible to add the new polymorphic methods to that hierarchy, which means that normally you would have to add something to the base class interface.

The visitor pattern allows you to extend the interface of the primary type by creating a
Separate class hierarchy of type visitor to virtualize the operations performed upon the
Primary type.

The objects of the primary type simply “accept” the visitor, then call the visitor’s dynamically-bound member function.

The below is the example for visitor design pattern.

Global scope resolution

Global scope resolution in Cpp with example.

This post will give the clarification about the Global scope of the variables.
The scope resolution operator gets you out of situations in which the name the compiler chooses by default is not what you want.

For example, suppose you have a structure with a local identifier iVal, and you want to select a global identifier iVal from inside a member function. As usual, the compiler would default to choosing the local one. Otherwise, we so must tell it to consider the global variable.

When you want to specify a global name using scope resolution, you use the operator with nothing in front of it.

Here is an example that shows global scope resolution for both a variable and a function:

Nested structures in CPP with examples

Nested structures in CPP with examples:

Yes, it is possible to write the nested structures within another structure.

The declaration syntax is what you would expect, as you can see in the following structure.
The below program represents the pushdown stack as a simple linked list so it “never” runs out of memory: