* New post Constructor Destructor and Inheritance& Array , Virtual base class in c++ language

Wednesday, 16 April 2014

0 Array in c++

Array in  c++ 

Array
An array is a group of data items of same type that share a common name . Normally ordinary variable stores one value at a time . If we want to store  more than one value at a time in a single variable , then we use arrays . Also we can say that an array is an ordered set of data items of similar type . All the element of array are stored in the memory in consecutive form . Since the array uses subscripts , therefore it is also known as subscripts variable .
An array variable can be declared as :-
           Syntax : data _ type variable _name [size] ;
             
             Ex: int x [10];
                    In the above declaration x is a array variable of 10 elements these element will store  in the memory in consecutive form as
       













   x [0]          x[1]         x[2]           x[3]         x[4]            x[5]         x[6]           x[7]          x[8]          x[9]


This array variable can be initialized as :
                                           Int x[10] = { 2 ,3,4,5,6,7,88,98,79,65}

Array of objects
When a variable of type class is used as array variable , then the type of variable is called array of objects . Also we can say that when an array variable contains the class type element then it is called array of objects . The syntax for array of object is written as : class_name object_name [size] ;
Ex : Max  m1 [3] ;
// Program to demonstrate the use of array of object
# include <iostream.h>
          #include <conio.h>
          Class student 
          {
              char Name [30] ;
              float age ;
              Public :
                       Void getdata ( );
                       Void putdata ( );
          };
            Void student : : getdata ( )
             {
                cout << "enter student name : " ;
                cin >>n ;
                cout << "enter  age of student :" ;
                cin >> age ;
              }
               
               Void student : : putdata ( )

             {
                cout << "name of student ="<<n<<"\n\n " ;
                cout << "  age of student ="<<age <<"\n\n" ;
              }
                Void main ( )
                {
                  int i ;
                  clrscr  ( );
                  student s [3] ;
                  cout << "============\n\n";
                  for ( i =0 ;i<3 ; i++)
                  {
                    cout <<"enter records of student :"<<:i+1"<<\n\n";
                     s[i].getdata( ) ;
                 }
                cout "================\n\n output \n\n";
                for ( i =0 ; i<3 ;i++)
                {
                   cout << " student :"<< i+1<<"\n\n";
                   s[i] .putdata  ( );
                }
                  getch ( ) ;
            } 
Pointer to object
Similarly to basic datatype , we can also create a pointer variable of an object . This pointer variable of type class can points to another object. This pointer variable is called pointer to object. 
Declaration of an object pointer : The pointer of an object can be  declared as an ordinary variable . The declaration of object pointer uses class name , asterisk * symbol and the pointer variable name of class . The syntax to declare an object pointer is written as : class _name * object _name ;
 The object pointer is also used to access the class member function using the arrow operator ( ->) .
Program to calculate binary of a decimal number of pointer to object ?
 Class A
        {
          Public : int n ;
                     Void getdata (  );
                     Void dectobin (  );
        };
        Void A : : getdata(  )
        {
           cout << "enter any decimal number :";                    // www.programmingking.in
           cin >> n ;
        }
        Void A : :  dectobin (  )
         {
             int b =1 , s =0 , rem ,k ;
             k =n ;
             while (n >0)
             {
               rem = n%2;
                n = n/2;
                s = s + rem * b ;
                b = b *10;
            }
            cout <<" binary of "<<k<<"="<<s<<end l;
     }
       Void main ( )
       {
          clrscr ( );
          A a1;
          A *p;
          p = & a1;
          cout << "=====================\n\n";
          p -> getdata ( );
          p -> dectobin ( );
          cout <<"================\n\n";
           getch ( );
        }
The this pointer : The " this " pointer is a special type of pointer variable , it is used to stores the address of the object.
          Uses of this pointer : this pointer is used to represent an object when it calls a member function. 'this' is a keyword  that points to the object if we print the address of object in memory location .Therefore e can find out the address of the object by calling the this pointer in the member function of the class.
    ex :
     class add
            {
      Private : 
                            int x ;
                Public :
                           Void print ( )
                            {
                              cout << this <<"\n\n";
                             }
               };
                 Void main ( )
                 {
                    add a1 , a2 ;
                    clrscr ( );
                    cout << "============\n\n";
                    cout << " address of  object a1 = "; a1.prints ( );
                    cout << " address of  object a2 = "; a2.prints ( );
                    cout << "===========\n\n";
                    getch ( );
                  } 
Accessing data member with 'this' pointer : The this pointer can also be used to access the data members of the class. When a member function is invoked then the this pointer is passed to the member function automatically as a built in argument.
# include <iostream .h>
         # include <conio.h>
         Class A 
        {
           int x ,y ;
           Public :
                     Void display ( )
                      {
                         this -> x =20;
                         this -> y = 40 ;
                         cout << this ->x <<"\n\n";
                         cout << this ->y <<"\n\n";
                       }
            };
         Void main ( )
         {
            clrscr ( ) ;
            A a1 ;
            a1.display ( );
             getch ( ) ;
         }  
