Tuesday, October 16, 2012

Copy Constructor & this pointer


Copy Constructor: Firstly, what is constructor? A constructor is a method define in a class by same name as of class. The work of constructor is to initialize the class members.Constructor is defined publicaly or privately. A Constructor never return any value,so it will never have any return type. A constructor without an argument is called default constructor. A constructor with an argument is known as parameterised constructor

 example:      class example
                       {
                          int a,b;            //data member 

                      public:
                                  
                            example()           //Default Constructor 
                           {
                              a=10;
                               b=10;
                           }
                           example(int x, int y)            //parameterised Constructor
                          {
                                a=x;   
                                b=y;
                           }
                       };

this pointer: Member function of every class object have a special kind of pointer known as this pointer, which points to the object itself. When we cal a member function it comes into existence with the value of this set to the address of object it is pointing. Using a this pointer any member function can find out the the address of the object which it is pointing. Using this pointer a member function can also access the data of the object.
  example:

Operator Overloading Program


Operator Overloading provide facility to give user define meaning to operator. With the help of operator overloading one can perform operation on non-standard data type(user defined data type). For example an '+' operator can add only predefine data type i.e int,long,float,double. Suppose we want to add two user defined data type say two matrix then the '+' operator is overloaded to perform addition of two matrix. The following program illustrate the operator overloading concept.

Default Arguments Program


When we have to define a function that will work on different no. of arguments for number of time then the solution is function overloading.But in case of overloading we have to pass the agruments again and again even if we are calling the function with same value of arguments for many time.C++ resolve this problem with the help of Default Arguments.Now if the function is called one or two or all the arguments missing,the compiler takes it value from the default agruments declare at function prototype.But the missing arguments must be the trailing arguments(those at the at of argument list).Have a look at example given below.

Program To Find Sum Of Digit


Some Basic C program frequently asked in Interview.

/* Program to find sum of digit of a number*/ 


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
while(a>0)
{
b=a%10;
s=s+b;
a=a/10;
}
printf("Sum of digit is %d",s);
getch();
}

Program To Convert a Number into Binary



Some Basic C program frequently asked in Interview.

/*Program to convert a number into Binary*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
printf("\t");                  //'\t' is for tab 
while(a>0)
{
b=a%2;
printf("%d",b);
printf("\b\b");         // '\b' is for backspace
a=a/2;
}
getch();
}

Program to check a prime number.


Some Basic C program frequently asked in Interview.

/* Program to check whether a number is prime or not*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
b=2;
while(a%b!=0)
{
b++;
}
if(a==b)
{
printf("\n%d is prime number",a);
}
else printf("\n\t%d is not prime number",a);
getch();
}

Program to Calculate power of a number


Some Basic C program frequently asked in Interview.

/* Power of a Number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter a number,and its power\t");
scanf("%d%d",&a,&b);
c=1;
while(b>=1)
{
c=c*a;
b--;
}
printf("%d",c);
getch();
}