Classes

Classes
  • Class is a user defined data type just like structures, but with a difference
  • It also has three sections namely private, public and protected
  • Using these, access to member variables of a class can be strictly controlled.
 The following is the general format of defining a class template

class tag_name
{   
public  :         // Must   
type member_variable_name;
        :   
type member_function_name();
        :
    
private :         // Optional  
type member_variable_name;                    
        :   
type member_function_name();                                         :
};


1.The keyword class is used to define a class template.

2.The private and public sections of a class are given    by the keywords ‘private’ and ‘public’ respectively.

3.Determine the accessibility of the members. All             the variables declared in the class, whether in the         private or the  public section, are the members of the       class.
4.The class scope is private by default.
  
  Conventions for naming classes

   Should be meaningful
   Should ideally be a noun
  First letter of every word should be in upper case

   Rules for naming classes
  Must not contain any embedded space or symbol
  Must begin with a letter, which may be followed by a sequence of  letters or digits
  Cannot be a keyword
  Member functions
 -Are means of passing messages and responding to them
  -Are declared inside the class body
  Example:
class Car
{
       void honk()
       {
      cout<<"BEEP BEEP!";
       }

    };
     Object 
   -Once a class is declared, an object of that type can be defined.
        -An object is said to be a specific instance of a class       just like Maruti car is an instance of a vehicle or pigeon   is the instance of  a bird.   
        -Once a class has been defined several objects of that
         type can  be declared

        -Objects can be passed to a function and returned back       just like normal variables.

        -When an object is passed by content , the compiler           creates another object as a formal variable in the called     function and copies all the data members from the actual     variable to it.

        -Objects can also be passed by address

         class check
  {
         public :
         check add(check);
             void get()
             {   
                     cin >> a;
             }

             void put()
             {
                     cout << a;
             }
                private :


              int
 a;
      };
     

      void main()
      {
             check c1,c2,c3;
             c1.get();
             c2.get();
             c3 = c1.add(c2);
             c3.put();
      }
       check check :: add ( check c2)
       {
        check temp;
        temp.a = a + c2.a;
        return ( temp);
       }
       
      The above example creates three objects of class check.
     




   
  
  
  




Post a Comment

0 Comments