Pointer to derived type : A pointer variable is used store the address of memory location , i.e it points to the objects of both base class as well as derived class . For ex : when we inherits in derived class . Then the derived members of derived class can access by the pointer to base class , but the original data and function of derived class are not accessible by the base pointer. To solve this pointer we should declare pointer to derived class. By derived pointer the data and functions of derived class can be invoked.

          Reference parameter : When a variable is passed to any function with address operator ( &) then the variable is called reference parameter of function .

 for ex : consider x & y are the simple variable , and if we pass variable to function  as reference parameter to a function then the statement will be written as sum ( int &x , int &y) ;
By using reference parameters any function can access the actual variables in the program. Also the function can return more then one value at the place of calling .
Passing reference to objects : The pass by reference method is beneficial than pass by value . Since it reduces the time of execution and saves memory space . When we pass the reference to any object then the object works directly on the actual arguments . Therefore  there is no need to copy the value of parameters .

# include <iostream.h>
          #include <conio.h>

       
          Class king 

           {
             public :
                       int h ,m ;
                      Void gettime (int i , int j )
                      {
                         h = i ; m =j ;
                      }
                  Void display ( )
                 {
                  cout << h << "hours and << m<< minutes <<"\n\n";
                 }
               Void sum ( king k1 , king k2 )
              {
                 m =k1.m + k2 .m ;
                 h = m/60 ;
                 m = m%60 ;
                 h = s1.h + s2.h ;
            }
       };
           Void main ( )                                             // www.programmingking.in
           {
            king p ,q , r ;
            clrscr ( );
           p.gettime ( 6 , 36);
           q.gettime ( 5 , 41 );
           r .sum (p ,q );
           cout << "=================\n\n";
           p.display ( );
           q.display ( );
           cout << "================\n\n";
           r .display ( ) ;
           cout <<"================\n\n";
           getch( );
         } 
  Returning reference : A function can return a reference . This function can be declared as _ data_type & function _name (argument list );
         for ex : if max is a function and if return a reference of int type data then this function will be declared as  : int & max ( );
 Using such type of function a large amount of data can be return by reference . There is no need to copy the data before returning . consider the example : 
          Class A 
          {
            public :
                     int x ,y ;
                     int &m ( ) 
                    {
                      return x ;
                    }
                     int & n ( )
                    {
                      return y ;
                    }
           };
             void main ( ) 
            {
              A a1;
              clrscr ( );
              a1 .m ( ) =100 ;
              a1 .n ( ) =200 ;
              cout << "output \n===============\n\n";
              cout <<"x =<<a1.m ( ) <<"\t y ="<<a1.n ( ) << "\n\n";
              getch ( );
          }   
  C++ dynamic Allocation aoperators : There are two operator new and delete in c++ that are known as dynamic Allocation operators . These are unary operators that are used dynamically allocating and deallocating the memory space .
  • New operator : The new operator allocates the memory space for an object and return a pointer that stores the address of allocated memory . The new operator can be used as : data _ type *pointervariable = new data_type .
Ex : int *p = new int ;
The new operator can allocates memory for any basis data type such as int , char , float ,double , and user defined data type such as class and structure . The above declaration uses data type two times . The first int specifies type of pointer variable and second int specify the required memory . When a new operator is called then it return a pointer to the allocated space .
  • Delete operator  : The working of delete operator is against the new operator  . The delete operator deallocates the memory space that has been allocated by the new operator . The delete operator can be written as delete *pointer ;
The delete operator takes number argument to release the memory . The operator new and delete works or memory on the free store . Therefore these are known as free store operators .
The new operator calls upon the function operator calls upon the function operator new ( ) to get memory and the delete operators calls upon the function operator delete ( ) to free the memory allocated by new operator .  
         
# include <iostream.h>
          #include <conio.h>
          void main ( ) 
         {
           char *p1 =new char ('A');
           int * p2 = new int (26);
          float * p3 = new float ( 5.5 );
          clrscr ( ) ;
          cout <<"===============\n\n";
          cout <<"grade of student = "<<*p1 <<"\n\n";
          cout <<"age of student = "<<*p2<<"\n\n";
          cout <<"height of student = "<<*p3 <<"\n\n";
          cout << "============";
        } 

Initialization Allocated memory : The allocated memory can be initialized using new operator . The syntax for initializing allocated memory is : syntax : data_type * pointer_variable = new data_type (value);
          Ex : int *p1 = new int (20) ;
          
The above statement will allocate the memory of 2 byte to hold the integer value and store 20 the this newly allocated memory and the integer pointer *p1 will hold the address of this memory .
 
        

0 comments:

Post a Comment

 

PROGRAMMINGqueen Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates

Blogger Widgets