Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

20120728

Is it necessary to accept an object by reference in copy constructor?

Is it necessary to accept a reference in copy constructor?
Yes, It is necessary to accept an object by reference in copy constructor because if an object is accepted by value then the copy constructor will fall in recursive loop.
C++ program for copy constructor:

#include<iostream>
using namespace std;
class copyDemo
{
int x;
public:
       copyDemo(copyDemo v) //taking object as value
        {
         x=v.x;
        }
};

int main()
{
copyDemo c;
copyDemo c1(c);// this will invoke copy constructor
return 0;
}

//explanation

The statement copyDemo c1(c);  causes the copy constructor to get called.
The value of c is passed If the copy constructor collects c in v by value then
the statement  copyDemo v would become as
copyDemo v=c;
here v is getting created and initialized so again copy constructor will get called and will go in recursive call loop.
Previous                             Home                               Next

Multiplication of two numbers using BITWISE operators

//How will you multiply two numbers using BITWISE operators
#include<stdio.h>
main()
{
   int a,b,result;
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
      if (b&01)               // Logical ANDing of the value of b with 01
      result=result+a; // Update the result with the new value of a.
      a<<=1;              // Left shifting the value contained in 'a' by 1.
      b>>=1;             // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}

//program to set and show bit at nth position

20120727

New Features in ANSI C++ Standard

How to Use New Features in ANSI C++ Standard.

1)      Newly added  data types in ANSI C++:
a)bool  data type
b)wchar_t data types
c) wide_character Literals
a)bool  data type:
bool  bb; //declarion of bool data type
bb=true;  //assignment of true value
bool  bb2 = false; //declaration and initialization(to true) simultaneously

b)wchar_t data types:
To represent the character sets of the languages that contains more than 255 characters.e.g. Japanese Language wchar_t data type is used that hold 16-bit wide characters. 
c) wide_character:
                                Wide character Literals begin  with the letter  L . It has 2 byte memory.
                                e.g.    L  ‘ms’ //example of wide character Literal.
         2)      New Operators:
A)     Static_cast operator
B)      Const_castoperator
C)      Reinterpret_cast operator
D)     Dynamic_cast operator
E)      Typeid operator

       3)      Namespace
       4)      Some old style header files are renamed.for example:
           Math.h file renamed as cmath in the new standard
Previous                             Home                               Next

20120718

Bitwise operation for division

// Bitwise operation for division

int main(void) {
int L, leftShift, i, changeMask;

leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;

for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}

printf("%i", i);

return 0;
}

Previous                             Home                               Next

20120123

Why destructor should be virtual in C++ ?

Why destructor should be virtual in C++ ?
Memory Leak ?

When delete is called virtual destructor makes sure to release memory of first derived class object then of by base class object. If destructor is not virtual then only the base class object's memory get released and not that of derived class object.
example:
#include<iostream>
using namespace std;
class base
{
  base(){};
 ~base(){};
};
class derived:public
{
  derived(){};
 ~derived(){};
};
int main()
{
base *ptr= new derived(); //exp1
delete ptr; //exp2
}
Explanation:
Line exp1 makes the derived and base constructoes to be called to allocate memory for base as well derived class objects.
But, if destructor is not virtual in base class then
Line exp2 makes to call the base destructor and derived destructor never gets called and hence memory allocated to derived class object is not release and leads to memory leak because memory used by derived class object can not be used util the end of the program.
Simples Way to learn Data Structure in C++ CPP or C
C++ Program for Circular Queue implementation using linked list
 
C++ program for Circular Queue implementation using Array

C++ program for reversing single Linked list nodes 

C++ Program for sorted single linked list with class

C++ Program for BST-Binary Search Tree   


Previous                             Home                               Next