Wednesday, 30 May 2012

DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE


links to other programs:
File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++

Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction




VISIT SITE LIFEQUEST.COM

Hi friends,

Here this example program show you through the step of how to create a binary addition  program and how to create copy constructor. though both topic need to be separate but copy constructor is can feet well here and is easy to under stand so i have included it in program.

First let's see how binary addition operation can be carried out on binary numbers 0's and 1's.

Binary addition has it's rules of carry bits and one important thing to note to easily understand binary operation is , the adding operation between any combination of two numbers one and zero is 'XOR' kind of operation.
but in addition to this we have to care of carry bit.

See below this exp,


  carry        carry      carry       carry          carry        carry      carry        carry                                     
1               1             0             0              1             1              1            

0 0 1 1 0 1 0 1
0 1 1 0 0 1 1 1
-------------------------------------------------------------------------------------------
1              0              0             1              1             1              0              0

Here you can see from right side that  addition of two ones is result in zero like in XOR operation, but carry is bit taken is one , and in second step from right again same as step  one . in third step from left when addition three  zero occur , result is one and carry taken is also one and so on. that's the rules to binary addition using carry. if any carry left in last step then it is keep in carry bit, however here our last carry is zero so no any kind of need to take in mind carry bit.

Let's look through the code how it's implemented.
First download the sample program download from here:

DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE



class BINARYINTEGER
{
public:
int  bin[8];
int n;
BINARYINTEGER()
{}
void getdata();
BINARYINTEGER(BINARYINTEGER &P)
{
     int i;
     for(i=0;i<=7;i++)
     {
     bin[i] = P.bin[i];
     }
}
BINARYINTEGER operator +(BINARYINTEGER );
void display();
};

You can see here i have take one 8 bit integer array to store  bit value's.
one is the default constructor and another is the copy constructor.
COPY CONSTRUCTOR:
Copy there is not any big diff. between constructor and copy, copy constructor is used when we to create an object of class from previously created class that is by simply copying it.

Look at  below in copy constructor function's argument , here the argument passed is actually reference
of another object,then the process is simple copying value of member variable.in this program i have asked user to enter array values for only one object and then i had copy it in another newly created object using copy constructor.



BINARYINTEGER(BINARYINTEGER &P)
{
      int i;
      for(i=0;i<=7;i++)
      {
      bin[i] = P.bin[i];
      }
}

For binary addition i have overloaded here binary + operator.

BINARYINTEGER BINARYINTEGER :: operator +(BINARYINTEGER b3)
{
BINARYINTEGER b2;
int len,i,carry;
carry=0;

      for(i=7;i>=0;i--)
      {
if((bin[i]==1) && (b3.bin[i]==1) && carry==1)
{
b2.bin[i]=1;
carry=1;
}
else if((bin[i]==1) && (b3.bin[i]==1) && carry==0)
{
b2.bin[i]=0;
carry=1;
}
else if((bin[i]==1) && (b3.bin[i]==0) && carry==0)
{
b2.bin[i]=1;
carry=0;
}
else if((bin[i]==0) && (b3.bin[i]==0) && carry==0)
{
b2.bin[i]=0;
carry=0;
}
else if((bin[i]==0) && (b3.bin[i]==1) && carry==1)
{
b2.bin[i]=0;
carry=1;
}
else if((bin[i]==0) && (b3.bin[i]==0) && carry==1)
{
b2.bin[i]=1;
carry=0;
}
else if((bin[i]==1) && (b3.bin[i]==0) && carry==1)
{
b2.bin[i]=0;
carry=1;
}
else if((bin[i]==0) && (b3.bin[i]==1) && carry==0)
{
b2.bin[i]=1;
carry=0;
}


}

Here you can see that i have use one carry bit and two normal bit's in condition check ,then the processing steps is all self illustrating.

Download executable program files from here:
DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE

Below is the output snap of above program
So that is it hope you have enjoy it!

links to other programs:
File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction





VISIT SITE LIFEQUEST.COM