Page 87 - DCAP404 _Object Oriented Programming
P. 87

Object-oriented Programming




                    Notes                 integer  int1,  int2,  int3;
                                          int1.getdata(15);
                                          int2.getdata(25);
                                          cout<<“\n  the  value  of  x  for  object  int1  “;
                                          int1.disp();
                                          cout<<“\n  the  value  of  x  for  object  int2  “;
                                          int2.disp();  .

                                   cout<<“\n  the  sum  of  private  data  values  of  x  belonging  to  objects  int1  and
                                   int2  is  “;
                                          int3  =  int1.sum(int2);
                                          int3.disp();
                                          getche();
                                   }
                                   You should see the following output from the program.

                                   the value of x for object int1 15
                                   the value of x for object int2 25
                                   the sum of private data values of x belonging to objects int1 and int2 is 40



                                     Did u know?  What is Returning a Reference to a const object?
                                     Though the main reason for using const reference is efficiency, there are restrictions on
                                     when this choice could be used. If a function returns an object that is passed to it, either by
                                     object invocation or as a method argument, we can increase the efficiency of the method
                                     by having it pass a reference.

                                   Self Assessment

                                   Fill in the blanks:
                                   10.  You can use a class type to create instances or ………………….. of that class type.

                                   11.  In C++, unlike C, you do not need to precede declarations of class objects with the keywords
                                       union, struct, and class unless the ……………….. of the class is hidden.

                                   4.4 Accessing a Member of Class

                                   Member data items of a class can be static. Static data members are data objects that are common
                                   to all objects of a class. They exist only once in all objects of this class. The static members are
                                   used when the information is to be shared. They can be public or private data. The main advantage
                                   of using  a static  member is  to declare the global data which  should be  updated while  the
                                   program lives in memory.

                                   When a static member is declared private, the  non-member  functions  cannot access  these
                                   members. But a public static member can be accessed by any member of the class. The static data
                                   member should be created and initialized before the main function control block begins.





          80                                LOVELY PROFESSIONAL UNIVERSITY
   82   83   84   85   86   87   88   89   90   91   92