Wednesday, 28 January 2015

Function pointers with overloaded function in C++ with example

Function pointers with overloaded function in C++ with example:

The use for function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

The overloaded functions are perform in the following seven contexts.

1) The initializer in a declaration of an object or reference
2) It is on the right-hand-side of an assignment expression
3) It is like a function call argument
4) as a user-defined operator argument
5) the return statement
6) explicit cast or static cast argument
7) non-type template argument

In each context, the name of an overloaded function may be preceded by address-of operator & and may be enclosed in a redundant set of parentheses.

Tuesday, 27 January 2015

Reversing the Linked List in C/C++ with examples

Reversing the Linked List in C/Cpp with example:

There are two ways to reverse the linked list:

1. We need to know more about the pointers or we have more knowledge on pointers.

2. Swapping starts from the first node’s object and the first node’s object is swapped with the last node’s object. Then, the second node’s object is swapped with the one before the last node’s object.

Monday, 26 January 2015

Reading the XML file in C++ with example

Reading the XML file in Cpp with example:

What Is XML?

As we now that XML is merely a syntax for describing data. Outside of a specific application, XML data has no meaning.
XML documents form a tree structure that starts at "the root" and branches to "the leaves".

Simple XML file example is
<?xml version="1.0" encoding="UTF-8"?>

<Address>

    <name>dhruthi</name>

     <plotno>G16/A, 306, Swarna Complex</ plotno >

     <Street>Near M.G.N school</Street >

     <District>Rangareddy</District>

</ Address >


Friday, 23 January 2015

Factory design pattern in C++ with example

Factory design pattern in C++ with example:

What is the factory pattern?

Let us see overview of the factory pattern:

Factory in real life constructs tangible objects, such as tables, cars and languages.
Like that, a factory in object-oriented programming constructs objects. When you use factories in your program, portions of code that want to create a particular object ask the factory for an instance of the object instead of calling the object constructor themselves.

For example, an interior decorating program might have a CarFactory object.

When part of the code needs a type of car such as a innova, it would call the buyCar() method of the CarFactory object, which would return a new car.

At first glance, factories seem to lead to complicated designs without clear benefits.

It appears that you’re only adding another layer of complexity to the program. Instead of calling buyCar () on a CarFactory, you could simply buy a new car object directly.

However, factories can actually be quite useful. Instead of creating various objects all over the program, you centralize the object creation for a particular domain. This localization is often a better model of real-world creation of objects.

Thursday, 22 January 2015

Singleton design pattern in C++ With example

Singleton design pattern in Cpp with example:

We can use single object of the class throughout the lifetime of an application using singleton design pattern.

To get a single object is by declaring a class, which contains only static methods. Then the static class is loaded into the memory when the program execution starts and still available until the application ends. If there is situation that we need only one instance of a class in a truly object oriented fashion by adhering to the basic principles of object oriented programming, the Singleton patterns are used.

The Singletons are often used to control access to resources such as database connections or sockets.

Suppose we have a license for only one connection for our database. A Singleton connection object makes sure that only one connection can be made at any time.

Tuesday, 20 January 2015

Merging of two files in C/C++ with examples

Merging of two files in C/Cpp with examples:

To copy the data from one file store it into the another file is called as merging of two files.

These are the steps that we need to follow while merging of two files.
1. Open the files1 and file2 in the read mode
2. Read the character by character and store it into another file.

Let us see the below example code and run it.

File Operations in C/C++ with examples

File Operations in C/Cpp with examples:

A file that represents the sequence of bytes that are stored on the disk, where the group of related data is stored.

A file is used for permanent storage of the data. In C we have some predefined file operations.
The list is shown below


Function
description
fopen()
Tis function creates a new file or open a existing file
fclose()
This function is used to close a file
getc()
This function is used to read a character from a file
putc()
This function is used to write a character to a file
fscanf()
This function is used to read a set of data from a file
fprintf()
This function is used to write a set of data to a file
getw()
This function is used to read a integer from a file
putw()
This function is used to write a integer to a file
fseek()
This function is used to set the position to desire point
ftell()
This function is used to give the current position in the file
rewind()
This function is used to set the position to the beginning point

For fopen function we have different types of mode available to open the file, Please see the below list

Implementation of the Binary tree in C++ working code and tests

Implementation of the Binary tree in Cpp working code and tests:

From the previous posts we have seen the different possible problems and the solutions related to the binary trees.

Some of those are
Creating the binary tree [Creating the binary tree and find the size in C++ with examples]
Binary tree Traversals (in-order, pre-order & post- order)[ Binary Search tree in C++(BST) with examples]
Finding the size of the existing binary tree [Creating the binary tree and find the size in C++ with examples]
Inserting the data into the binary tree [Explain inserting the data into the Binary Tree in C++ with code examples ]
Finding maximum depth [Finding the maximum and minimum depth of the binary trees in C++ with examples]
Finding the minimum depth [Finding the maximum and minimum depth of the binary trees in C++ with examples]
Finding the total sum of the path of the Binary Tree [Finding the sum of the path of Binary tree in C++ with examples]
Finding the mirror of the tree [Finding the mirror of the binary tree with the solution in C++ with example]
Doubling the tree [Double the binary tree with the example in C++] etc.

