Thursday, January 10, 2013

Void Pointer – C++.



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;
                        }
              }
      }

Thursday, January 3, 2013

Constructor & Destructor – C++.



A constructor is a special member function which is used to initialize the object of its class. Constructors are similar to methods, but they are not methods. Like a method, a constructor has a set of parameters and a body of code. Unlike methods, however, constructors have no return type. In C++, there are  types of constructors, Default constructors can be called with no arguments, parameterize constructors can have argument and Copy constructor

Default constructor -  A default does not have any parameters. It is not necessary to create a default constructor, it is automatically created by the compiler. The default constructor initializes all instance variables to default value. 

Parameterize Constructor - The constructor that can take arguments are called parameterized constructors.

Copy Constructor - A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type.

Destructor : Destructor functions are the inverse of constructor functions. Destructors are called when objects are need to be destroyed.


Example : Default Constructor

#include <iostream.h>

#include<iostream.h>
  class D_Constructor
      {
           private:
              int num;
           public:
           //Default Constructor of the class D_Constructor
              D_Constructor()
                   {
                        num=5;
                   }
              void Disp()
                   {
                        cout<<"\nNumber : "<<num;
                   }
      };
  void main()
      {
           //Crerating object and Invoking default constructor
           D_Constructor DC;
           DC.Disp();
      }


Example – Parameterize Constructor

#include <iostream.h>

  class P_Constructor
      {
           private:
              int num_1,num_2;
           public:
              P_Constructor() //Default Constructor
                   {
                        num_1=2;
                        num_2=5;
                   }
              //Parameterise Constructor -  with one parameter
              P_Constructor(int x)
                   {
                        num_1=x;
                        num_2=15;
                   }
              //Parameterise Constructor -  with two parameter
              P_Constructor(int x, int y)
                   {
                        num_1=x;
                        num_2=y;
                   }
              void sum()
                   {
                        cout<<"\nSum of "<<num_1<<" and "<<num_2;
                        cout<<" is = "<<num_1+num_2;
                   }
};

  void main( )
      {
           // object for default constructor
           P_Constructor ob_1;
           // object for parameterise constructor with one parameter
           P_Constructor ob_2(4);
           // object for parameterise constructor with two parameter
           P_Constructor ob_3(4,8);
           ob_1.sum();
           ob_2.sum();
           ob_3.sum();
      }


Example – Copy Constructor

#include <iostream.h>
  class Person
      {
           private:
              int age;
           public:
              Person (int x): age (x)
                   {}
              Person (const Person& copy): age (copy.age) //Copy Constructor
                   {
                   }
              void disp()
                   {
                        cout<<"\nAge : "<<age;
                   }
      };
  void main()
      {
           Person P_1(25);
           Person P_2 (35);
           P_1.disp();
           P_2.disp();
           Person Ob = P_1; //call the Default Copy Constructor
           Ob.disp();
           Ob=P_2;
           Ob.disp();
      }



Example – Destructor



#include <iostream.h>
#include <string.h>

  class Destructor_Ex
      {
           private:
              char *str;
              int n;
           public:
              // A Parameterize Constructor
              Destructor_Ex(const char* S, int n1)
                   {
                        n=n1;
                        str=new char(n+1);
                        strcpy(str,S);
                   }
              // Destructor
               ~Destructor_Ex()
                   {
                        delete[] str;
                   }
              void Disp()
                   {
                        cout<<"\nString is = "<<str;
                   }
      };


  void main ()
      {
           Destructor_Ex ob = Destructor_Ex("This is Demo", 20);
           ob.Disp();
           // Destructor is called & control back to main()
      }