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


Monday 26 September 2011

programms on array and strings


// Program to reverse an array
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,temp;
cout<<"Enter an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The reverse array is"<<endl;
for(i=0,j=9;i<=j;i++,j--)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}



//another way to reverse an array
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],arr1[10],i,j;
cout<<"Enter an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The reverse array is"<<endl;
for(i=0,j=9;i<10;i++,j--)
{
arr1[i]=arr[j];
}
for(i=0;i<10;i++)
{
cout<<arr1[i]<<endl;
}
getch();
}



// program to enter five names and display them
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char name[5][20];
cout<<"Enter five names"<<endl;
for(int i=0;i<5;i++)
{
gets(name[i]);
}
cout<<"Names entered are"<<endl;
for(i=0;i<5;i++)
{
puts(name[i]);
}
getch();
}







Sunday 25 September 2011

c++programs of linklist


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
struct node
{
int data;
struct node *next;
};
void addatbeg();
void addatend();
void afterany();
void display();
void delatbeg();
void delatend();
void delafter();
node * search();
 node *head;
void main()
{

 head=NULL;
clrscr();
char ans='y';
while((ans=='y')||(ans=='Y'))
{
clrscr();
cout<<"Operations on Linked list are"<<endl;
cout<<"1. Addition at beginning"<<endl;
cout<<"2. Addition at end"<<endl;
cout<<"3. Addition after any element"<<endl;
cout<<"4. Display link list"<<endl;
cout<<"5. Deletion at beginning"<<endl;
cout<<"6. Deletion from end"<<endl;
cout<<"7. Deletion after any element"<<endl;
cout<<"8. Exit from program"<<endl;

int ch;
cin>>ch;
switch(ch)
{
case 1:
addatbeg();
break;
case 2:
addatend();
break;
case 3:
afterany();
break;
case 4:
display();
break;
case 5:
delatbeg();
break;
case 6:
delatend();
break;
case 7:
delafter();
break;
case 8:
exit(0);
}
cout<<"Do you wish to return to the main menu"<<endl;
cin>>ans;
}
getch();
}

void addatbeg()
{
node *ptr;
ptr=new node;
cout<<"Enter the element"<<endl;
cin>>ptr->data;
if(head==NULL)
{
ptr->next=NULL;
}
else
{
ptr->next=head;
}
head=ptr;
}
void display()
{
node *ptr;
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->next;
}
cout<<endl;
}

void addatend()
{
node *ptr,*loc;
ptr=new node;
cout<<"Enter the element"<<endl;
cin>>ptr->data;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
}
else
{
loc=head;
while(loc->next!=NULL)
{
loc=loc->next;
}
loc->next=ptr;
}
}
node * search()
{
int num;
cout<<"Enter an element to search"<<endl;
cin>>num;
node *loc;
loc=head;
while(loc->next!=NULL)
{
   if(loc->data==num)
   {
return loc;
break;
   }
loc=loc->next;
}
}
void afterany()
{
node *ptr,*loc;
ptr=new node;
loc=search();
cout<<"Enter the element to be inserted"<<endl;
cin>>ptr->data;
 ptr->next=loc->next;
 loc->next=ptr;
}

void delatbeg()
{
node *ptr;
if(head==NULL)
{
cout<<"Linklist is empty"<<endl;
textcolor(134);
cprintf("Press any key to exit...............");
getch();
exit(0);
}
else
{
ptr=head;
head=head->next;
delete(ptr);
}
}
void delatend()
{
node *ptr,*loc;
ptr=head;
while(ptr->next!=NULL)
{
   loc=ptr;
   ptr=ptr->next;
}
  loc->next=NULL;
  delete(ptr);
}


void delafter()
{
node *loc,*ptr;
loc=search();
if(loc->next==NULL)
{
cout<<"It is the last element so can't perform operation"<<endl;
}
else
{
ptr=loc->next;
loc->next=ptr->next;
delete(ptr);
}
}