Now, we can find the working code of the binary search tree.

Monday, 19 January 2015

Double the binary tree with the example in C++

Double the binary tree with the example in Cpp:

For each node in a binary search tree, create a new duplicate node, and insert the duplicate as the left child of the original node.
The resulting tree should still be a binary search tree.

See the below sample tree
Sample tree

 After doubling the tree the result is

Double the tree

Finding the mirror of the binary tree with the solution in C++ with example


Finding the mirror of the binary tree with the solution in Cpp and explain with examples:


The mirror of the binary tree means change a tree so that the roles of the left and right pointers are swapped at every node. Let us consider the sample example tree.

             14

             / \

            12  15

           / \

         11  13  


is changed to...
                              14

                             / \

                            15  12

                           / \

                          13   11



Sunday, 18 January 2015

Finding the sum of the path of Binary tree in C++ with examples

Finding the sum of the path of Binary tree in Cpp with examples

Let us create a tree as shown below that is a sequence of nodes in the tree starting with the root node to leaf, as we know that leaf node means that node that has no children. And also the empty tree contains no leafs, So there is no possibility of the root-to-leaf paths.

See the below example tree that contains four root-to-leaf paths.

You can see the previous post to insert the data into the binary tree [Explain inserting the data into the Binary Tree in C++ with code examples]
      6

    /    \

  4    12

  /    /    \

10  13  4

/  \     \

8 2    12



The possible Root-to-leaf paths shown below

Friday, 16 January 2015

Finding the maximum and minimum depth of the binary trees in C++ with examples

Finding the maximum and minimum depth of the binary trees in Cpp with examples:

As we have seen before post we have bit knowledge about creating, inserting and getting size of the binary trees.

GetMaximumDepth()
Given a binary tree, compute its "GetMaximumDepth" -- the number of nodes along the longest path from the root node down to the farthest leaf node.

Syntax of the function is

int GetMaximumDepth(struct node* node)


Here we are passing root node as an argument. It returns the maximum depth.

GetMinimumDepth()

Given a non-empty binary search tree, return the minimum data value found in that tree.
Note that it is not necessary to search the entire tree.

Note:This can be solved with recursion or with a simple while loop.

Thursday, 15 January 2015

Explain inserting the data into the Binary Tree in C++ with code examples

How to Insert the data into the Binary Tree in Cpp:

Here we are going to learn how to insert the new node into the binary tree.
I hope that you were remembered the previous post about the binary trees and the syntax of the binary tree. What is the binary tree? What is the structure of the binary tree and so on.

You can find more information on Binary Trees here.

InsertNewNode() – This function helps us to understand how to insert a new node with the given number into the tree in the in the correct place. The InsertNewNode() code modifies the tree structure.

The InsertNewNode() returns the new tree pointer to use to its caller. Calling InsertNewNode() with the number 4 on this tree.


Newly Inserted Node

Creating the binary tree and find the size in C++ with examples

Creating the binary tree and find the size in Cpp:

First, we will look into the solution to create the binary tree:

This is a very basic problem with a little pointer manipulation. Write code that builds the following little 4-1-3 binary search tree.

4-1-3 binary tree view


Monday, 12 January 2015

Binary Search tree in C++(BST) with examples:

Binary Search tree in Cpp(BST) with examples:

A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element.

The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees”.

Empty tree: A null pointer represents a binary tree with no elements -- the empty tree.
The formal recursive definition is: a binary tree is either empty, or is made of a single node,

Where the left and right pointers (recursive definition ahead) each point to a binary tree.
They differentiate only with one feature that is the organization of the data.

Here is the small example that helps us to understand the binary tree
A binary tree is a tree that is limited such that each node has only two children.

Sunday, 11 January 2015

Comparison of a float with another value in C

Comparison of a float with another value in C:

Find out the output of the below program
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
    float fVal = 1.2;
    if (fVal == 1.2)
        printf("I am in the IF condition");
    else if (fVal == 1.2f)
        printf("I am in the ELSE IF condition");
    else
       printf("iam in the ELSE condition");
  return 0;
}


Friday, 9 January 2015

Pointers in C++ with examples

Pointers in Cpp with examples:

Pointers are very easy in C++. If we understand correctly we can play with the pointers, otherwise they will play with us. As a C++ developer, we have to know about the pointers.
When we define a variable, the memory allocated for that and that memory location have its address. That address can be accessed using & operator, which defines the address in the memory location.
 Let us look into the following program, which will print the address of the variables defined:
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
   int  iVal1;
   char iVal2[10];
   cout << "Address of iVal1 is: ";
   cout << &iVal1 << endl;
   cout << "Address of iVal2 is: ";
   cout << &iVal2 << endl;
   return 0;
}