You are on page 1of 6

//Pointers to functions

#include<iostream.h>
#include<conio.h>
typedef void (*funptr)(int, int);
void add(int i, int j)
{
cout<<i<<"+"<<j<<"="<<i+j;
}
void sub(int i, int j)
{
cout<<i<<"-"<<j<<"="<<i-j;
}
void main()
{
funptr ptr;
ptr= &add;
ptr(1,2);
cout<<endl;
ptr= &sub;
ptr(3,2);
getch();
}

OUTPUT:
1+2=3
3-2=1
//Array of pointers

#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void main()
{
int i=0;
char *ptr[10]={"books","television", "computer", "sports"};
char str[25];
clrscr();
cout<<"\n enter ur favorite leisure pursuit: ";
cin>>str;
for(i=0;i<4;i++)
{
if(!strcmp(str, *ptr[i]))
{
cout<<"\n ur favorite pursuit "<<" is available"<<endl;
break;
}
}
if(i==4)
cout<<"\n not available";
getch();
}

OUTPUT:
enter ur favorite leisure pursuit: books
ur favorite pursuit is available
//Pointers and objects

#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
float inches;
public:
void getdist()
{
cout<<"\n enter the feet";
cin>>feet;
cout<<"\n enter inches";
cin>>inches;
}
void showdist()
{
cout<<feet<<"\'"<<inches;
}
};
void main()
{
clrscr();
distance dist;
dist.getdist();
dist.showdist();
distance* distptr;
distptr = new distance;
distptr->getdist();
distptr->showdist();
getch();
}
OUTPUT:

enter the feet4

enter inches7
4'7
enter the feet6

enter inches9
6'9
//Bubble sorting using pointers

#include<iostream.h>
#include<conio.h>
void main()
{
void bsort(int*, int);
const int n=10;
int arr[n]={37,84,62,91,11,65,57,28,19,49};
bsort(arr,n);
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";getch();
}
void bsort(int* ptr,int n)
{
void order(int* ,int*);
int j,k;
for(j=0;j<n-1;j++)
for(k=k+1;k<n;k++)
order(ptr+j, ptr+k);
}
void order(int *numb1,int *numb2)
{
if(*numb1 > *numb2)
{
int temp = *numb1;
*numb1 = *numb2;
*numb2 = temp;
}
}

OUTPUT:
11 19 28 37 49 57 62 65 84 91
//Address and pointers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int var1=11;
int var2=44;
cout<<endl<<&var1;
cout<<endl<<&var2;
getch();
}

OUTPUT:

0x8fd6fff4
0x8fd6fff2

You might also like