Thursday, December 13, 2012

Friend Class – C++



In C++, a friend class is which can access the "private" and "protected" data members and members method of the class in which it is declared as a friend. On declaration of friend class all member function of the friend class become friend of the class in which the friend class was declared.

If class A declares class B as its friend then class B can access all the data member including the one that declared as protected and private. In addition class B also have access of all member method of class A.



Example : In the following example, among two class  the class Friend1  declare the class Friend2 as friend. So, Friend2 class eligible to access the data member (x) and member method (getdata()) of Friend1 class with help of an object of Friend1.  In this program we add two numbers where the on number inputted from each class.

#include <iostream.h>
  class Friend1
     {
        private :
            friend class Friend2;
            int x;
        public:
            void getdata();
     };
  void Friend1:: getdata()
     {
        cout<<"\n\tEnter First No : ";
        cin>>x;
     }
  class Friend2
     {
        private:
            int y;
        public:
            void read();
            void sum(Friend1 temp);
     };

  void Friend2::read()
     {

        cout<<"\n\tEnter Second No : ";
        cin>>y;
     }
  void Friend2::sum(Friend1 tmp)
     {
        tmp.getdata();
        read();
        cout<<"\n\t Sum of "<<tmp.x<<" and "<<y<<"       =      "<<tmp.x+y;
     }

  void main()
     {
        Friend1 Fr1;
        Friend2 Fr2;
        Fr2.sum(Fr1);
     }






Example : In this program there are two classes are  TV and Remote, Where TV class is the mutual friend of  Remote to toggle the state of a remote variable State.

#include <iostream.h>
  class Remote
     {
        private :
            friend class TV;
            char State;
        public:
            Remote()
              {
                  State='I';
              }
            void remote();
     };
  void Remote:: remote()
     {
        if(State=='N')
            {
              cout<<"\n\t * * The Remote is now in Normal Mode * *\n";
            }
        else if(State=='I')
            {
              cout<<"\n\t * * The Remote is now in Interactive Mode * *\n ";
            }
     }
  class TV
     {
        private:
        enum mode{OFF = 0, ON =1};
        public:
            void Toggle(Remote);
     };

  void TV::Toggle(Remote tmp)
     {
        int TvState;
        char st;
        cout<<"\n\tWanter to Put TV on(y/n)? ";
        cin>>st;
        if(st=='y' || st=='Y')
            {
              mode(ON);
              cout<<"\n\tThe TV is On ";
              while(st!='X' || st!='x')
                  {
                       cout<<"\n\tEnter State of Remote (I)nteractive/(N)ormal/E(x)it : ";
                       cin>>st;
                       if(st>=97 && st<=122)
                          {
                               st-=32;
                          }
                       if(st=='I' || st=='N')
                          {
                               tmp.State=st;
                               tmp.remote();
                          }
                       else if(st=='X')
                          {
                               break;
                          }
                       else
                          {
                               cout<<"\n\tInvalid Input";
                          }
                  }
            }
            else
              {
                  cout<<"\n\tTv is OFF, You Cannot Change Remote State";
              }
     }

  void main()
     {
        clrscr();
        Remote Rm;
        TV T;
        Rm.remote();
        T.Toggle(Rm);
     }




No comments: