Page 104 - DCAP404 _Object Oriented Programming
P. 104

Unit 5: Static Members




                                                                                                Notes
                 Example:

          class  Car
          {
          public:                            // the next five are public


          void  Start();
          void  Accelerate();
          void  Brake();
          void  SetYear(int  year);
          int  GetYear();



          private:                           // the rest is private


          int  Year;
          Char  Model  [255];
          };                                 // end of class declaration



          Car OldFaithful;                   // make an instance of car
          int bought;                        // a local variable of type int
          OldFaithful.SetYear(84)  ;                    //  assign  84  to  the  year
          bought  =  OldFaithful.GetYear();        //  set  bought  to  84
          OldFaithful.Start();                              //  call  the  start  method

          5.3 Static Objects

          1.   A static object is a class all of whose members (functions and data) are declared static. E.g.,
               (source file)
               public  class  Static  Example  {
               static double x;                      // Static data
               static void printx ()                 // Static method
               {
               System.out.println  (“x  =  “  +  x);

               }
               public  static  void  main  (String[]  argv)
               {
               x  =  5.34;







                                           LOVELY PROFESSIONAL UNIVERSITY                                   97
   99   100   101   102   103   104   105   106   107   108   109