Pages

January 30, 2013

Pointers in C++ (OOPs)


syntax and working of showing how pointers points to its particular value in C++ (OOPs)

Pointer is that variable which stores the memory address of another variable. It means pointer is itself a variable which stores hex-decimal address of the location.
Suppose we assign the value 10 to any variable called num and this will store at any location in the memory then address of that location is stored by the pointer variable.

Pointer variable is declared by using format:
                  Data_type *variable;
        int  *pt;

To assign address to the pointer format:
                 pt= #
Here *pt will give the content of that location but pt will give address of that location. Similarly, pointer on floating, double and character data can be assigned and the pointer is always defined under that type to which the content is defined.

/* Program to perform all the arithmetic operations using pointers*/


#include<iostream.h>
#include<conio.h>
main()
{
      int *p,*q,a,b;
      cout<<"\n Enter any two interger value";
      cin>>a>>b;
      p=&a;
      q=&b;
      int sum=(*p)+(*q);
      int sub=(*p)-(*q);
      int mul=(*p)*(*q);
      float div= float(*p)/(*q);
      cout<<"\n Addition is "<<sum;
      cout<<"\n Subtraction is "<<sub;
      cout<<"\n Multiplication is "<<mul;
      cout<<"\n Division is "<<div;
      getch();
      }
output of program to perform arithmetic operation using pointers in C++ (OOPs)

/* Program to enter a limit and print is sum upto that limit using pointers*/

#include<iostream.h>
#include<conio.h>
main()
{
      int a[100],i,sum=0,n,*p;
      cout<<"\n How many elements you want to enter";
      cin>>n;
      cout<<"\n Enter the elements";
      for(i=1;i<=n;i++)
      {
                       cin>>a[i];
                       }
      for(i=1;i<=n;i++)
      {
                       p=&a[i];
                       sum=sum+(*p);
                       }
      cout<<"\n Sum of the series upto limit "<<n<<" is "<<sum;
      getch();
      }
output of program to enter elements and show its sum using pointers in C++ (OOPs)

/* Program to show arithmetic operations with more than one value return */

#include<iostream.h>
#include<conio.h>
int sum,sub,mul,mod;
int operation(int *p,int *q)
{
    sum=(*p)+(*q);
    sub=(*p)-(*q);
    mul=(*p)*(*q);
    mod=(*p)%(*q);
    return(*p,*q);
}
main()
{
      int a,b;
      cout<<"\n Enter any two integer value";
      cin>>a>>b;
      operation(&a,&b);
      cout<<"\n Addition is "<<sum;
      cout<<"\n Subtraction is "<<sub;
      cout<<"\n Multiplication is "<<mul;
      cout<<"\n Remainder is "<<mod;
      getch();
      }
output of program to perform arithmetic operations and returning more than one value using pointers in C++ (OOPs)
   

/* Program to show use of pointer to pointer call */

#include<iostream.h>
#include<conio.h>
main()
{
      int n,*p,**q;
      cout<<"\n Enter any value";
      cin>>n;
      p=&n;
      cout<<"\n p is "<<p;
      cout<<"\n\n *p is "<<*p;
      q=&p;
      cout<<"\n\n q is "<<*q;
      cout<<"\n\n *q is "<<**q;
      getch();
      }
output of program to show use of pointer to pointer call in C++ (OOPs)

/* Program to print student details using pointer */

#include<iostream.h>
#include<conio.h>
char nam[10],cls[10],*clas,*name;
int ag,rol,*roll,*age;
int student(int *roll,int *age,char *clas,char *name)
{
    cout<<"\n Enter students name, age, roll number and class";
    cin>>nam>>ag>>rol>>cls;
    return(*roll,*age,*clas,*name);
}
main()
{
      student(&rol,&ag,cls,nam);
      cout<<"\n R.No\tAge\tName\tClass";
      cout<<"\n "<<rol<<"\t"<<ag<<"\t"<<nam<<"\t"<<cls;
      getch();
      }
output of program to show student details using more than one value return in pointers in C++ (OOPs)

No comments:

Post a Comment