Wednesday 12 October 2011

a program for constructor and destructor

#include<iostream.h>
class abc
{
int x;
public:
abc()  //default constructor
{
  x=10;
}
abc(int a)// parameterised constructor
{
x=a;
}
abc (abc &A)  // copy constructor
{
x=A.x;
}
~abc()    // destructor
{
cout<<"destructor called"<<endl;
}
void accept()
{
cout<<"Enter a number"<<endl;
cin>>x;
}
void disp()
{
cout<<"the value of x is "<<x<<endl;
}
};
void main()
{
 abc a;
 a.disp();                // value is 10
 abc a1(1000);        // implicit call
       // explicit call abc a1=abc(1000);
 a1.disp();
 // when we r declaring 'a' as object the default constructor is called and when
 // we r declarin a1 as object parameteried constructor is called
 abc a2(a1);         // implicit call
       // explicit call abc a2=abc(a1);
a2.disp();
 }
i will surely explain u this program later

Monday 3 October 2011

use of random function




random() function is used to generate a number between 0 and n-1 if we pass a number n as a parameter to it. its header file is stdlib.h
for ex
int x = random(10) will generate any number between 0 and 9
To generate a number in a range we can use the following formula
random(UL-LL+1)+ LL
where UL = Upper Limit
          LL = Lower Limit
eg to generate a number between 100 and 500
random(401)+100
 random function is very important for board exams as questions are being asked from it