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

Friday, 28 March 2014

0 Friend function C++ language

Friend function C++ language


Friend function : A friend function is a non - member function of any class . It is declared friendly to  a class . Instead of not being a member function of the class it can access the private data of the class . As we know that private data and member function cannot be invoked outside the class . That is a non  - member function cannot have an access to private data of the class.
     Sometimes there is a situation where two different classes like to perform the same task using a common function this common function is not a member of any class . In such situation , c++ allow the common function to be made friendly with both classes , means allowing the function to have access to the private data of these classes . These function do not have need to be a member of any of these class . The friend function in the class may be declared as :
Syntax :
         Class A
         {
                     ---------------
                     ---------------
                     ---------------
         }
                       Public :
                                 ---------------
                                 ---------------
                        Friend void sum ( ) ;
       } ; 
 The function is declaration is preceded by the keyword friend . The friend function may be defined elsewhere in the program like a normal function . In the definition of the friend function the keyword friend and the scope operator : : is not used . A function can be declared as friend in any number of classes . A friend function is not a member function yet it has full access to the private member of the class .
Characteristics of a friend function are


  • It is not in the scope of the class to which it is declared as friend .
  • Since it is not in the scope of the class therefore it cannot be called using the object of that class .
  • It can invoke member function with object name and dot membership operator .
  • It can be declared either in the public or private section .
  • Normally it receive the objects as arguments.
/*Program to calculate average value of two number using friend function*/
 Class Average 
{
               int a , b ;
           Public :
                   Void Set value ( )
                    {
                       cout << " enter two number = ";
                        cin >> a >> b;
                     }
                   Friend void Avg ( Average A1) ;
          };
                   Void Avg (Average A1)
                   {
                      cout <<"Average of two number =  "  <<(A1.a + A1 .b)/2 ;
                    }
               Void main ( )
 {
                Average X ;
                clrscr ( );
                X . set value ( );          // calling member function of class 
                Avg (X );                    // Calling friend function 
               getch ( )
 /* Program to find maximum of two number of two different class using friend function * /
# include <iostream .h>
# include <conio .h>
 Class B ;
 Class A 
         {
              Int x ;
         Public :
                  Void getdata (int i)
                   {
                        X = i;
                    }
                     Friend void max (A , B) ;
           };
              Class B
            { 
                Int y;
                    Public : 
                           Void set value (int j)
                           {
                                y = j ;
                            }
                       Friend void max (A, B);
               };
                     Void max ( A m  , B  n)
                     {
                         if m.x > n.y
                            cout << "max is =" << m.x;
                         else
                           cout << "max is = "<< n.y;
                       } 
        Void main ( )
{
     A  A1;
               B  B1;
               Clrscr ( );
               A1. getdata (100);
               B1 . setvalue(200);
               Max (A1, B1);
                getch ( );
}  
 Friend classes : In c++ like a friend function , a class can be declared as a friend class. All the member function of the friend class have full access to the members of the class in which it is declared as friend . Also we can say that if a new class is declared as friend in an old class then all the members of new class can access all the members of the old class.
*Program to calculate  Square and cube of any number using friend class*
Class First
{
                 Int x , y ;
          Public :
                Void input ( )
                 {
                    cout << "enter two number = "
                    cin>> x>>y ;
                  }
                  Friend class second ;
};
            class second
            {
               Public :
               Void display ( )
                {
                  First f 1 ;
                  Int c , s ;
                  F1 . input ( ) ;
                 S = (f1.x ) * (f1.x);
                 C = (f1.y)*(f1.y)*(f1.y)
                  cout <<" square of number =" << S << end l ;
                  cout << " cube of number "<<e<<end l;
   }
} ;
               Void main ( )
              {
                 Second s1 ;
                 clrscr ( );
                 s1 . display ( );
                  getch  ( );
              } 
    Output :
                 Enter two number : 4 ,5 
                 Square of number : 16
                 Cube of number : 125
 Scope resolution operator (: :)  = It is made up of two colons ( : :) . This operator is used to differentiate between the class member names and the other name (global variables) . The scope resolution operator can be used as : : variable _ name .
If any program contains a variable with same name as global variable as well as a local variable of class . Then the local variable may be used in the class directly but the global variable is used with the scope resolution operator . We can also use scope resolution operator to define the member function outside the class .  
*Program to demonstrate the use of scope resolution operator *
Int x = 100 , y = 200 ;
         Class Sample 
        {
            Int x , y;
         Public :
                 Void display ( ) 
                  {
                        x = 50 , y = 60 ;
                        cout << " global x ="<< : : x << end l;
                        cout <<"local x ="<<x<<end l;
                        cout << " global y ="<< : : y << end l ;
                        cout << "local y ="<< y << end l ;
                  }
      };
       Void main ( )
      {
           Sample s1 ;
            clrscr ( ) ;
            s1.display ( );
            getch ( );
      }

       Output :
                   Global x = 100
                   Local x =50
                   Global y = 200
                  Local y = 60 
 Static class members  : Any data member or member function declared preceded by a keyword " static " , is known as static data member or member function . The static members those belong to any class are called static class member.

  • Static Data member :  Static Data member are normally used to maintain the values common to the entire class. Also they can be used as  a  counter that count the occurances of all objects in the program . It can be declared as : Static int x ; 
Characteristics of static data member
  1. It  is initialized to 0 when the first object is created . No other initialization are permitted .
  2. Only one copy of this variable is created for the class and it is shared by all the objects of the same class.
  3. It is visible only within the class : The static data member may be defines as :
         Data type class _ name : : variable name
                            Ex:    int net ::s; 
 
 
 

0 comments:

Post a Comment

 

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

Blogger Widgets