Page 103 - DCAP404 _Object Oriented Programming
P. 103

Object-oriented Programming




                    Notes
                                          Example:
                                   const float g = 9.8;

                                   cout << “The acceleration due to gravity is” << g << “m/s2”;
                                   g=g+4; // ERROR!! Const variables cannot be modified.
                                   The const keyword specifies  that the values of  the variable will not change throughout  the
                                   program. This prevents the programmer from changing the value of variable, like the one in the
                                   example given  above. If  the keyword, const, has been used while defining the variable, the
                                   compiler will report an error if the programmer tries to modify it.

                                   5.2.1 The Class Keyword

                                   Syntax for the class keyword is as follows.
                                   class  class_name
                                   {

                                   //  access  control  keywords  here
                                   //  class  variables  and  methods  declared  here
                                   };
                                   You use the class keyword to declare new types. A class is a collection of class member data,
                                   which are variables  of various  types, including other classes. The class  also contains  class
                                   functions—or methods—which are functions used to manipulate the data in the class and to
                                   perform other services for the class. You define objects of the new type in much the same way in
                                   which you define any variable. State the type (class) and then the variable name (the object). You
                                   access the class members and functions by using the dot (.) operator. You use access control
                                   keywords to declare sections of the class as public or private. The default for access control is
                                   private. Each keyword changes the access control from that point on to the end of the class or
                                   until the next access control keyword. Class declarations end with a closing brace and a semicolon.


                                          Example:
                                   class  Cat
                                   {
                                   public:
                                   unsigned  int  Age;
                                   unsigned  int  Weight;
                                   void  Meow();

                                   };


                                   Cat    Frisky;
                                   Frisky.Age  =  8;
                                   Frisky.Weight  =  18;
                                   Frisky.Meow();





          96                                LOVELY PROFESSIONAL UNIVERSITY
   98   99   100   101   102   103   104   105   106   107   108