In ‘C’, enough said about the Pointers, in C++
the declaration or use of pointer is 
same as it is in 'C'. We know that a 
Pointer is data type whose value refers directly to (or “points to”)
another value that already stored in the computer memory using its
address.  E. G. – int *p, p1=10;
p=&p1. Here, we discuss about void pointer.
A void pointer is a void * is a generic
pointer and it can be converted to or from a pointer to any type and  without restriction of losing of information
and any pointer can be implicitly converted to void*. However, it is good for
'C' but for 'C++' template class is a better an option than the void *.
Example : Displaying values of
Different types using void *.
#include <iostream.h>
  class
VoidPt
      {
           private:
              void
*n;
           public:
           VoidPt()
              {}
           void
Print_Int(void *num)
              {
                   n=num;
                   cout<<"\n\n\tInteger =  "<<*((int *)n);
              }
           void
Print_Float(void *num)
              {
                   n=num;
                   cout<<"\n\n\tFloat
=  "<<*((float *)n);
              }
           void Print_Char(void
*num)
              {
                   n=num;
                   cout<<"\n\n\tCharacter
is =  "<<*((char *)n);
              }
      };
  void
main()
      {
           VoidPt
VP;
           float
*num=new float ;
           int
*num1=new int;
           char
*ch=new char;
           cout<<"\n\tEnter
a no with fraction : ";
           cin>>(*num);
           cout<<"\n\tEnter
a integer no : ";
           cin>>(*num1);
           cout<<"\n\tEnter
a Character : ";
           cin>>(*ch);
           VP.Print_Float(num);
           VP.Print_Int(num1);
           VP.Print_Char(ch);
      }
Example – Addition of two Nos.
#include <iostream.h>
  class
VoidPt
      {
           private:
              void
*n,*m;
           public:
           VoidPt()
              {}
           void
add(void *num,void *num1)
              {
                   n=num;
                   m=num1;
                   cout<<"\n\n\tSum of  "<<*((float *)n)<<"
and ";
                   cout<<*((int
*)m)<<" = ";
                   cout<<*((float *)n) + *((int
*)m);
              }
      };
  void
main()
      {
           VoidPt
VP;
           float
*num=new float ;
           int
*num1=new int;
           cout<<"\n\tEnter
a no with fraction : ";
           cin>>(*num);
           cout<<"\n\tEnter a
integer no : ";
           cin>>(*num1);
           VP.add(num,num1);
      }
}
Example – Linked list using void
pointer. 
#include <iostream.h>
  class
list
      {
           private:
              list
*next;
              list
*head,*ptr;
              void
*n;
           public:
              list()
                   {
                        head=ptr=NULL;
                   }
              void
add(void *num)
                   {
                        list
*temp;
                        temp=new
list();
                        temp->n=num;
                        temp->next=NULL;
                        if(head==NULL)
                            {
                                ptr=head=temp;
                            }
                        else
                            {
                                ptr->next=temp;
                                ptr=temp;
                            }
                   }
              void
display(int flag)
                   {
                        list *temp;
                        cout<<"\n\t";
                        for(temp=head;temp!=NULL;temp=temp->next)
                            {
                                if(flag==1)
                                      {
                                                cout<<*((int
*)temp->n)<<" 
";
                                      }
                                else
if(flag==2)
                                      {
                                                cout<<*((float
*)temp->n)<<" 
";
                                      }
                                else
if(flag==3)
                                      {
                                                cout<<*((char
*)temp->n)<<" 
";
                                      }
                            }
                   }
      };
  void main()
      {
           list I,F,C;
           int *num, c=0,ch;
           char
*chr;
           float
*num1;
           while(c!=3)
              {
                   cout<<"\n\t1.
Add"<<"\n\t2.
Display"<<"\n\t3. Exit";
                   cout<<"\n\tEnter
the Choice : ";
                   cin>>c;
                   switch(c)
                        {
                            case
1:
                                cout<<"\n\t1.
Integer/ 2. Float /3.
Character";
                                cin>>ch;
                                if(ch==1)
                                      {
                                                num=new
int;
                                                cout<<"\n\tEnter
a Integer No : ";
                                                cin>>(*num);
                                                I.add(num);
                                      }
                                else
if(ch==2)
                                      {
                                                num1=new
float;
                                                cout<<"\n\tEnter
a Fractional No. : ";
                                                cin>>(*num1);
                                                F.add(num1);
                                      }
                                else
if(ch==3)
                                      {
                                                chr=new
char;
                                                cout<<"\n\tEnter
a Character : ";
                                                cin>>(*chr);
                                                C.add(chr);
                                      }
                                else
                                      {
                                                cout<<"\n\t****
Invalid Input ****";
                                      }
                                break;
                           case 2:
                                cout<<"\n\t1.
Integer / 2.Float / 3.
Character";
                                cin>>ch;
                                if(ch==1)
                                      {
                                                I.display(1);
                                      }
                                else
if(ch==2)
                                      {
                                                F.display(2);
                                      }
                                else
if(ch==3)
                                      {
                                                C.display(3);
                                      }
                                else
                                      {
                                                cout<<"\n\t****
Invalid Input ****";
                                      }
                                break;
                            case
3:
                                cout<<"\n\t     * * Quit * *";
                                break;
                        }
              }
      }
 
 
 
No comments:
Post a Comment