Thursday, December 27, 2012

Array Object – C++



The Array object is meant to store multiple values in a single variable. Object arrays are more flexible way to solve a problem. It store values of different types in a single collection. An object array also can store a and reference. Using of array of object also reduce the runtime performance.


Example – Input numbers using Array of Objects and print the sum


#include <iostream.h>

  class ObjArr
      {
           private:
              int num;
           public:
              void input()
                   {
                        cin>>num;
                   }
              int disp(int s)
                   {
                        cout<<num<<" ";
                        s=s+num;
                        return s;
                   }

      };

  void main()
      {
           clrscr();
           ObjArr OB[10];
           int i,sum=0;
           cout<<"\n\tEnter 10 No. \n";
           for(i=0;i<10;i++)
              {
                   cout<<"\t";
                   OB[i].input();
              }
           cout<<"\n\tValues. ";
           for(i=0;i<10;i++)
              {
                   sum=OB[i].disp(sum);
              }
           cout<<"\n\tSum = "<<sum;
      }


Example – Input ‘N’ numbers using Array of Objects and Sorting the elements using Bubble Sort Technique.


#include <iostream.h>

  class ObjArrSrt
      {
           private:
              int num;
           public:
              void Getdata();
              void Disp();
              void Bsort(ObjArrSrt *, int);
       };
  void ObjArrSrt::Getdata()
      {
           cout<<"\t";
           cin>>num;
      }
  void ObjArrSrt::Bsort(ObjArrSrt *Bub,int n)
      {
           int temp;
           for(int i=0;i<n;i++)
              {
                   for(int j=0;j<n-i-1;j++)
                        {
                            if(Bub[j].num>Bub[j+1].num)
                                {
                                      temp=Bub[j].num;
                                      Bub[j].num=Bub[j+1].num;
                                      Bub[j+1].num=temp;
                                }
                        }
              }
      }
  void ObjArrSrt::Disp()
      {
           cout<<num<<" ";
      }

  void main()
      {
           int n;
           ObjArrSrt *Ob,Obj;
           cout<<"\n\tEnter the Size : ";
           cin>>n;
           Ob=new ObjArrSrt[n];
           cout<<"\n\tEnter "<<n<<" Elements : ";
           for(int i=0;i<n;i++)
              {
                   Ob[i].Getdata();
              }
           Obj.Bsort(Ob,n);
           cout<<"\n\tOriginal Elements : ";
           for(i=0;i<n;i++)
              {
                   Ob[i].Disp();
              }
           cout<<"\n\tElements After Sorting : ";
           for(i=0;i<n;i++)
              {
                   Ob[i].Disp();
              }
           getch();
      }


Thursday, December 20, 2012

Friend Function – C++.


Friend functions are not considered as class members; they are like any other normal functions. We know that the private data member  of a class can be accessed only by the class' member functions. However , there is an exception. A function which declared with 'friend' keyword will be friendly  with any other class even though it is not a member of that class. The friend function can access the private members of the class. Friend function doesn't need any scope of the class.  They need not need to call with an object or with '.' and '->' operator.


Example : Adding two nos. using friend function.

#include <iostream.h>

  class Frinds;
  class Frinds_1
      {
           private :
              int x;
           public :
              void set(int n)
                   {
                        x=n;
                   }
              //Declaration of Friend Function max() in First Class
              friend void maximum(Frinds,Frinds_1);
      };
  class Frinds
      {
           private:
              int a;
           public:
              void setter(int n)
                   {
                        a=n;
                   }
              //Declaration of Friend Function max() in Second Class
              friend void maximum(Frinds, Frinds_1);
      };

  void maximum(Frinds f, Frinds_1 f1) //Friend Function max() without scope
      {
           if(f.a>f1.x)
              {
                   cout<<"\n\t"<<f.a<<" is big\n";
              }
           else
              {
                   cout<<"\n\t"<<f1.x<<" is big\n";
              }
      }
  void main()
      {
           clrscr();
           int n;
           Frinds f;
           Frinds_1 f1;
           cout<<"\n\tEnter value for first class : ";
           cin>>n;
           f.set(n);
           cout<<"\n\tEnter value for  second class : ";
           cin>>n;
           f1.set(n);
           maximum(f,f1); //calling the freind function
           getch();
      }


Example – Addition of Complex No.

#include <iostream.h>

  class Frinds
      {
           private :
              float re,im;
           public :
              void Read()
                   {
                        cout<<"\nEnter value for real : ";
                        cin>>re;
                        cout<<"\nEnter value for Imaginery : ";
                        cin>>im;
                   }
              void show(Frinds);
              friend Frinds complx(Frinds,Frinds);
      };
  void Frinds::show(Frinds f)
      {
           cout<<"\n"<<f.re<<" + "<<f.im<<"\n";
      }
  Frinds complx(Frinds f, Frinds f1)
      {
           Frinds f2;
           f2.re=f.re+f1.re;
           f2.im=f.im+f1.im;
           return f2;
      }
  void main()
      {
           clrscr();
           Frinds f,f1,f2;
           f.Read();
           f1.Read();
           f2=complx(f,f1);
           cout<<"First Input = ";
           f.show(f);
           cout<<"Second Input = ";
           f1.show(f1);
           cout<<"Result = ";
           f2.show(f2);
           getch();
      }

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